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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/etsy/request.rb', line 32

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

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



26
27
28
29
# File 'lib/etsy/request.rb', line 26

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

.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:



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/etsy/request.rb', line 109

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:



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

def base_path # :nodoc:
  "/v2"
end

#clientObject

:nodoc:



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

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

#deleteObject



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

def delete
  client.delete(endpoint_url)
end

#endpoint_url(options = {}) ⇒ Object

:nodoc:



123
124
125
126
127
# File 'lib/etsy/request.rb', line 123

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



61
62
63
# File 'lib/etsy/request.rb', line 61

def get
  client.get(endpoint_url)
end

#multipart?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/etsy/request.rb', line 129

def multipart?
  !!@multipart_request
end

#parametersObject

:nodoc:



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

def parameters # :nodoc:
  @parameters
end

#postObject



65
66
67
68
69
70
71
# File 'lib/etsy/request.rb', line 65

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

#putObject



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

def put
  client.put(endpoint_url)
end

#queryObject

:nodoc:



93
94
95
# File 'lib/etsy/request.rb', line 93

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

#resourcesObject

:nodoc:



89
90
91
# File 'lib/etsy/request.rb', line 89

def resources # :nodoc:
  @resources
end

#to_url(val) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/etsy/request.rb', line 97

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