Class: HanamiEmail::BaseRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami_email.rb

Direct Known Subclasses

Alias

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BaseRequest

Returns a new instance of BaseRequest.



23
24
25
26
27
28
29
# File 'lib/hanami_email.rb', line 23

def initialize(params={})
  @api_key = params.delete(:api_key) || ::HanamiEmail.api_key
  raise ":api_key param or config option is required" unless @api_key
  @domain = params.delete(:domain) || ::HanamiEmail.default_domain
  raise ":domain param or :default_domain config option is required" unless @domain
  @params = params
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



21
22
23
# File 'lib/hanami_email.rb', line 21

def api_key
  @api_key
end

#domainObject (readonly)

Returns the value of attribute domain.



21
22
23
# File 'lib/hanami_email.rb', line 21

def domain
  @domain
end

#paramsObject (readonly)

Returns the value of attribute params.



21
22
23
# File 'lib/hanami_email.rb', line 21

def params
  @params
end

Instance Method Details

#default_paramsObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hanami_email.rb', line 31

def default_params
  {
    timeout: ::HanamiEmail.timeout || 15,
    connecttimeout: ::HanamiEmail.connecttimeout || 15,
    followlocation: true,
    headers: {
      "Content-Type"  => "application/json",
      "apikey" => api_key,
    },
  }
end

#wrap_error(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hanami_email.rb', line 43

def wrap_error(response)
  response.tap do |response|
    if response.timed_out?
      raise TimeoutError, response
    elsif response.code == 0
      raise NoHTTPResponseError, response
    elsif !response.success?
      raise NonSuccessfulHTTPError, response
    end
  end
end