Class: FlickRaw::OAuth
- Inherits:
-
Object
- Object
- FlickRaw::OAuth
- Defined in:
- lib/flickraw.rb
Defined Under Namespace
Classes: FailedResponse
Instance Attribute Summary collapse
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #access_token(url, token_secret, oauth_params = {}) ⇒ Object
- #authorization_header(url, params) ⇒ Object
- #authorize_url(url, oauth_params = {}) ⇒ Object
- #gen_default_params ⇒ Object
- #gen_nonce ⇒ Object
- #gen_timestamp ⇒ Object
-
#initialize(consumer_key, consumer_secret) ⇒ OAuth
constructor
A new instance of OAuth.
- #post_form(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
- #post_multipart(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
- #request_token(url, oauth_params = {}) ⇒ Object
- #sign(method, url, params, token_secret = nil, consumer_secret = @consumer_secret) ⇒ Object
Constructor Details
#initialize(consumer_key, consumer_secret) ⇒ OAuth
Returns a new instance of OAuth.
65 |
# File 'lib/flickraw.rb', line 65 def initialize(consumer_key, consumer_secret); @consumer_key, @consumer_secret = consumer_key, consumer_secret end |
Instance Attribute Details
#proxy ⇒ Object
Returns the value of attribute proxy.
57 58 59 |
# File 'lib/flickraw.rb', line 57 def proxy @proxy end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
56 57 58 |
# File 'lib/flickraw.rb', line 56 def user_agent @user_agent end |
Class Method Details
.escape(v) ⇒ Object
52 |
# File 'lib/flickraw.rb', line 52 def escape(v); URI.escape(v.to_s, /[^a-zA-Z0-9\-\.\_\~]/) end |
.parse_response(text) ⇒ Object
53 |
# File 'lib/flickraw.rb', line 53 def parse_response(text); Hash[text.split("&").map {|s| s.split("=")}] end |
Instance Method Details
#access_token(url, token_secret, oauth_params = {}) ⇒ Object
100 101 102 103 |
# File 'lib/flickraw.rb', line 100 def access_token(url, token_secret, oauth_params = {}) r = post_form(url, token_secret, oauth_params) OAuth.parse_response(r.body) end |
#authorization_header(url, params) ⇒ Object
75 76 77 78 |
# File 'lib/flickraw.rb', line 75 def (url, params) params_norm = params.map {|k,v| OAuth.escape(k) + "=\"" + OAuth.escape(v) + "\""}.sort.join(", ") "OAuth realm=\"" + url.to_s + "\", " + params_norm end |
#authorize_url(url, oauth_params = {}) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/flickraw.rb', line 93 def (url, oauth_params = {}) params_norm = oauth_params.map {|k,v| OAuth.escape(k) + "=" + OAuth.escape(v)}.sort.join("&") url = URI.parse(url) url.query = url.query ? url.query + "&" + params_norm : params_norm url end |
#gen_default_params ⇒ Object
82 83 84 85 86 |
# File 'lib/flickraw.rb', line 82 def gen_default_params { :oauth_version => "1.0", :oauth_signature_method => "HMAC-SHA1", :oauth_consumer_key => @consumer_key, :oauth_nonce => gen_nonce, :oauth_timestamp => } end |
#gen_nonce ⇒ Object
81 |
# File 'lib/flickraw.rb', line 81 def gen_nonce; [OpenSSL::Random.random_bytes(32)].pack('m0').gsub(/\n$/,'') end |
#gen_timestamp ⇒ Object
80 |
# File 'lib/flickraw.rb', line 80 def ; Time.now.to_i end |
#post_form(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/flickraw.rb', line 105 def post_form(url, token_secret, oauth_params = {}, params = {}) oauth_params = gen_default_params.merge(oauth_params) oauth_params[:oauth_signature] = sign(:post, url, params.merge(oauth_params), token_secret) url = URI.parse(url) r = Net::HTTP.start(url.host, url.port, @proxy_host, @proxy_port, @proxy_user, @proxy_password) { |http| request = Net::HTTP::Post.new(url.path) request['User-Agent'] = @user_agent if @user_agent request['Authorization'] = (url, oauth_params) request.form_data = params http.request(request) } raise FailedResponse.new(r.body) if r.is_a? Net::HTTPClientError r end |
#post_multipart(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/flickraw.rb', line 121 def post_multipart(url, token_secret, oauth_params = {}, params = {}) oauth_params = gen_default_params.merge(oauth_params) params_signed = params.reject {|k,v| v.is_a? File}.merge(oauth_params) oauth_params[:oauth_signature] = sign(:post, url, params_signed, token_secret) url = URI.parse(url) r = Net::HTTP.start(url.host, url.port, @proxy_host, @proxy_port, @proxy_user, @proxy_password) { |http| boundary = "FlickRaw#{gen_nonce}" request = Net::HTTP::Post.new(url.path) request['User-Agent'] = @user_agent if @user_agent request['Content-type'] = "multipart/form-data, boundary=#{boundary}" request['Authorization'] = (url, oauth_params) request.body = '' params.each { |k, v| if v.is_a? File basename = File.basename(v.path).to_s basename = basename.encode("utf-8").force_encoding("ascii-8bit") if RUBY_VERSION >= "1.9" filename = basename request.body << "--#{boundary}\r\n" << "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" << "Content-Transfer-Encoding: binary\r\n" << "Content-Type: image/jpeg\r\n\r\n" << v.read << "\r\n" else request.body << "--#{boundary}\r\n" << "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n" << "#{v}\r\n" end } request.body << "--#{boundary}--" http.request(request) } raise FailedResponse.new(r.body) if r.is_a? Net::HTTPClientError r end |
#request_token(url, oauth_params = {}) ⇒ Object
88 89 90 91 |
# File 'lib/flickraw.rb', line 88 def request_token(url, oauth_params = {}) r = post_form(url, nil, {:oauth_callback => "oob"}.merge(oauth_params)) OAuth.parse_response(r.body) end |
#sign(method, url, params, token_secret = nil, consumer_secret = @consumer_secret) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/flickraw.rb', line 67 def sign(method, url, params, token_secret = nil, consumer_secret = @consumer_secret) params_norm = params.map {|k,v| OAuth.escape(k) + "=" + OAuth.escape(v) }.sort.join("&") text = method.to_s.upcase + "&" + OAuth.escape(url) + "&" + OAuth.escape(params_norm) key = consumer_secret.to_s + "&" + token_secret.to_s digest = OpenSSL::Digest::Digest.new("sha1") [OpenSSL::HMAC.digest(digest, key, text)].pack('m0').gsub(/\n$/,'') end |