Can't get Twitter gem search results in the model - ruby-on-rails

Still very new to Rails. How do I call my method in calltwitter.rb file located in my lib folder from my model? Basically I want the array returned from the calltwitter.rb into my model so I can store it.
I have these two classes:
lib/twitter/calltwitter.rb
require 'rubygems'
require 'twitter'
class CallTwitter
def search(search_string)
Twitter.search(search_string, :rpp => 5, :lang => "en", :result_type => "mixed").map do |result|
search_tweets << {:image_url => result.profile_image_url, :from_user => result.from_user, :tweet => result.text, :tweeteddate => result.created_at}
end
return search_tweets
end
and
require './lib/twitter/CallTwitter.rb'
class Tweet < ActiveRecord::Base
def get_search_tweets
search_tweets = CallTwitter.new
search_tweets.search("search string")
end
end

I am not sure but could you please try
**edit this in config/application.rb Hope it will help.
config.autoload_paths += %W(#{config.root}/lib/twitter/CallTwitter.rb)

Had to use "self" like so:
def self.get_search_tweets
search_tweets = CallTwitter.new
search_tweets.search("search string")
end
Also had to reload the rails console.
rails c

Related

Rails: upload cvs file to process it as a hash

I have this model:
class Survey < ActiveRecord::Base
attr_accessor :csvFile_file_name
has_attached_file :csvFile, :path => ":rails_root/public/:class/:attachment/:id/:style_:basename.:extension"
serialize :content, Hash
#after_save :do_cvs_process
def do_csv_process
product = {}
FasterCSV.foreach(self.csvFile.path, :headers => true, :col_sep => ",") do |row|
row.to_hash.each do |key, value|
product[key.underscore.to_sym] = value
end
end
self.update_column(:content, {:first => product})
end
end
I have several problems:
Because of standard browser security, I have to upload file and save it before processing it with csv to assign it as a hash to my :content attribute... That's why I'm using update_column to avoid callbacks. Is there a clever way to do it?
It does not work! When back to the view <%= #survey.content %> rails tells me that it found an array when it expected a hash.
def self.import_csv_file(iFileName)
c = CSV.open iFileName
header= c.first.map{ |i| i.to_s.strip.downcase; }
c.each { |row| import_hash( Hash[*header.zip(row).flatten] ); }
c.close
end
My product class has an import_hash method that looks for lowercase/spacefree headers and matches them to fields in the product.
product.name = hash['productname'] || hash['name'] #for example.
use faster_csv gem. Here are some quick links:
[SOURCE] https://github.com/JEG2/faster_csv
[DOCS] http://fastercsv.rubyforge.org/
[CHEATSHEET]http://cheat.errtheblog.com/s/faster_csv/
Please do some R&D on GitHub before pasting a questions, these questions are already there.

Rails create two associatiated models in one New-Create action

I have such models:
class Doc
has_many :photos
end
and
class Photo
belongs_to :doc
end
all photos uploaded to the cloud with CarrierWave-Paperclip like approach.
DocController#new prebuilds Doc with:
#doc = Doc.new
And only after saving this new Doc, in update action i can really upload photos to existed Doc object with:
#doc.photos << some_new_photo
But i want this feature in doc#new action. So, how i can upload photos like prebuilded Photo objects and add them to prebuilded Doc with #doc.photos << [photos] at the same time?
UPD:
Main problem, that when i make doc#new - i don't really know how many photos i'll upload during using form. So i have dynamically builded array of photos, that shouldn't be saved to DB, if associated Doc not saved/
You can use a somewhat convoluted Rails feature called accepts_nested_attributes which lets you create any number of associated objects in one go.
Basically your create call would end up accepting something like this:
{ :doc => { :name => 'somname', :date => Time.now, :photos_attributes => [
{ :filename => 'funnybear.gif', :filesize => '120kb' },
{ :filename => 'happybear.gif', :filesize => '72kb' },
{ :filename => 'angrybear.gif', :filesize => '240kb' }
]}}
You can use blocks
#doc = Doc.new do |doc|
doc.photos << some_new_photo
end
or you can redefine initialize method
class Doc
def initialize
#photos << some_new_photos
end
end
Or you can use build:
#doc = Doc.new
params[:photos].each do |some_new_photo|
#doc.photos.build some_new_photo
end
if #doc.save!
... etc

save! method for referenced attributes in mongoid

I use Rails 3.0.6 with mongoID 2.0.2. Recently I encountered an issue with save! method when overriding setter (I am trying to create my own nested attributes).
So here is the model:
class FeedItem
include Mongoid::Document
has_many :audio_refs
def audio_refs=(attributes_array, binding)
attributes_array.each do |attributes|
if attributes[:audio_track][:id]
self.audio_refs.build(:audio_track => AudioTrack.find(attributes[:audio_track][:id]))
elsif attributes[:audio_track][:file]
self.audio_refs.build(:audio_track => AudioTrack.new(:user_id => attributes[:audio_track][:user_id], :file => attributes[:audio_track][:file]))
end
end
if !binding
self.save!
end
end
AudioRef model (which is just buffer between audio_tracks and feed_items) is:
class AudioRef
include Mongoid::Document
belongs_to :feed_item
belongs_to :audio_track
end
And AudioTrack:
class AudioTrack
include Mongoid::Document
has_many :audio_refs
mount_uploader :file, AudioUploader
end
So here is the spec for the FeedItem model which doesn`t work:
it "Should create audio_track and add audio_ref" do
#audio_track = Fabricate(:audio_track, :user_id => #author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3"))
#feed_item= FeedItem.new(
:user => #author,
:message => {:body => Faker::Lorem.sentence(4)},
:audio_refs => [
{:audio_track => {:id => #audio_track.id}},
{:audio_track => {:user_id => #author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3")}}
]
)
#feed_item.save!
#feed_item.reload
#feed_item.audio_refs.length.should be(2)
end
As you can see, the reason I am overriding audio_refs= method is that FeedItem can be created from existing AudioTracks (when there is params[:audio_track][:id]) or from uploaded file (params[:audio_track][:file]).
The problem is that #feed_item.audio_refs.length == 0 when I run this spec, i.e. audio_refs are not saved. Could you please help me with that?
Some investigation:
1) binding param is "true" by default (this means we are in building mode)
I found a solution to my problem but I didnt understand why save method doesnt work and didn`t make my code work. So first of all let me describe my investigations about the problem. After audio_refs= is called an array of audio_refs is created BUT in any audio_ref is no feed_item_id. Probably it is because the feed_item is not saved by the moment.
So the solution is quite simple - Virtual Attributes. To understand them watch corresponding railscasts
So my solution is to create audio_refs by means of callback "after_save"
I slightly changed my models:
In FeedItem.rb I added
attr_writer :audio_tracks #feed_item operates with audio_tracks array
after_save :assign_audio #method to be called on callback
def assign_audio
if #audio_tracks
#audio_tracks.each do |attributes|
if attributes[:id]
self.audio_refs << AudioRef.new(:audio_track => AudioTrack.find(attributes[:id]))
elsif attributes[:file]
self.audio_refs << AudioRef.new(:audio_track => AudioTrack.new(:user_id => attributes[:user_id], :file => attributes[:file]))
end
end
end
end
And the spec is now:
it "Should create audio_track and add audio_ref" do
#audio_track = Fabricate(:audio_track, :user_id => #author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3"))
#feed_item= FeedItem.new(
:user => #author,
:message => {:body => Faker::Lorem.sentence(4)},
:audio_tracks => [
{:id => #audio_track.id},
{:user_id => #author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3")}
]
)
#feed_item.save!
#feed_item.reload
#feed_item.audio_refs.length.should be(2)
end
And it works fine!!! Good luck with your coding)
Check that audio_refs=() is actually being called, by adding debug output of some kind. My feeling is that your FeedItem.new() call doesn't use the audio_refs=() setter.
Here's the source code of the ActiveRecord::Base#initialize method, taken from APIdock:
# File activerecord/lib/active_record/base.rb, line 1396
def initialize(attributes = nil)
#attributes = attributes_from_column_definition
#attributes_cache = {}
#new_record = true
#readonly = false
#destroyed = false
#marked_for_destruction = false
#previously_changed = {}
#changed_attributes = {}
ensure_proper_type
populate_with_current_scope_attributes
self.attributes = attributes unless attributes.nil?
result = yield self if block_given?
_run_initialize_callbacks
result
end
I don't currently have an environment to test this, but it looks like it's setting the attributes hash directly without going through each attribute's setter. If that's the case, you'll need to call your setter manually.
Actually, I think the fact you're not getting an exception for the number of arguments (binding not set) proves that your setter isn't being called.

Is there find_or_create_by_ that takes a hash in Rails?

Here's some of my production code (I had to force line breaks):
task = Task.find_or_create_by_username_and_timestamp_and_des \
cription_and_driver_spec_and_driver_spec_origin(username,tim \
estamp,description,driver_spec,driver_spec_origin)
Yes, I'm trying to find or create a unique ActiveRecord::Base object. But in current form it's very ugly. Instead, I'd like to use something like this:
task = Task.SOME_METHOD :username => username, :timestamp => timestamp ...
I know about find_by_something key=>value, but it's not an option here. I need all values to be unique. Is there a method that'll do the same as find_or_create_by, but take a hash as an input? Or something else with similat semantics?
Rails 3.2 first introduced first_or_create to ActiveRecord. Not only does it have the requested functionality, but it also fits in the rest of the ActiveRecord relations:
Task.where(attributes).first_or_create
In Rails 3.0 and 3.1:
Task.where(attributes).first || Task.create(attributes)
In Rails 2.1 - 2.3:
Task.first(:conditions => attributes) || Task.create(attributes)
In the older versions, you could always write a method called find_or_create to encapsulate this if you'd like. Definitely done it myself in the past:
class Task
def self.find_or_create(attributes)
# add one of the implementations above
end
end
I also extend the #wuputah's method to take in an array of hashes, which is very useful when used inside db/seeds.rb
class ActiveRecord::Base
def self.find_or_create(attributes)
if attributes.is_a?(Array)
attributes.each do |attr|
self.find_or_create(attr)
end
else
self.first(:conditions => attributes) || self.create(attributes)
end
end
end
# Example
Country.find_or_create({:name => 'Aland Islands', :iso_code => 'AX'})
# take array of hashes
Country.find_or_create([
{:name => 'Aland Islands', :iso_code => 'AX'},
{:name => 'Albania', :iso_code => 'AL'},
{:name => 'Algeria', :iso_code => 'DZ'}
])

How to convert a Ruby object to JSON

I would like to do something like this:
require 'json'
class Person
attr_accessor :fname, :lname
end
p = Person.new
p.fname = "Mike"
p.lname = "Smith"
p.to_json
Is it possible?
Yes, you can do it with to_json.
You may need to require 'json' if you're not running Rails.
To make your Ruby class JSON-friendly without touching Rails, you'd define two methods:
to_json, which returns a JSON object
as_json, which returns a hash representation of the object
When your object responds properly to both to_json and as_json, it can behave properly even when it is nested deep inside other standard classes like Array and/or Hash:
#!/usr/bin/env ruby
require 'json'
class Person
attr_accessor :fname, :lname
def as_json(options={})
{
fname: #fname,
lname: #lname
}
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
p = Person.new
p.fname = "Mike"
p.lname = "Smith"
# case 1
puts p.to_json # output: {"fname":"Mike","lname":"Smith"}
# case 2
puts [p].to_json # output: [{"fname":"Mike","lname":"Smith"}]
# case 3
h = {:some_key => p}
puts h.to_json # output: {"some_key":{"fname":"Mike","lname":"Smith"}}
puts JSON.pretty_generate(h) # output
# {
# "some_key": {
# "fname": "Mike",
# "lname": "Smith"
# }
# }
Also see "Using custom to_json method in nested objects".
Try it. If you're using Ruby on Rails (and the tags say you are), I think this exact code should work already, without requiring anything.
Rails supports JSON output from controllers, so it already pulls in all of the JSON serialization code that you will ever need. If you're planning to output this data through a controller, you might be able to save time by just writing
render :json => #person

Resources