Class: Gimmie::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/gimmie/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Proxy

Returns a new instance of Proxy.



8
9
10
11
12
13
# File 'lib/gimmie/proxy.rb', line 8

def initialize(options = {})
  @cookie_key = options[:cookie_key] || ENV['GIMMIE_COOKIE_KEY']
  @oauth_key = options[:oauth_key] || ENV['GIMMIE_OAUTH_KEY']
  @oauth_secret = options[:oauth_secret] || ENV['GIMMIE_OAUTH_SECRET']
  @url_prefix = options[:url_prefix] || ENV['GIMMIE_URL_PREFIX'] || 'https://api.gimmieworld.com'
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gimmie/proxy.rb', line 20

def call(env)
  player_uid, ignore = CGI::Cookie::parse(env['HTTP_COOKIE'])[@cookie_key]
  suffix = env['REQUEST_URI'].to_s.split('gimmieapi=').last
  destination_url = "#{@url_prefix}#{suffix}"
  response = token(player_uid || '').get(destination_url)
  response_headers = {}
  response.each_capitalized do |key,value|
    response_headers[key] = value unless key == 'Transfer-Encoding' && value == 'chunked'
  end
  [response.code.to_i, response_headers, [response.body]]
end

#consumerObject



14
15
16
# File 'lib/gimmie/proxy.rb', line 14

def consumer
  @consumer ||= OAuth::Consumer.new(@oauth_key, @oauth_secret, :site => @url_prefix)
end

#token(access_token) ⇒ Object



17
18
19
# File 'lib/gimmie/proxy.rb', line 17

def token(access_token)
  OAuth::AccessToken.new(consumer, access_token, @oauth_secret)
end