Class: SocialRebate::Connection
- Inherits:
-
Object
- Object
- SocialRebate::Connection
- Includes:
- HTTParty
- Defined in:
- lib/social_rebate/connection.rb
Defined Under Namespace
Classes: ResponseError
Constant Summary collapse
- STATUS =
['UNVERIFIED', 'VERIFIED', 'VOID', 'COUPON']
Class Method Summary collapse
Instance Method Summary collapse
- #api_token_keys_valid? ⇒ Boolean
- #check_body_params(body) ⇒ Object
- #check_post_url(url) ⇒ Object
- #check_put_url(url) ⇒ Object
- #get(url, params = {}) ⇒ Object
-
#initialize(creds = {}) ⇒ Connection
constructor
A new instance of Connection.
- #parsed_response(response) ⇒ Object
- #post(url, body = {}, headers = {}) ⇒ Object
- #put(url, body = {}, headers = {}) ⇒ Object
- #request(method, url, options = {}) ⇒ Object
- #serialize_param(params) ⇒ Object
- #set_headers(headers) ⇒ Object
- #validate_creds(creds) ⇒ Object
Constructor Details
#initialize(creds = {}) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/social_rebate/connection.rb', line 11 def initialize(creds={}) validate_creds(creds) @configuration = {} SocialRebate::Connection.api_token_keys.each do |key| @configuration[key] = creds[key].to_s end @store_key = creds[:store_key] @configuration[:format] = 'json' end |
Class Method Details
.api_required_keys ⇒ Object
26 27 28 |
# File 'lib/social_rebate/connection.rb', line 26 def self.api_required_keys [:api_key, :api_secret, :store_key].freeze end |
.api_token_keys ⇒ Object
22 23 24 |
# File 'lib/social_rebate/connection.rb', line 22 def self.api_token_keys [:api_key, :api_secret].freeze end |
Instance Method Details
#api_token_keys_valid? ⇒ Boolean
51 52 53 |
# File 'lib/social_rebate/connection.rb', line 51 def api_token_keys_valid? return SocialRebate::Connection.api_token_keys.detect {|key| @configuration[key] == ''} == nil end |
#check_body_params(body) ⇒ Object
96 97 98 99 100 |
# File 'lib/social_rebate/connection.rb', line 96 def check_body_params(body) if !body.key?(:status) || !SocialRebate::Connection::STATUS.include?(body[:status]) raise ResponseError.new("Missing or incorrect status param") end end |
#check_post_url(url) ⇒ Object
108 109 110 111 112 |
# File 'lib/social_rebate/connection.rb', line 108 def check_post_url(url) if url !~ /\/v[0-9]+\/orders\/$/i raise ResponseError.new("Incorrect post request url, expected: /api/v[0-9]/orders/") end end |
#check_put_url(url) ⇒ Object
102 103 104 105 106 |
# File 'lib/social_rebate/connection.rb', line 102 def check_put_url(url) if url !~ /\/v[0-9]+\/orders\/[0-9a-zA-Z]+\/$/i raise ResponseError.new("Incorrect put request url, expected: /api/v[0-9]/orders/<your order id>/") end end |
#get(url, params = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/social_rebate/connection.rb', line 59 def get(url, params={}) url = "#{url}?#{serialize_param(@configuration)}" unless params.empty? url = "#{url}&#{serialize_param(params)}" end request(:get, url) end |
#parsed_response(response) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/social_rebate/connection.rb', line 44 def parsed_response(response) unless response.success? raise ResponseError.new(response) end response.parsed_response end |
#post(url, body = {}, headers = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/social_rebate/connection.rb', line 67 def post(url, body={}, headers={}) check_post_url(url) body.merge!(@configuration).merge!({:store_key => @store_key}) = {} [:body] = body [:headers] = set_headers(headers) request(:post, url, ) end |
#put(url, body = {}, headers = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/social_rebate/connection.rb', line 77 def put(url, body={}, headers={}) check_body_params(body) check_put_url(url) body = body.merge!(@configuration).merge!({:store_key => @store_key}) = {} [:body] = body [:headers] = set_headers(headers) request(:put, url, ) end |
#request(method, url, options = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/social_rebate/connection.rb', line 36 def request(method, url, ={}) unless api_token_keys_valid? raise ResponseError.new("Please set api_key and api_secret correctly") end [:body] = [:body].to_json if [:body] parsed_response(self.class.__send__(method, url, )) end |
#serialize_param(params) ⇒ Object
55 56 57 |
# File 'lib/social_rebate/connection.rb', line 55 def serialize_param(params) params.sort.map {|key, value| URI.escape("#{key}=#{value}")}.join('&') end |
#set_headers(headers) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/social_rebate/connection.rb', line 88 def set_headers(headers) hash = {} hash = headers unless headers.empty? hash['content-type'] = "application/json" hash['Accept'] = "application/json" hash end |
#validate_creds(creds) ⇒ Object
30 31 32 33 34 |
# File 'lib/social_rebate/connection.rb', line 30 def validate_creds(creds) SocialRebate::Connection.api_required_keys.each do |key| raise ResponseError.new("Required key: #{key} missing") unless creds[key] end end |