class Etsy::SecureClient
SecureClient
¶ ↑
Used for generating tokens and calling API methods that require authentication.
Public Class Methods
new(attributes = {})
click to toggle source
Create a new client with the necessary parameters. Accepts the following key/value pairs:
:request_token :request_secret :access_token :access_secret
The request token / secret is useful for generating the access token. Pass the access token / secret when initializing from stored credentials.
# File lib/etsy/secure_client.rb, line 20 def initialize(attributes = {}) @attributes = attributes end
Public Instance Methods
access_secret()
click to toggle source
Access secret for this request, either generated from the request token or taken from the :access_secret parameter.
# File lib/etsy/secure_client.rb, line 49 def access_secret @attributes[:access_secret] || client.secret end
access_token()
click to toggle source
Access token for this request, either generated from the request token or taken from the :access_token parameter.
# File lib/etsy/secure_client.rb, line 42 def access_token @attributes[:access_token] || client.token end
delete(endpoint)
click to toggle source
# File lib/etsy/secure_client.rb, line 80 def delete(endpoint) client.delete(endpoint) end
get(endpoint)
click to toggle source
Fetch a raw response from the specified endpoint.
# File lib/etsy/secure_client.rb, line 68 def get(endpoint) client.get(endpoint) end
post(endpoint)
click to toggle source
# File lib/etsy/secure_client.rb, line 72 def post(endpoint) client.post(endpoint) end
post_multipart(endpoint, params = {})
click to toggle source
# File lib/etsy/secure_client.rb, line 84 def post_multipart(endpoint, params = {}) client = Net::HTTP.new(Etsy.host, Etsy.protocol == "http" ? 80 : 443) client.use_ssl = true if Etsy.protocol == "https" client.start do |http| req = Net::HTTP::Post.new(endpoint) add_multipart_data(req, params) add_oauth(req) res = http.request(req) end end
put(endpoint)
click to toggle source
# File lib/etsy/secure_client.rb, line 76 def put(endpoint) client.put(endpoint) end
request_token()
click to toggle source
Generate a request token.
# File lib/etsy/secure_client.rb, line 35 def request_token consumer.get_request_token(:oauth_callback => Etsy.callback_url) end
Private Instance Methods
add_multipart_data(req, params)
click to toggle source
Encodes the request as multipart
# File lib/etsy/secure_client.rb, line 99 def add_multipart_data(req, params) crlf = "\r\n" boundary = Time.now.to_i.to_s(16) req["Content-Type"] = "multipart/form-data; boundary=#{boundary}" body = "" params.each do |key,value| esc_key = CGI.escape(key.to_s) body << "--#{boundary}#{crlf}" if value.respond_to?(:read) body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{crlf}" body << "Content-Type: image/jpeg#{crlf*2}" body << open(value.path, "rb") {|io| io.read} else body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{crlf*2}#{value}" end body << crlf end body << "--#{boundary}--#{crlf*2}" req.body = body req["Content-Length"] = req.body.size end
add_oauth(req)
click to toggle source
Uses the OAuth gem to add the signed Authorization header
# File lib/etsy/secure_client.rb, line 122 def add_oauth(req) client.sign!(req) end
has_access_data?()
click to toggle source
# File lib/etsy/secure_client.rb, line 126 def has_access_data? !@attributes[:access_token].nil? && !@attributes[:access_secret].nil? end