Class: Hamburglar::Gateways::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hamburglar/gateways/base.rb

Overview

Hamburglar::Gateways::Base is the main class that handles sending API requests to upstream providers. All other gateways should inherit from this class

Direct Known Subclasses

MaxMind::Base

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base



21
22
23
24
25
# File 'lib/hamburglar/gateways/base.rb', line 21

def initialize(params = {})
  @params   = Hash[Hamburglar.config.credentials].merge(params)
  @errors   = {}
  @response = {}
end

Class Attribute Details

.api_urlObject (readonly)

The API URL



18
19
20
# File 'lib/hamburglar/gateways/base.rb', line 18

def api_url
  @api_url
end

Instance Attribute Details

#errorsObject (readonly)

Errors returned when validating or submitting a request



11
12
13
# File 'lib/hamburglar/gateways/base.rb', line 11

def errors
  @errors
end

#paramsObject (readonly)

The parameters for the API request



8
9
10
# File 'lib/hamburglar/gateways/base.rb', line 8

def params
  @params
end

#responseObject (readonly)

Response returned by an API call



14
15
16
# File 'lib/hamburglar/gateways/base.rb', line 14

def response
  @response
end

Class Method Details

.required_paramsObject

Required parameters for an API call



33
34
35
# File 'lib/hamburglar/gateways/base.rb', line 33

def self.required_params
  @required_params || []
end

.set_required_params(*params) ⇒ Object

Set required parameters for an API call



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

def self.set_required_params(*params)
  @required_params = params
end

Instance Method Details

#submitObject

Subclasses need to implement this.

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/hamburglar/gateways/base.rb', line 63

def submit
  raise NotImplementedError
end

#validate(revalidate = false) ⇒ Object Also known as: valid?

Validate presence of required_params

Returns false if a parameter isn't set



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hamburglar/gateways/base.rb', line 40

def validate(revalidate = false)
  @validated = false if revalidate
  unless @validated
    @errors[:missing_parameters] = []
    self.class.required_params.each do |req|
      unless @params.has_key?(req)
        @errors[:missing_parameters] << req
      end
    end
    @validated = true
  end
  @errors[:missing_parameters].empty?
end

#validate!Object

Validate presence of required_params

Raises Hamburglar::InvalidRequest if validation fails



58
59
60
# File 'lib/hamburglar/gateways/base.rb', line 58

def validate!
  validate || raise(Hamburglar::InvalidRequest)
end