public
Rubygem
Description: ActiveResource wrapper library for Springnote.com's REST API
Homepage: http://myruby.net/pages/391111
Clone URL: git://github.com/deepblue/springnote_resources.git
springnote_resources / lib / exts / request_with_oauth.rb
100644 36 lines (28 sloc) 1.218 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module RequestWithOauth
  def self.included(mod)
    mod.alias_method_chain :request, :oauth
    mod.send :cattr_accessor, :oauth_configuration
  end
  
  delegate :oauth_configuration, :to => "self.class"
    
  def request_with_oauth(method, path, *arguments)
    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 : 0}b %.2fs)" % time if logger
    handle_response(result)
  end
  
  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 = "Net::HTTP::#{method.to_s.capitalize}".constantize.new(path, *arguments)
    req.oauth! @http, oauth_configuration.consumer, oauth_configuration.token, :signature_method => 'HMAC-SHA1'
      
    @http.request(req)
  end
  
  def oauth?
    oauth_configuration && oauth_configuration.consumer && oauth_configuration.token
  end
end
 
ActiveResource::Connection.send :include, RequestWithOauth