I am trying to retrieve a binary file that I successfully stored using GridFS.
I am using MongoDB v3.0.6, Ruby 2.0.0 and MongoDB Ruby Driver v 2.0.1 on Mac OS X.
Is there a working example?
Looking over the docs, it seems like this should work:
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
client.database.fs.find_one(:filename => 'new-file.txt') #=> Returns a Mongo::Grid::File
Here's how to stream it to a file:
client.database.fs.open_download_stream(file_id) do |stream|
IO.write('some-file', stream.read)
end
The following code works:
require 'rubygems'
require 'mongo'
include Mongo
$client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'garden')
Mongo::Logger.logger.level = ::Logger::ERROR
$files = $client[:files]
puts 'connected!'
# Upload file
fs = $client.database.fs
$file = File.open("delete.rb")
$file_id = fs.upload_from_stream("delete.rb", $file)
$file.close
$file_to_write = File.open('perfectCopy', 'w')
fs.download_to_stream($file_id, $file_to_write)
Mihalis.
Related
I am creating a Ruby script which reads data from an Excel sheet and put its data on a MySQL database. I have written it and installed the necessary gems. However, when I try to run it via my cPanel host and I get the following error:
Array ( [0] => /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- ruby-mysql (LoadError) [1] => from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' [2] => from ../ruby/InsertarFaltantesExcel.rb:2 )
Ruby Code:
require 'rubygems'
require 'ruby-mysql'
require 'spreadsheet'
#load './spreadsheet.rb'
con = Mysql.connect('xx', 'xx', 'xx','xx')
ARGV = "--help" if ARGV.empty?
workbook = Spreadsheet.open(ARGV[0])
sheet = workbook.worksheet(0)
sheet.each do |row|
#faltantes = {
"id_verificador" => "#{row[0]}",
"order_id" => "#{row[1]}",
"id_proveedor" => "#{row[28]}",
"shipping" => "#{row[10]}",
"ean" => "#{row[4]}",
"isbn" => "#{row[5]}",
"description" => "#{row[8]}",
"sku" => "#{row[9]}",
"cost" => "#{row[40]}",
"order_price" => "#{row[14]}",
"master" => "#{row[39]}",
"quantity_purchased" => "#{row[11]}",
"total_price" => "#{row[12]}",
"condition" => "#{row[33]}",
"tracking" => "#{row[29]}"
}
insertar_faltantes(#faltantes, con)
end
def insertar_faltantes(hash, con)
statement - con.prepare("INSERT INTO articulos(art_id_verificador, art_id_orden, art_id_proveedor, art_shipping, art_N13, art_ISBN, art_titulo, art_SKU, art_cost, art_precio, art_master, art_cantidad, art_total, art_condition,
art_tracking) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);")
statement.execute "#{hash['id_verificador']}", "#{hash['order_id']}", "#{hash['id_proveedor']}", "#{hash['shipping']}", "#{hash['ean']}", "#{hash['isbn']}", "#{hash['sku']}", "#{hash['cost']}", "#{hash['order_price']}","#{hash['master']}",
"#{hash['quantity_purchased']}", "#{hash['condition']}", "#{hash['tracking']}"
end
The gems in ~/ruby/gems/gems/ are not being recognised by your Ruby executable. Find where all the other gems are being kept and move these ones into there.
Alternatively, try using a different package manager. If you have installed gems successfully in the past, use the manager you used for that.
I am creating a hash in Ruby and want to write it to a JSON file, in the correct format.
Here is my code:
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
fJson = File.open("public/temp.json","w")
fJson.write(tempHash)
fJson.close
And here is the contents of the resulting file:
key_aval_akey_bval_b
I'm using Sinatra (don't know what version) and Ruby v 1.8.7.
How can I write this to the file in the correct JSON format?
Require the JSON library, and use to_json.
require 'json'
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
File.open("public/temp.json","w") do |f|
f.write(tempHash.to_json)
end
Your temp.json file now looks like:
{"key_a":"val_a","key_b":"val_b"}
With formatting
require 'json'
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
File.open("public/temp.json","w") do |f|
f.write(JSON.pretty_generate(tempHash))
end
Output
{
"key_a":"val_a",
"key_b":"val_b"
}
This question is for ruby 1.8 but it still comes on top when googling.
in ruby >= 1.9 you can use
File.write("public/temp.json",tempHash.to_json)
other than what mentioned in other answers, in ruby 1.8 you can also use one liner form
File.open("public/temp.json","w"){ |f| f.write tempHash.to_json }
To make this work on Ubuntu Linux:
I installed the Ubuntu package ruby-json:
apt-get install ruby-json
I wrote the script in ${HOME}/rubybin/jsonDEMO
$HOME/.bashrc included:
${HOME}/rubybin:${PATH}
(On this occasion I also typed the above on the bash command line.)
Then it worked when I entered on the command line:
jsonDemo
I'm trying to run a Query against Google Big Query, using the Ruby API.
This is my first project with Ruby and I'm still learning the language.
This is also my first project using the Google API.
ENVIORNMENT:
Windows 7
Ruby 1.9
Faraday 0.90
Goolge API - Service Account Authentication
My Code runs without giving any warnings or error messages through:
#client.authorization.fetch_access_token!
doc = File.read('bigQueryAPI.json')
#bigQuery = #client.register_discovery_document('bigquery', 'v2', doc)
NOTE: #bigQuery is loaded from a file because when I try to load #bigquery with
#bigquery = #client.discovered_api('bigquery', 'v2')
I get Google::APIClient::ClientError: Not Found and inspect only prints
#<Google::APIClient::API:0x17c94cc ID:bigquery:v2>
However If I save the Big Query API as a text file from
https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest
and then load it as a text file with
doc = File.read('bigQueryAPI.json')
#bigQuery = #client.register_discovery_document('bigquery', 'v2', doc)
then #bigQuery.inspect actually returns something useful.
#bigQuery.inspect output.
However, When I try to actually run a query, like so:
result = #client.execute!(
:api_method => #bigQuery.batch_path.query,
:body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
"count(payload_commit) as commits_total, " +
"count(DISTINCT repository_name) / count(payload_commit) as average, " +
"FROM [githubarchive:github.timeline]" }, #,
:parameters => { "projectId" => #project_id })
I get the following error:
NoMethodError: undefined method `query_values' for nil:NilClass
Here's the Full Stacktrace of the error:
1) Error:
test_averages(Test_GitHub_Archive):
NoMethodError: undefined method `query_values' for nil:NilClass
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:145:in `uri='
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:101:in `initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `new'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `generate_request'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:583:in `execute!'
C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/GitHub_Archive.rb:39:in `get_averages'
C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/TestSpec/test_GitHub_Archive.rb:26:in `test_averages'
Here is the results for #client.inspect
NOTE: I would have pasted here, but my post ended over the length limit.
After doing some digging. It looks like I'm not passing the proper #bigQuery prameter to get the query function.
Looking at the dump for #bigQuery.inspect I need to pass the method at line 751.
However I can't seem to figure out how to pass that method.
If you strip out the rest of the inspect output the "path" looks like this:
{ "resources => { "jobs" => { "methods" => { "query"
I've tried #bigQuery.Jobs.query and that results in an error stating that #bigQuery.Jobs doesn't exist.
So am I creating #bigQuery correctly?
Why doesn't #bigQuery.Jobs.query work?
Here's how I got it to work with the bigquery.jobs.query method, which is probably what you need.
I had to set OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE because otherwise the authorization process would fail miserably, but that might be specific to min Win7/MgitSys environment. In any case, this specific line is not safe in prod.
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'google/api_client/auth/file_storage'
require 'openssl'
require 'json'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
# Initialize the client.
client = Google::APIClient.new(
:application_name => 'Example Ruby application',
:application_version => '1.0.0'
)
CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json"
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
# Initialize Google+ API. Note this will make a request to the
# discovery service every time, so be sure to use serialization
# in your production code. Check the samples for more details.
#bigQuery = client.discovered_api('bigquery', 'v2')
# Load client secrets from your client_secrets.json.
client_secrets = Google::APIClient::ClientSecrets.load
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
# The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed
# application flow, which ties in with FileStorage to store credentials
# between runs.
flow = Google::APIClient::InstalledAppFlow.new(
:client_id => client_secrets.client_id,
:client_secret => client_secrets.client_secret,
:scope => ['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/bigquery']
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
puts "authorized, requesting"
# Make an API call.
result = client.execute!(
:api_method => #bigQuery.jobs.query,
:body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
"count(payload_commit) as commits_total, " +
"count(DISTINCT repository_name) / count(payload_commit) as average, " +
"FROM [githubarchive:github.timeline]" }, #,
:parameters => { "projectId" => "845227657643" })
puts JSON.dump result.data
I'd like to use the Readability API through the Readit gem; however, I've been having some trouble trying to get an access token through XAuth. Here's the code that I have:
require 'highline/import'
require 'yaml'
require 'oauth'
require 'readit'
config = YAML.load_file("config/readability.yaml")
uname = ask ("Username: ")
passwd = ask ("Password: ") {|q| q.echo = false}
consumer = OAuth::Consumer.new(config["-consumer_key"], config["-consumer_secret"], :site => "https://www.readability.com/api/rest/v1/oauth/access_token/")
access_token = consumer.get_access_token(nil, {}, {:x_auth_mode => 'client_auth', :x_auth_username => uname, :x_auth_password => passwd})
However, when I try to run this, I get the following:
/Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:219:in `token_request': 404 NOT FOUND (OAuth::Unauthorized)
from /Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:109:in `get_access_token'
from instab.rb:11:in `<main>'
Can someone explain to me what I am doing wrong?
You should write as follows:
consumer = ::OAuth::Consumer.new(Readit::Config.consumer_key,Readit::Config.consumer_secret,:site=>"https://www.readability.com/", :access_token_path => "/api/rest/v1/oauth/access_token/")
I am creating a hash in Ruby and want to write it to a JSON file, in the correct format.
Here is my code:
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
fJson = File.open("public/temp.json","w")
fJson.write(tempHash)
fJson.close
And here is the contents of the resulting file:
key_aval_akey_bval_b
I'm using Sinatra (don't know what version) and Ruby v 1.8.7.
How can I write this to the file in the correct JSON format?
Require the JSON library, and use to_json.
require 'json'
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
File.open("public/temp.json","w") do |f|
f.write(tempHash.to_json)
end
Your temp.json file now looks like:
{"key_a":"val_a","key_b":"val_b"}
With formatting
require 'json'
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
File.open("public/temp.json","w") do |f|
f.write(JSON.pretty_generate(tempHash))
end
Output
{
"key_a":"val_a",
"key_b":"val_b"
}
This question is for ruby 1.8 but it still comes on top when googling.
in ruby >= 1.9 you can use
File.write("public/temp.json",tempHash.to_json)
other than what mentioned in other answers, in ruby 1.8 you can also use one liner form
File.open("public/temp.json","w"){ |f| f.write tempHash.to_json }
To make this work on Ubuntu Linux:
I installed the Ubuntu package ruby-json:
apt-get install ruby-json
I wrote the script in ${HOME}/rubybin/jsonDEMO
$HOME/.bashrc included:
${HOME}/rubybin:${PATH}
(On this occasion I also typed the above on the bash command line.)
Then it worked when I entered on the command line:
jsonDemo