Class: SpreedlyCore::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/spreedly-core-ruby/base.rb

Overview

Base class for all Spreedly 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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/spreedly-core-ruby/base.rb', line 85

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"].collect do |error|
      case error
      when Hash
        error["__content__"]
      else
        error
      end
    end
  end
end

Class Method Details

.configure(environment_key, secret, options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/spreedly-core-ruby/base.rb', line 17

def self.configure(environment_key, secret, options = {})
  @@environment_key = environment_key
  self.basic_auth(@@environment_key, secret)
  base_uri options[:endpoint]
  @@gateway_token = options.delete(:gateway_token)
end

.environment_keyObject



24
# File 'lib/spreedly-core-ruby/base.rb', line 24

def self.environment_key; @@environment_key; end

.gateway_tokenObject



25
# File 'lib/spreedly-core-ruby/base.rb', line 25

def self.gateway_token; @@gateway_token; end

.gateway_token=(gateway_token) ⇒ Object



26
# File 'lib/spreedly-core-ruby/base.rb', line 26

def self.gateway_token=(gateway_token); @@gateway_token = gateway_token; end

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

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



42
43
44
# File 'lib/spreedly-core-ruby/base.rb', line 42

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



48
49
50
# File 'lib/spreedly-core-ruby/base.rb', line 48

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



30
31
32
# File 'lib/spreedly-core-ruby/base.rb', line 30

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

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

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



36
37
38
# File 'lib/spreedly-core-ruby/base.rb', line 36

def self.verify_put(path, options={}, &block)
  verify_request(:put, path, options, 200, 422, &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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spreedly-core-ruby/base.rb', line 57

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

  if (response.code == 422 && !response.parsed_response.nil? && response.parsed_response.has_key?("errors"))
    raise UnprocessableRequest.new(response.parsed_response["errors"]["error"])
  end

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