Class: SpreedlyCore::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/spreedly_core/base.rb

Overview

Base class for all SpreedlyCore API requests

Direct Known Subclasses

Gateway, PaymentMethod, Response, Transaction

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Given a hash of attrs, assign instance variables using the hash key as the attribute name and hash value as the attribute value



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spreedly_core/base.rb', line 79

def initialize(attrs={})
  attrs.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
  # errors may be nil, empty, a string, or an array of strings. 
  @errors = if @errors.nil? || @errors["error"].blank?
              []
            elsif @errors["error"].is_a?(String)
              [@errors["error"]]
            else
              @errors["error"]
            end
end

Class Method Details

.configure(login, secret, gateway_token) ⇒ Object



17
18
19
20
21
# File 'lib/spreedly_core/base.rb', line 17

def self.configure(, secret, gateway_token)
  @@login = 
  self.basic_auth(@@login, secret)
  @@gateway_token = gateway_token
end

.gateway_tokenObject



24
# File 'lib/spreedly_core/base.rb', line 24

def self.gateway_token; @@gateway_token; end

.loginObject



23
# File 'lib/spreedly_core/base.rb', line 23

def self.; @@login; end

.verify_get(path, options = {}, &block) ⇒ Object

make a get request to path If the request succeeds, provide the respones to the &block



40
41
42
# File 'lib/spreedly_core/base.rb', line 40

def self.verify_get(path, options={}, &block)
  verify_request(:get, path, options, 200, &block)
end

.verify_options(path, options = {}, &block) ⇒ Object

make an options request to path If the request succeeds, provide the respones to the &block



46
47
48
# File 'lib/spreedly_core/base.rb', line 46

def self.verify_options(path, options={}, &block)
  verify_request(:options, path, options, 200, &block)
end

.verify_post(path, options = {}, &block) ⇒ Object

make a post request to path If the request succeeds, provide the respones to the &block



28
29
30
# File 'lib/spreedly_core/base.rb', line 28

def self.verify_post(path, options={}, &block)
  verify_request(:post, path, options, 200, 422, 201, &block)
end

.verify_put(path, options = {}, &block) ⇒ Object

make a put request to path If the request succeeds, provide the respones to the &block



34
35
36
# File 'lib/spreedly_core/base.rb', line 34

def self.verify_put(path, options={}, &block)
  verify_request(:put, path, options,  &block)
end

.verify_request(request_type, path, options, *allowed_codes, &block) ⇒ Object

make a request to path using the HTTP method provided as request_type *allowed_codes are passed in, verify the response code (200, 404, etc) is one of the allowed codes. If *allowed_codes is empty, don’t check the response code, but set an instance variable on the object created in the block containing the response code.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/spreedly_core/base.rb', line 55

def self.verify_request(request_type, path, options, *allowed_codes, &block)
  begin 
    response = self.send(request_type, path, options)
  rescue Timeout::Error, Errno::ETIMEDOUT => e
    raise TimeOutError.new("Request to #{path} timed out. Is Spreedly Core down?")
  end
    
  if allowed_codes.any? && !allowed_codes.include?(response.code)
    raise InvalidResponse.new(response, "Error retrieving #{path}. Got status of #{response.code}. Expected status to be in #{allowed_codes.join(",")}")
  end

  if options.has_key?(:has_key) &&
      (response.parsed_response.nil? || !response.parsed_response.has_key?(options[:has_key]))
    raise InvalidResponse.new(response, "Expected parsed response to contain key '#{options[:has_key]}'")
  end

  block.call(response).tap do |obj|
    obj.instance_variable_set("@http_code", response.code)
  end
end