Class: Etsy::SecureClient

Inherits:
Object
  • Object
show all
Defined in:
lib/etsy/secure_client.rb

Overview

SecureClient

Used for generating tokens and calling API methods that require authentication.

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ SecureClient

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.



20
21
22
# File 'lib/etsy/secure_client.rb', line 20

def initialize(attributes = {})
  @attributes = attributes
end

Instance Method Details

#access_secretObject

Access secret for this request, either generated from the request token or taken from the :access_secret parameter.



49
50
51
# File 'lib/etsy/secure_client.rb', line 49

def access_secret
  @attributes[:access_secret] || client.secret
end

#access_tokenObject

Access token for this request, either generated from the request token or taken from the :access_token parameter.



42
43
44
# File 'lib/etsy/secure_client.rb', line 42

def access_token
  @attributes[:access_token] || client.token
end

#clientObject

:nodoc:



62
63
64
# File 'lib/etsy/secure_client.rb', line 62

def client # :nodoc:
  @client ||= has_access_data? ? client_from_access_data : client_from_request_data
end

#client_from_access_dataObject

:nodoc:



58
59
60
# File 'lib/etsy/secure_client.rb', line 58

def client_from_access_data # :nodoc:
  OAuth::AccessToken.new(consumer, @attributes[:access_token], @attributes[:access_secret])
end

#client_from_request_dataObject

:nodoc:



53
54
55
56
# File 'lib/etsy/secure_client.rb', line 53

def client_from_request_data # :nodoc:
  request_token = OAuth::RequestToken.new(consumer, @attributes[:request_token], @attributes[:request_secret])
  request_token.get_access_token(:oauth_verifier => @attributes[:verifier])
end

#consumerObject

:nodoc:



24
25
26
27
28
29
30
31
# File 'lib/etsy/secure_client.rb', line 24

def consumer # :nodoc:
  path = '/v2/oauth/'
  @consumer ||= OAuth::Consumer.new(Etsy.api_key, Etsy.api_secret, {
    :site               => "#{Etsy.protocol}://#{Etsy.host}",
    :request_token_path => "#{path}request_token?scope=#{Etsy.permission_scopes.join('+')}",
    :access_token_path  => "#{path}access_token"
  })
end

#delete(endpoint) ⇒ Object



80
81
82
# File 'lib/etsy/secure_client.rb', line 80

def delete(endpoint)
  client.delete(endpoint)
end

#get(endpoint) ⇒ Object

Fetch a raw response from the specified endpoint.



68
69
70
# File 'lib/etsy/secure_client.rb', line 68

def get(endpoint)
  client.get(endpoint)
end

#post(endpoint) ⇒ Object



72
73
74
# File 'lib/etsy/secure_client.rb', line 72

def post(endpoint)
  client.post(endpoint)
end

#post_multipart(endpoint, params = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



76
77
78
# File 'lib/etsy/secure_client.rb', line 76

def put(endpoint)
  client.put(endpoint)
end

#request_tokenObject

Generate a request token.



35
36
37
# File 'lib/etsy/secure_client.rb', line 35

def request_token
  consumer.get_request_token(:oauth_callback => Etsy.callback_url)
end