class OAuth::Consumer
def access_token_path
def access_token_path @options[:access_token_path] end
def access_token_url
def access_token_url @options[:access_token_url]||site+access_token_path end
def authorize_path
def authorize_path @options[:authorize_path] end
def authorize_url
def authorize_url @options[:authorize_url]||site+authorize_path end
def create_http_request(http_method,path,*arguments)
def create_http_request(http_method,path,*arguments) http_method=http_method.to_sym if [:post,:put].include?(http_method) data=arguments.shift end headers=(arguments.first.is_a?(Hash) ? arguments.shift : {}) case http_method when :post request=Net::HTTP::Post.new(path,headers) when :put request=Net::HTTP::Put.new(path,headers) when :get request=Net::HTTP::Get.new(path,headers) when :delete request=Net::HTTP::Delete.new(path,headers) when :head request=Net::HTTP::Head.new(path,headers) else raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}" end if data.is_a?(Hash) request.set_form_data(data) elsif data request.body=data.to_s end request end
def create_signed_request(http_method,path, token=nil,request_options={},*arguments)
Creates and signs an http request.
def create_signed_request(http_method,path, token=nil,request_options={},*arguments) request=create_http_request(http_method,path,*arguments) sign!(request,token,request_options) request end
def get_request_token
def get_request_token response=token_request(http_method,request_token_path) OAuth::RequestToken.new(self,response[:oauth_token],response[:oauth_token_secret]) end
def http
def http @http ||= Net::HTTP.new(uri.host, uri.port) end
def http_method
def http_method @http_method||=@options[:http_method]||:post end
def initialize(consumer_key,consumer_secret,options={})
def initialize(consumer_key,consumer_secret,options={}) # ensure that keys are symbols @options=@@default_options.merge( options.inject({}) do |options, (key, value)| options[key.to_sym] = value options end) @key = consumer_key @secret = consumer_secret end
def request(http_method,path, token=nil,request_options={},*arguments)
@consumer.request(:post,'/people',@token,{},@person.to_xml,{ 'Content-Type' => 'application/xml' })
@consumer.request(:get,'/people',@token,{:scheme=>:query_string})
The arguments parameters are a hash or string encoded set of parameters if it's a post request as well as optional http headers.
It's recommended to use the Token classes to set this up correctly.
Creates, signs and performs an http request.
def request(http_method,path, token=nil,request_options={},*arguments) http.request(create_signed_request(http_method,path,token,request_options,*arguments)) end
def request_token_path
def request_token_path @options[:request_token_path] end
def request_token_url
def request_token_url @options[:request_token_url]||site+request_token_path end
def scheme
def scheme @options[:scheme] end
def sign!(request,token=nil, request_options = {})
def sign!(request,token=nil, request_options = {}) request.oauth!(http,self,token,{:scheme=>scheme}.merge(request_options)) end
def signature_base_string(request,token=nil, request_options = {})
def signature_base_string(request,token=nil, request_options = {}) request.signature_base_string(http,self,token,{:scheme=>scheme}.merge(request_options)) end
def site
def site @options[:site] end
def token_request(http_method,path,token=nil,request_options={},*arguments)
def token_request(http_method,path,token=nil,request_options={},*arguments) response=request(http_method,path,token,request_options,*arguments) if response.code=="200" CGI.parse(response.body).inject({}){|h,(k,v)| h[k.to_sym]=v.first;h} else response.error! end end
def uri(url=nil)
def uri(url=nil) @uri||=URI.parse(url||site) end