Guest User

attempt.rb

a guest
Sep 17th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.11 KB | None | 0 0
  1. require 'sinatra'
  2. require 'omniauth-twitter'
  3. require 'twitter'
  4. require 'yaml'
  5. require 'active_support/core_ext/numeric/time'
  6.  
  7. # Enable Ramon! http://espn.go.com/nba/player/_/id/3231/ramon-sessions
  8. # because You must provide a session to use OmniAuth.
  9. enable :sessions
  10.  
  11. # TIL -- by default Sinatra looks for Rack handlers with namespace names:
  12. # HTTP and WEBrick, in that order.
  13. # Since the HTTP namespace has been defined Sinatra actually thinks it's a Rack handle
  14. # So unless you use Thin client, you have to explicitly set webrick
  15. set :server, 'webrick'
  16.  
  17. configure do
  18.   @@config = YAML.load_file("config.yml") rescue nil || {}
  19. end
  20.  
  21. # Set up omniauth
  22. use OmniAuth::Builder do
  23.   provider :twitter, @@config['api']['consumer_key'], @@config['api']['consumer_secret']
  24. end
  25.  
  26. helpers do
  27.   def get_xdays()
  28.     Time.zone = 'Singapore'
  29.     x_days = (Time.zone.parse(@@config['misc']['target_date'])- Time.zone.now).to_i/1.day
  30.   end
  31.  
  32.   def send_tweet()
  33.     # Set up twitter
  34.     client = Twitter::REST::Client.new do |config|
  35.       config.consumer_key        = @@config['api']['consumer_key']
  36.       config.consumer_secret     = @@config['api']['consumer_secret']
  37.       config.access_token        = @@config['access']['token']
  38.       config.access_token_secret = @@config['access']['secret']
  39.     end
  40.  
  41.     # Check if there is a file
  42.     # I made the image names the # on jersey
  43.     File.file?("images/#{get_xdays()}.jpg") ? img_path = "images/#{get_xdays()}.jpg" : img_path = "images/404.jpg"
  44.  
  45.     # Use it to tweet!!!
  46.     client.update_with_media("OWWW YEAAAH!!! #{get_xdays()}", File.new(img_path))
  47.  
  48.     # ========= JUST MY NOTES =========
  49.     # OK so posting with just text is easy
  50.     # client.update("Test #{get_xdays()}  #{Time.new.strftime("%Y-%m-%d %H:%M:%S")}")
  51.  
  52.     # Even posting a tweet with photo on your server is easy
  53.     # client.update_with_media("test wit photo frm local", File.new('lebrick.png'))
  54.  
  55.     # Tweeting a photo from a URL needs another gem :(
  56.     # client.update_with_media("test wit photo", open(url))
  57.     # ========= JUST MY NOTES =========
  58.  
  59.   end
  60. end
  61.  
  62. get '/' do
  63.   send_tweet()
  64. end
Advertisement
Add Comment
Please, Sign In to add comment