I am getting the following error when trying to call tweet.entities in my twitter feed controller.
private method `entities' called for #Twitter::Status:0x94d4bf4
I have no methods called entities in the code, I even did a full file search to check.
I either need to rename the entities part of the object or somehow find where this so called private method is or circumnavigate it somehow. My method within my twitter_feeds controller is as follows:
def hometimeline
#user = User.find(current_user.id)
tweets = #user.tweeting.user_timeline(count: '10', include_entities: true)
parsed_tweets = []
i = 0
tweets.each do |tweet|
more = Hash.new
more['test'] = tweet
internal_hash = Hash.new
mappings = {"source" => "fixed"}
another = more['test']
boo = Array(tweet)
#newhash = Hash[twee.map {|k, v| [mapping[k], v] }]
#newhash = Hash[more.map {|k, v| [mappings[k], v] }]
#newhash = Hash[tweet.map {|k, v| [mappings[k] || k, v] }]
internal_hash['parsed_text'] = tweet.entities
internal_hash['id'] = tweet.id
internal_hash['raw_text'] = tweet.text
internal_hash['name'] = tweet.user.name
internal_hash['screen_name'] = tweet.user.screen_name
internal_hash['user_id'] = tweet.user.id
internal_hash['user_image_url'] = tweet.user.profile_image_url
parsed_tweets << internal_hash
i = i + 1
end
respond_to do |format|
format.json { render json: parsed_tweets }
end
end
Essentially I am sending a set of parsed tweets to the client and want to achieve link wrapping etc. on the server.
Is there any way I can find where the code thinks this method is or is there a way to make it not private or ignore it so I can call tweet.entities.
EDIT: One extra point to note is that I did have an entity model and entities controller but have deleted them and still get this.
Thanks
Turns out this error is caused by the Twitter gem and they have methods for calling the entities object namely, tweet.urls, tweet.user_mentions etc. rather than tweet.entities.user_mentions as I would have thought.
Docs are here http://rdoc.info/gems/twitter/Twitter/Tweet
Related
I am working on a CLI Project and trying to open up a web page by using url variable declared in another method.
def self.open_deal_page(input)
index = input.to_i - 1
#deals = PopularDeals::NewDeals.new_deals
#deals.each do |info|
d = info[index]
#product_url = "#{d.url}"
end
#product_url.to_s
puts "They got me!"
end
def self.deal_page(product_url)
#self.open_deal_page(input)
deal = {}
html = Nokogiri::HTML(open(#product_url))
doc = Nokogiri::HTML(html)
deal[:name] = doc.css(".dealTitle h1").text.strip
deal[:discription] = doc.css(".textDescription").text.strip
deal[:purchase] = doc.css("div a.button").attribute("href")
deal
#binding.pry
end
but I am receiving this error.
`open': no implicit conversion of nil into String (TypeError)
any possible solution? Thank you so much in advance.
Try returning your #product_url within your open_deal_page method, because now you're returning puts "They got me!", and also note that your product_url is being created inside your each block, so, it won't be accessible then, try creating it before as an empty string and then you can return it.
def open_deal_page(input)
...
# Create the variable
product_url = ''
# Assign it the value
deals.each do |info|
product_url = "#{info[index].url}"
end
# And return it
product_url
end
In your deal_page method tell to Nokogiri to open the product_url that you're passing as argument.
def deal_page(product_url)
...
html = Nokogiri::HTML(open(product_url))
...
end
Here's an extract of the code that I am using:
def retrieve(user_token, quote_id, check="quotes")
end_time = Time.now + 15
match = false
until Time.now > end_time || match
#response = http_request.get(quote_get_url(quote_id, user_token))
eval("match = !JSON.parse(#response.body)#{field(check)}.nil?")
end
match.eql?(false) ? nil : #response
end
private
def field (check)
hash = {"quotes" => '["quotes"][0]',
"transaction-items" => '["quotes"][0]["links"]["transactionItems"]'
}
hash[check]
end
I was informed that using eval in this manner is not good practice. Could anyone suggest a better way of dynamically checking the existence of a JSON node (field?). I want this to do:
psudo: match = !JSON.parse(#response.body) + dynamic-path + .nil?
Store paths as arrays of path elements (['quotes', 0]). With a little helper function you'll be able to avoid eval. It is, indeed, completely inappropriate here.
Something along these lines:
class Hash
def deep_get(path)
path.reduce(self) do |memo, path_element|
return unless memo
memo[path_element]
end
end
end
path = ['quotes', 0]
hash = JSON.parse(response.body)
match = !hash.deep_get(path).nil?
I'm banging my head trying to understand how the Twitter gem's pagination works.
I've tried max_id and cursor and they both strangely don't work.
Basically the maximum I can get out of search results is 100, and I would like to get 500.
Current code:
max_page = 5
max_id = -1
#data = []
for i in (1..max_page)
t = twt_client.search("hello world", :count => 100, :result_type => :recent, :max_id => max_id)
t.each do | tweet |
#data << tweet
end
max_id = t.next_results[:max_id]
end
This actually tells me that next_results is a private method, anyone has a working solution?
Without knowing which gem you're referencing (please specify a URL), I'd say intiuitively that cursor and max_id wouldn't get you what you want. However count would. Since you say you're only retrieving 100 results and count is set to 100, that would make sense to me.
t = twt_client.search("hello world", :count => 500, :result_type => :recent, :max_id => max_id)
I'm assuming you're talking about the Twitter client referenced here. My first question is: What's twt_client and for that matter, what does its search method return? It's also possible that you've unwittingly updated the gem and there's been a code base change that makes your current script out of date.
Take a look at your installed gem version and another look at the README here.
Twitter::SearchResults#next_results is private, because they try to provide uniform interface for enumeration.
Look, there's included Twitter::Enumerable in search_results.rb
module Twitter
class SearchResults
include Twitter::Enumerable
...
private
def last?
!next_page?
end
...
def fetch_next_page
response = #client.send(#request_method, #path, next_page).body
self.attrs = response
end
...
end
end
And if you look at enumerable.rb, you'll see that method's Twitter::SearchResults#last? and Twitter::SearchResults#fetch_next_page are used by Twitter::SearchResults#each method
module Twitter
module Enumerable
include ::Enumerable
# #return [Enumerator]
def each(start = 0)
return to_enum(:each, start) unless block_given?
Array(#collection[start..-1]).each do |element|
yield(element)
end
unless last?
start = [#collection.size, start].max
fetch_next_page
each(start, &Proc.new)
end
self
end
...
end
end
And Twitter::SearchResults#each will iterate over pages until there's #attrs[:search_metadata][:next_results] in Twitter's responses. So you need to break iteration after you'll reach 500th element.
I think you just need to use each
#data = []
tweet_number = 1
search_results = twt_client.search("hello world", count: 100, result_type: :recent)
search_results.each do |tweet|
#data << tweet
break if tweet_number == 500
end
This post is a result of looking into gem's sources and twitter's api. I could make a mistake somewhere, since I haven't checked my thoughts in console.
Try this (I basically only updated the calculation of the max_id in the loop):
max_page = 5
max_id = -1
#data = []
for i in (1..max_page)
t = twt_client.search("hello world", :count => 100, :result_type => :recent, :max_id => max_id)
t.each do | tweet |
#data << tweet
end
max_id = t.to_a.map(&:id).max + 1 # or may be max_id = t.map(&:id).max + 1
end
I'm using Ruby 1.9.3 RSS::Maker module to generate a RSS 2.0 feed for a podcast. I'd like to start including a <content:encoded> element for show notes. This is my code for generating the RSS.
s3_bucket = AWS::S3::Bucket.find(options[:bucket])
content = RSS::Maker.make(version) do |m|
m.channel.title = options[:title]
m.channel.link = options[:link]
m.channel.description = options[:desc]
m.channel.language = options[:language]
m.channel.itunes_image = options[:image]
m.items.do_sort = true
s3_bucket.select{|object| object.key =~ /[\s\w]+\.(m4b|mp3|m4a|ogg|aac)/}.each do |audio|
i = m.items.new_item
i.link = audio.url(:authenticated => false)
i.title = audio.key.split(".")[0]
i.author = options[:author]
i.pubDate = audio.last_modified
i.guid.content = audio.etag
i.enclosure.url = i.link
i.enclosure.length = audio.content_length
i.enclosure.type = audio.content_type
# Insert content:encoded code here
end
end
For generating the <content:encoded> element I've tried:
i.encoded :content audio.metadata[:content]
i.encoded :content, audio.metadata[:content]
i.content = audio.metadata[:content]
i.content.encoded = audio.metadata[:content]
i.encoded = audio.metadata[:content]
i.encoded.content = audio.metadata[:content]
None of these work and most throw a NoSuchMethod exception - which is not surprising based on the documentation of the RSS::Maker module.
Is there a way using RSS::Maker to add arbitrary elements with a namespace?
You’re looking for i.content_encoded.
The documentation isn’t too great, so I used i.methods to get a list of methods I can use on feed items. methods works on all objects.
The list is quite long, so you most likely only want to show methods you can write to, i.e. something like this:
RSS::Maker.make("2.0") do |ma|
puts ma.items.new_item.methods.keep_if { |m|
m.to_s.end_with?("=")
}.join("\n")
end
I have this code that tries to return a value from a method:
temp = "123"
return temp
and I have this line that calls the method and assigns the return value:
person_connections = #client.get_person_connections(:id => current_user_id )
but when I try to inspect person_connections, it shows some different object string. Any idea how to return the actual value of the temp variable?
def get_person_connections(options = {})
person_id = options[:id]
path = "/people/id=" + person_id + ":(num-connections)"
query_connections(path, options)
self
end
and
private
def query_connections(path, options={})
fields = options.delete(:fields) || LinkedIn.default_profile_fields
if options.delete(:public)
path +=":public"
elsif fields
path +=":(#{fields.map{ |f| f.to_s.gsub("_","-") }.join(',')})"
end
headers = options.delete(:headers) || {}
params = options.map { |k,v| v.is_a?(Array) ? v.map{|i| "#{k}=#{i}"}.join("&") : "#{k}=#{v}" }.join("&")
path += "?#{params}" if not params.empty?
temp_var = get(path, headers)
hash = JSON.parse(temp_var)
conn = hash["numConnections"]
end
As Samy said in a comment:
In Ruby, the last statement will be returned.
So if we take a look at get_person_connections, we see that the last line is self. What it means is that it returns the instance on which the method was called, #client in this case.
Additional notes: the solution would be to remove self, although if the method is used elsewhere be careful as returning self is often used to allow chaining of methods (though it hardly makes sense to do that on a get method).