Ruby - Not able to read yaml file - ruby-on-rails-3

Hi all I am retriving data from database and storing it in a File.i am stroing data in yaml format.
require 'mysql2'
require 'yaml'
client = Mysql2::Client.new(:host => "localhost",:username => 'root' , :password => 'root' , :database => 'jgroup')
results = client.query("SELECT * FROM jivegroup")
File.open("D:/j.yml","w") do |file|
results.each do |index|
file.write(index.to_yaml);
end
end
below is my file "j.yml"
---
groupID: 1000
name: T1
description: ""
creationDate: 1209446456903
modificationDate: 1378128624533
---
groupID: 1001
name: T2
description:
creationDate: 1209446473683
modificationDate: 1378181717000
---
but whenver i am trying to load the above file with YAML::load it is giving my only first record. i want to load all records, plz help.
below is my code for reading yml file
YAML::load( File.read('D:/jivegroup.yml') )
{"groupID"=>1000, "name"=>"T1", "description"=>"", "creationDate"=>1209446456903, "modificationDate"=>1378128624533}

If you absolutely want to go with this file design, then use YAML::load_documents to load more than one record at a time.
I would suggest instead using an list in the file. It seems cleaner to me (as what you have is a list of similar records, not a set of unrelated documents).

Hey Finally found answer to my own question on stack overflow itself. i used YAML.load_stream(open("D:/jive_group.yml") instead YAML::load( File.read('D:/jivegroup.yml') )

Related

how to test carrierwave fog google in rspec with setting up the configuration

I have below configuration and I wanted to write TC for it in ruby. I am new to ruby and wanted to understand how we can set the configuration of Fog to point to mock and use it in test-case.
class TestUploader < CarrierWave::Uploader::Base
storage :fog
def fog_credentials
{
:provider => 'google',
:google_project =>'my project',
:google_json_key_location =>'myCredentialFile.json'
}
end
def fog_provider
'fog/google'
end
def fog_directory
'{#bucket-name}'
end
def store_dir
when :File
"#{file.getpath}/file"
when :audio
"#{file.getpath}/audio"
else
p " Invalid file "
end
end
end
class TestModel
mount_uploader :images, TestUploader
end
Could someone please assist me from configuring to write and execute the unit test on it with few example. Any help would be really appreciated.
From the test I did, I got the following sample code working with Google Cloud Storage using Fog gem:
require "fog/google"
# Uncomment the following line if you want to use Mock
#Fog.mock!
# Bucket name
bucket = "an-existing-bucket"
# Timestamp used as sample string
test = Time.now.utc.strftime("%Y%m%d%H%M%S")
connection = Fog::Storage.new({
:provider => "Google",
:google_project => "your-project",
:google_json_key_location => "path-to-key.json",
})
# Lists objects in a bucket
puts connection.list_objects(bucket)
#Creates new object
connection.put_object(bucket, test, test)
puts "Object #{test} was created."
It works in production, but fails using mock mode with the following error:
`not_implemented': Contributions welcome! (Fog::Errors::MockNotImplemented)
It seems that it is not implemented as shown at the put_object method definition in the documentation.
Also, this is said in this GitHub issue:
Closing issue. 1.0.0 is out, and we have no more mocks for json backed objects.
Credentials
As shown in the Fog's documentation, to configure Google Credentials you have to them as follows:
connection = Fog::Storage.new({
:provider => 'Google',
:google_storage_access_key_id => YOUR_SECRET_ACCESS_KEY_ID,
:google_storage_secret_access_key => YOUR_SECRET_ACCESS_KEY
})
Mock
In the GitHub - Fog::Google documentation, there is also a minimal config to integrate Fog with Carrierwave.
In order to use the Cloud Storage mock, you can use the following line:
Fog.mock!
connection = Fog::Storage.new(config_hash)
Provider Specific Resources
In the provider documentation section, you will find links to provider specific documentation and examples.
Community supported providers can get assistance by filing Github Issues on the appropriate repository.
Provider
Documentation
Examples
Support Type
Google
Documentation
fog-google-examples
Community
In order to maximize the benefits of open source, you are encouraged submit bugs to Github Issues
In this GitHub example, you could find an implementation for Google Cloud Storage.
Class List
At the RubyGems documentation for fog-google, you can find the class definitions and parameters. For example, the list_objects method:
#list_objects(bucket, options = {}) ⇒ Google::Apis::StorageV1::Objects
Lists objects in a bucket matching some criteria.
Parameters:
bucket (String) — Name of bucket to list
options (Hash) (defaults to: {}) — Optional hash of options
Options Hash (options):
:delimiter (String) — Delimiter to collapse objects under to emulate a directory-like mode
:max_results (Integer) — Maximum number of results to retrieve
:page_token (String) — Token to select a particular page of results
:prefix (String) — String that an object must begin with in order to be returned
:projection ("full", "noAcl") — Set of properties to return (defaults to “noAcl”)
:versions (Boolean) — If true, lists all versions of an object as distinct results (defaults to False)
Returns:
(Google::Apis::StorageV1::Objects)

Files uploaded to SoundCloud by API are downloaded with name 'SoundCloud Download' instead of original file name

We are constantly uploading audio to SoundlCloud from our site by API and today i've noticed that when i download track from an account interface it is saving as 'SoundCloud Download', even no extension.
I've checked other files uploaded and is looks like it started to happen around 2 weeks ago, because files older than 2 weeks are downloaded with their original file name. I've double checked that our upload solution didn't change and even implemented another one, but results were the same.
To me it looks like something has changed on their side. Does anyone experience same problem? Any ideas where to look at?
One of the solutions i'm using to test this is as follows:
$client = new \Njasm\Soundcloud\SoundcloudFacade(
'...',
'...'
);
$client->userCredentials('...', '...');
$filename = str_replace(' ', '_', $_FILES['file_data']['name']);
$pathName = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
move_uploaded_file($_FILES['file_data']['tmp_name'], $pathName);
$trackData = [
'title' => $_POST['title'],
'downloadable' => true,
'license' => 'cc-by-nc-sa',
'sharing' => 'private'
];
$track = $client->upload($pathName, $trackData);
it is an API Wrapper in PHP - https://github.com/njasm/soundcloud
Thanks.

Rails 3 + Paperclip: moving current data to another folder

As i'm new to Rails, I made the mistake of using the default path ( /system/:attachment/:id/:style/:filename ) in 4 different models. Now, i would like to move each model to its own folder but without losing old data.
What is the proper way of handling this? Does Paperclip offer an option to automatically migrate data from old folders?
Thanks.
I had a similar dilemma. We were storing all our attachments in a certain path, and then business requirements changed and everything had to be moved and re-organized.
I'm surprised how little info there is on changing paperclip path and moving files. Maybe I'm missing the obvious?
Like Fernando I had to write a rake task. Here's what my code looks like (attachments model is Attachment, and the actual Paperclip::Attachment object is :file )
task :move_attachments_to_institution_folders => :environment do
attachments = Attachment.all
puts "== FOUND #{attachments.size} ATTACHMENTS =="
old_path_interpolation = APP_CONFIG[ 'paperclip_attachment_root' ] + "/:id_partition.:extension"
new_path_interpolation = APP_CONFIG[ 'paperclip_attachment_root' ] + "/:institution/reports/:id_:filename"
attachments.each do |attachment|
# the interpolate method of paperclip takes the symbol variables and evaluates them to proper path segments.
old_file_path = Paperclip::Interpolations.interpolate(old_path_interpolation, attachment.file, attachment.file.default_style) #see paperclip docs
puts "== Current file path: #{old_file_path}"
new_file_path = Paperclip::Interpolations.interpolate(new_path_interpolation, attachment.file, attachment.file.default_style)
if File.exists?(old_file_path)
if !File.exists?(new_file_path) #don't overwrite
FileUtils.mkdir_p(File.dirname(new_file_path)) #create folder if it doesn't exist
FileUtils.cp(old_file_path, new_file_path)
puts "==== File copied (^_^)"
else
puts "==== File already exists in new location."
end
else
puts "==== ! Real File Not Found ! "
end
end
The key thing for me was to have paperclip re-calculate the old path by using its default interpolations. From then it was just a matter of using standard FileUtils to copy the file over. The copy takes care of renaming.
P.S.
I'm on rails 2.3.8 branch, with paperclip -v 2.8.0
I ended up creating a small rake task to do this. Assuming that you have a model called User and your image file is called "image", place the following code in lib/tasks/change_users_folder.rb
desc "Change users folder"
task :change_users_folder => :environment do
#users = User.find :all
#users.each do |user|
unless user.image_file_name.blank?
filename = Rails.root.join('public', 'system', 'images', user.id.to_s, 'original', user.image_file_name)
if File.exists? filename
user.image = File.new filename
user.save
end
end
end
end
Them, run rake change_users_folder and wait.
Note that this won't delete old files. They will be kept at the original place and a copy will be created at the new folder. If everything went well, you can delete them later.
And for my future code, i will make sure i always set :path and :url when using paperclip :)

Ruby on Rails - Opening a file using Spreadsheet RubyGem

I think I'm probably missing something quite simple, as I am new to this gem (and Ruby/Rails in general), but here we go...
I just installed the Spreadsheet gem from RubyGems and used Bundler to install it. Following that, I restarted my local server.
I want to create my database from an Excel file I have, but am struggling to open the file. The code I have is:
require 'spreadsheet'
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open('C:\Users\Lev Berlin\Documents\Personal\Projects
\FactsRus\Nutritional Analysis Models\Data for Rails model import.xls')
sheet1 = book.worksheet('Sheet1')
And the error I get after running >rails runner script/load_excel_file.rb (which has the code above) is:
Permission denied - C:\Users...import.xls (Errno::EACCES)
Like I said - I'm probably missing something very simple, but any pointers would be appreciated.
Problem was that the file I was trying to read from was open! N00b mistake, but I eventually figured it out.
Thanks all for looking.
use parseexcel in gem file instead of spreadsheet.
gem 'parseexcel'
write below code in db/seed.rb file place your excel file in project/db/data folder
def seed_cities
workbook = Spreadsheet::ParseExcel.parse("#{Dir.getwd}/db/data/cities.xls")
workbook.worksheet(0).each(1) { |row|
next if row == nil;
col = row.at(0);
next if col == nil;
id = col.to_s('latin1').strip;
next if id == "";
country_id = row.at(1).to_s('latin1').strip;
state_id = row.at(2).to_s('latin1').strip;
name = row.at(3).to_s('latin1').strip;
code = row.at(4).to_s('latin1').strip;
city = City.new(:country_id => country_id, :state_id => state_id, :name => name, :code => code)
unless city.save
city.errors.each { |e| puts "Database Error: ", e }
end
}
end
seed_cities()
this is my cities.xls excel file
id country_id state_id name code
1 7 77 Lahore LHR
2 7 77 Islamabad ISB

Rails 3 Carrierwave-Fog-S3 error: Expected(200) <=> Actual(404 Not Found)

I'm using Carrerwave 0.5.3 and getting a 404 error on my call to Picture.save in the Create method of my picture controller. Per the instructions in lib/carrierwave/storage/s3.rb I have the following in my initialization file (config/initializers/carrierwave_fog.rb):
CarrierWave.configure do |config|
config.s3_access_key_id = "xxxxx"
config.s3_secret_access_key = "xxxxx"
config.s3_bucket = "mybucket" #already created in my S3 account
end
In photo_uploader.rb I have:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :s3
def store_dir
"uploads" # already created in my s3 account
end
def cache_dir
"uploads/cache" #already created in my s3 account
end
end
The exact error:
Excon::Errors::NotFound in PicturesController#create
Expected(200) <=> Actual(404 Not Found)
request => {:expects=>200}
response => #<Excon::Response:0x00000104a72448 #body="", #headers={}, #status=404>
I found a slightly similar question here Carrierwave and s3 with heroku error undefined method `fog_credentials=' . But setting things up the way I have it now apparently worked in that case. Unfortunately it didn't for me.
I've put a picture in my bucket and set the permissions to public and can access the picture via a browser. So things on the AWS S3 side seem to be working.
Not sure where to go next. Any ideas?
Well, I slept on this for a night came back the next day and all was good. Not sure why it suddenly started working.
Make sure your file names are sanitized and do not contain invalid characters like spaces or slashes.
To sanitize a string you can call the gsub method on it. The following method call will sanitize files for upload to S3, Google Cloud Storage etc.
"Invalid\ file *& | | name.png".gsub(/[^0-9A-z.\-]/, '_')