Class: Etsy::Request
- Inherits:
-
Object
- Object
- Etsy::Request
- Defined in:
- lib/etsy/request.rb
Overview
Request
A basic wrapper around GET requests to the Etsy JSON API
Class Method Summary collapse
- .delete(resource_path, parameters = {}) ⇒ Object
-
.get(resource_path, parameters = {}) ⇒ Object
Perform a GET request for the resource with optional parameters - returns A Response object with the payload data.
- .post(resource_path, parameters = {}) ⇒ Object
- .put(resource_path, parameters = {}) ⇒ Object
Instance Method Summary collapse
-
#association(options = {}) ⇒ Object
:nodoc:.
-
#base_path ⇒ Object
:nodoc:.
-
#client ⇒ Object
:nodoc:.
- #delete ⇒ Object
-
#endpoint_url(options = {}) ⇒ Object
:nodoc:.
-
#get ⇒ Object
Perform a GET request against the API endpoint and return the raw response data.
-
#initialize(resource_path, parameters = {}) ⇒ Request
constructor
Create a new request for the resource with optional parameters.
- #multipart? ⇒ Boolean
-
#parameters ⇒ Object
:nodoc:.
- #post ⇒ Object
- #put ⇒ Object
-
#query ⇒ Object
:nodoc:.
-
#resources ⇒ Object
:nodoc:.
- #to_url(val) ⇒ Object
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 |
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(={}) # :nodoc: s = [:resource].dup if .include? :fields s << "(#{[[:fields]].flatten.join(',')})" end if .include?(:limit) || .include?(:offset) s << ":#{.fetch(:limit, 25)}:#{.fetch(:offset, 0)}" end s end |
#base_path ⇒ Object
:nodoc:
55 56 57 |
# File 'lib/etsy/request.rb', line 55 def base_path # :nodoc: "/v2" end |
#client ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/etsy/request.rb', line 81 def client # :nodoc: @client ||= secure? ? secure_client : basic_client end |
#delete ⇒ Object
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( = {}) # :nodoc: url = "#{base_path}#{@resource_path}" url += "?#{query}" if .fetch(:include_query, true) url end |
#get ⇒ Object
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
129 130 131 |
# File 'lib/etsy/request.rb', line 129 def multipart? !!@multipart_request end |
#parameters ⇒ Object
:nodoc:
85 86 87 |
# File 'lib/etsy/request.rb', line 85 def parameters # :nodoc: @parameters end |
#post ⇒ Object
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 |
#put ⇒ Object
73 74 75 |
# File 'lib/etsy/request.rb', line 73 def put client.put(endpoint_url) end |
#query ⇒ Object
: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 |
#resources ⇒ Object
: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 |