Class: Etsy::Request

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

Overview

Request

A basic wrapper around GET requests to the Etsy JSON API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_path, parameters = {}) ⇒ Request

Create a new request for the resource with optional parameters



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/etsy/request.rb', line 27

def initialize(resource_path, parameters = {})
  parameters = parameters.dup
  @token = parameters.delete(:access_token) || Etsy.credentials[:access_token]
  @secret = parameters.delete(:access_secret) || Etsy.credentials[:access_secret]
  raise("Secure connection required. Please provide your OAuth credentials via :access_token and :access_secret in the parameters") if parameters.delete(:require_secure) && !secure?
  @multipart_request = parameters.delete(:multipart)
  @resource_path = resource_path
  @resources = parameters.delete(:includes)
  if @resources.class == String
    @resources = @resources.split(',').map {|r| {:resource => r}}
  elsif @resources.class == Array
    @resources = @resources.map do |r|
      if r.class == String
        {:resource => r}
      else
        r
      end
    end
  end
  parameters = parameters.merge(:api_key => Etsy.api_key) unless secure?
  @parameters = parameters
end

Class Method Details

.get(resource_path, parameters = {}) ⇒ Object

Perform a GET request for the resource with optional parameters - returns A Response object with the payload data



11
12
13
14
# File 'lib/etsy/request.rb', line 11

def self.get(resource_path, parameters = {})
  request = Request.new(resource_path, parameters)
  Response.new(request.get)
end

.post(resource_path, parameters = {}) ⇒ Object



16
17
18
19
# File 'lib/etsy/request.rb', line 16

def self.post(resource_path, parameters = {})
  request = Request.new(resource_path, parameters)
  Response.new(request.post)
end

.put(resource_path, parameters = {}) ⇒ Object



21
22
23
24
# File 'lib/etsy/request.rb', line 21

def self.put(resource_path, parameters = {})
  request = Request.new(resource_path, parameters)
  Response.new(request.put)
end

Instance Method Details

#association(options = {}) ⇒ Object

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/etsy/request.rb', line 100

def association(options={}) # :nodoc:
  s = options[:resource].dup

  if options.include? :fields
    s << "(#{[options[:fields]].flatten.join(',')})"
  end

  if options.include?(:limit) || options.include?(:offset)
    s << ":#{options.fetch(:limit, 25)}:#{options.fetch(:offset, 0)}"
  end

  s
end

#base_pathObject

:nodoc:



50
51
52
# File 'lib/etsy/request.rb', line 50

def base_path # :nodoc:
  "/v2"
end

#clientObject

:nodoc:



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

def client # :nodoc:
  @client ||= secure? ? secure_client : basic_client
end

#endpoint_url(options = {}) ⇒ Object

:nodoc:



114
115
116
117
118
# File 'lib/etsy/request.rb', line 114

def endpoint_url(options = {}) # :nodoc:
  url = "#{base_path}#{@resource_path}"
  url += "?#{query}" if options.fetch(:include_query, true)
  url
end

#getObject

Perform a GET request against the API endpoint and return the raw response data



56
57
58
# File 'lib/etsy/request.rb', line 56

def get
  client.get(endpoint_url)
end

#multipart?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/etsy/request.rb', line 120

def multipart?
  !!@multipart_request
end

#parametersObject

:nodoc:



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

def parameters # :nodoc:
  @parameters
end

#postObject



60
61
62
63
64
65
66
# File 'lib/etsy/request.rb', line 60

def post
  if multipart?
    client.post_multipart(endpoint_url(:include_query => false), @parameters)
  else
    client.post(endpoint_url)
  end
end

#putObject



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

def put
  client.put(endpoint_url)
end

#queryObject

:nodoc:



84
85
86
# File 'lib/etsy/request.rb', line 84

def query # :nodoc:
  to_url(parameters.merge(:includes => resources.to_a.map { |r| association(r) }))
end

#resourcesObject

:nodoc:



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

def resources # :nodoc:
  @resources
end

#to_url(val) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/etsy/request.rb', line 88

def to_url(val)
  if val.is_a? Array
    to_url(val.join(','))
  elsif val.is_a? Hash
    val.reject { |k, v|
      k.nil? || v.nil? || (k.respond_to?(:empty?) && k.empty?) || (v.respond_to?(:empty?) && v.empty?)
    }.map { |k, v| "#{to_url(k.to_s)}=#{to_url(v)}" }.join('&')
  else
    URI.escape(val.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  end
end