Module: RequestWithOauth

Defined in:
lib/exts/request_with_oauth.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/exts/request_with_oauth.rb', line 2

def self.included(mod)
  mod.send :cattr_accessor, :common_params
  mod.common_params = {}
  
  mod.alias_method_chain :request, :oauth
  mod.send :cattr_accessor, :oauth_configuration    
end

Instance Method Details

#common_paramsObject



10
11
12
# File 'lib/exts/request_with_oauth.rb', line 10

def common_params
  self.class.common_params
end

#http_request(method, path, arguments) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/exts/request_with_oauth.rb', line 32

def http_request(method, path, arguments)
  # old way
  return http.send(method, path, *arguments) unless oauth?
  
  # new way
  logger.debug "(request with oauth)" if logger
  @http = http

  req = create_http_request(method, path, *arguments)
  req.oauth! @http, oauth_configuration.consumer, oauth_configuration.token, :signature_method => 'HMAC-SHA1', :clobber_request => true
  # logger.debug req.to_hash

  @http.request(req)
end

#oauth?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/exts/request_with_oauth.rb', line 47

def oauth?
  oauth_configuration && oauth_configuration.consumer && oauth_configuration.token
end

#request_with_oauth(method, path, *arguments) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/exts/request_with_oauth.rb', line 16

def request_with_oauth(method, path, *arguments)
  # append common parameters
  unless common_params.empty?
    path += path.include?('?') ? '&' : '?'
    path += common_params.to_query
  end
  
  logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
  result = nil
  time = Benchmark.realtime { result = http_request(method, path, arguments) }
  logger.info "--> #{result.code} #{result.message} (#{result.body ? result.body.length : 0}b %.2fs)" % time if logger
  handle_response(result)
rescue Timeout::Error => e
  raise TimeoutError.new(e.message)
end