cannot load such file -- Twitter, sinatra app on Heroku
You appear to be running:
require 'Twitter'
Note the capital T
. Does it work if you run instead:
require 'twitter'
It looks like your local machine is running OS X, which typically uses a case-insensitive HFS+ filesystem.
Heroku runs on Linux systems, which typically use case-sensitive filesystems.
sinatra and tweetstream wont work together
As mentioned above the server should be set to thin
.
Due to the event nature I'm not sure how the random_tweet_about
function above would work.
I was able to get TweetStream working with Sinatra by wrapping TweetStream::Client.new
in an EM.schedule
block. Perhaps competing event loops require it being in another thread.
require 'sinatra'
require 'tweetstream'
set :server, 'thin'
twitter = YAML.load_file('twitter.yml')['twitter']
TweetStream.configure do |config|
config.consumer_key = twitter['CONSUMER_KEY']
config.consumer_secret = twitter['CONSUMER_SECRET']
config.oauth_token = twitter['ACCESS_TOKEN']
config.oauth_token_secret = twitter['ACCESS_SECRET']
config.auth_method = :oauth
end
connections = []
get '/tweets', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
out.callback { connections.delete(out) }
end
end
EM.schedule do
TweetStream::Client.new.track('#yolo') do |status|
connections.each do |out|
out << status.text
end
end
end
Ruby/Sinatra app (file) does not run (start)
I just needed to add this code, and my sinatra app is now alive:
set :server, 'webrick'
twitter gem not working in heroku?
Have you created a .gems file and put twitter
in it?
Depending on which heroku 'stack' you are on, you may need to do that instead of a bundler Gemfile. You can change your stack for one that uses bundler by doing this:
heroku stack:migrate bamboo-ree-1.8.7
Which makes bundler available (I think).
Simple way to cache twitter gem tweet in Sinatra?
I use memcache (or now dalli) for stuff like that. There are two options. You could hit the cache first and if the timestamp is within a certain threshold, just return the cached value without incurring an API hit. Or you could use the API, cache the value, and in your rescue block return the cached value if you exceed the API threshold.
require "memcache"
cache = MemCache.new...
...
@twitter = cache.get("some_user").first
if @twitter.nil?
begin
@twitter = Twitter.user_timeline...
cache.set("some_user", @twitter) if @twitter
rescue ...
@twitter = default
end
end
or
require "memcache"
cache = MemCache.new...
...
begin
@twitter = Twitter.user_timeline...
cache.set("some_user", @twitter) if @twitter
rescue...
@twitter = cache.get("some_user").first||default
end
Then of course you'll need to be running the memcached daemon on the server.
Implement caching in Sinatra app to handle twitter rate limits
Assuming you keep your very simple scenario, you could use a small custom class to store the information and provide thread-safe methods (it is not clear from your question where your problem exactly resides, but this one problem will arise anyway):
require 'json'
require 'sinatra'
require 'date'
require 'thread'
require 'twitter'
set :server, 'webrick'
set :haml, :format => :html5
class MyCache
def initialize()
@mutex = Mutex.new
@last_update = DateTime.new # by default, -4732 BC
@client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
end
def get_cache
@mutex.synchronize do
if DateTime.now - @last_update > 10.0 / (3600 * 24)
@last_update = DateTime.now
arr = []
retweeters = @client.retweeters_of(429627812459593728)
retweeters.each do |retweeter|
ob = {}
ob[:name] = retweeter.name
ob[:followers_count] = retweeter.followers_count
arr.push(ob)
end
# remove the duplicates and sort on the users with the most followers,
sorted_influencers = arr.sort_by { |hsh| hsh[:followers_count] }
sorted_influencers.reverse!
@cache = sorted_influencers[0..9].to_s
end
@cache
end
end
end
my_cache = MyCache.new
get '/' do
content_type :json
my_cache.get_cache
end
This version now includes everything needed. I use the @client to store the instance of the twitter client (I suppose it's reusable), also note how the whole code is inside the if
statement, and at last we update @cache
. If you are unfamiliar with Ruby, the value of a block is determined by its last expression, so when I write @cache
alone it is as if I had written return @cache
.
Sinatra, Twitter, and StringIO
I faced with same issue but using Rails.
Problem is in size of image: if size of image is less than 10kb then open(photo_url)
will give you StringIO
object, if size is more then 10kb then - File
object which is saved in tmp/
folder. File
object respond's to to_io
method and StringIO
object doesn't.
What you can do - create file in tmp
folder from your file, and then use this this file for posting to TW.
For example:
img = open(url)
if img.is_a?(StringIO)
ext = File.extname(url)
name = File.basename(url, ext)
Tempfile.new([name, ext])
else
img
end
Sinatra server won't start - wrong number of arguments
Your thin
gem is a pre
version. Removing that gem and using thin 1.6.0 should fix the problem. gem uninstall thin && gem install thin
. Alternatively, you could use a different server, like puma. gem install puma
, then under require 'sinatra'
add configure { set :server, :puma }
Related Topics
Interested in What the "<<" Does
How to Convert JSON to a Hash, Search for and Change a Value
How to Merge Consecutive Repeating Elements in an Array
Multiple Applications Using a Single Code Base in Ruby
How to List the Available Variables in an Ruby Erb Template
How to Use Private Submit to Hide from Profile
Cucumber Not Finding Step Definitions
Is There a Best Directory to Place Image Uploads on Heroku
Zero-Length String Being Returned from String#Split
Heroku Rails 3.1 App - Compiling Assets Locally VS Compiling Assets During Slug Compilation
Error Loading Active Record Gem with Sinatra App Using Rvm
Rails 3 Order by Count on Has_Many :Through
Optional Parens in Ruby for Method with Uppercase Start Letter