Class: KSToken

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/kickscraper/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ KSToken

Returns a new instance of KSToken.



6
7
8
# File 'lib/kickscraper/connection.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kickscraper/connection.rb', line 10

def call(env)
  # replace '+' symbols in the query params with their original spaces, because there
  # seems to be a bug in the way some versions of Fararay escape parameters with spaces
  env[:url].query_params.each { |key, value|
    env[:url].query_params[key] = value.tr('+', ' ')
  }
  
  # add format=json to all public search requests, or add the oauth_token to all api requests once we have it
  if env[:url].to_s.index('https://api.kickstarter.com').nil?
    env[:url].query_params['format'] = 'json'
  else
    env[:url].query_params['oauth_token'] = Kickscraper.token unless Kickscraper.token.nil?
  end
  
  # make the call
  @app.call(env)
end