Class: JetEmail::Request

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

Constant Summary collapse

BASE_URL =
ENV["JETEMAIL_BASE_URL"] || "https://api.jetemail.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "", body = {}, verb = "post") ⇒ Request

Returns a new instance of Request.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jetemail/request.rb', line 9

def initialize(path = "", body = {}, verb = "post")
  raise JetEmail::Error.new("API key is not set") if JetEmail.api_key.nil?

  api_key = JetEmail.api_key
  api_key = api_key.call if api_key.is_a?(Proc)

  @path = path
  @body = body
  @verb = verb
  @headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json",
    "User-Agent" => "jetemail-ruby:#{JetEmail::VERSION}",
    "Authorization" => "Bearer #{api_key}"
  }
end

Instance Attribute Details

#body ⇒ Object

Returns the value of attribute body.



7
8
9
# File 'lib/jetemail/request.rb', line 7

def body
  @body
end

#verb ⇒ Object

Returns the value of attribute verb.



7
8
9
# File 'lib/jetemail/request.rb', line 7

def verb
  @verb
end

Instance Method Details

#perform ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/jetemail/request.rb', line 26

def perform
  options = build_options
  resp = HTTParty.send(@verb.to_sym, "#{BASE_URL}/#{@path}", options)

  data = parse_response(resp)
  handle_error!(data, resp) if error_response?(resp)
  data
end