class RestfullOauth::Connection
This Gem will enable you to communicate to any restfull service using Oauth v1 If you only have a consumer_token and consumer_secret then you might need to get a verifier code Use this command to get your app authenticated: % authenticate Example:
% irb
>> require 'restfull_oauth' >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}) >> post_data = {'foo' => 'bar' }.to_json >> response = RestfullOauth::connect('POST','https://your server/rest/api', post_data) => #<Net::HTTPOK 200 OK readbody=true> >> puts JSON.parse(response.body).to_yaml
Public Class Methods
new(config = {})
click to toggle source
# File lib/restfull_oauth.rb, line 29 def initialize(config = {}) @config = config self end
Public Instance Methods
connect(method, uri, post_data=nil)
click to toggle source
# File lib/restfull_oauth.rb, line 34 def connect(method, uri, post_data=nil) params = params(@config['consumer_key'], @config['token'] ) signature_base_string = signature_base_string(method, uri.to_s, params) signing_key = @config['consumer_secret'] + '&' + @config['token_secret'] params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string)) header_string = create_header(params) json_response = request_data(header_string, uri, method, post_data) end
Private Instance Methods
create_header(params)
click to toggle source
# File lib/restfull_oauth.rb, line 74 def create_header(params) header = "OAuth " params.each do |k, v| header += "#{k}=\"#{v}\"," end header.slice(0..-2) end
generate_nonce(size=6)
click to toggle source
# File lib/restfull_oauth.rb, line 45 def generate_nonce(size=6) Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '') end
params(consumer_key, token)
click to toggle source
# File lib/restfull_oauth.rb, line 53 def params(consumer_key, token) params = {'oauth_consumer_key' => consumer_key, 'oauth_nonce' => generate_nonce, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => token, 'oauth_version' => '1.0', 'oauth_timestamp' => Time.now.to_i.to_s } end
request_data(header, uri, method, post_data=nil)
click to toggle source
# File lib/restfull_oauth.rb, line 82 def request_data(header, uri, method, post_data=nil) uri = URI(uri) http = Net::HTTP.new(uri.host, uri.port) if (uri.port == 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end if method == 'POST' resp, data = http.post(uri.path, post_data, { 'Authorization' => header, 'Content-Type' => 'application/json; charset=utf-8' }) elsif method == 'PUT' request = Net::HTTP::Put.new(uri.path) headers = { 'Authorization' => header, 'Content-Type' => 'application/json' } headers.keys.each do |key| request[key] = headers[key] end request.body = post_data resp, data = http.request(request) else resp, data = http.get(uri.path, { 'Authorization' => header, 'Content-Type' => 'application/json; charset=utf-8' }) end resp end
sign(key, base_string)
click to toggle source
# File lib/restfull_oauth.rb, line 68 def sign(key, base_string) digest = OpenSSL::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, key, base_string) Base64.encode64(hmac).chomp.gsub(/\n/, '') end
signature_base_string(method, uri, params)
click to toggle source
# File lib/restfull_oauth.rb, line 63 def signature_base_string(method, uri, params) encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}") encoded = method + '&' + url_encode(uri) + '&' + encoded_params end
url_encode(string)
click to toggle source
# File lib/restfull_oauth.rb, line 49 def url_encode(string) CGI::escape(string) end