Module: Txbr::RequestMethods

Included in:
BrazeApi
Defined in:
lib/txbr/request_methods.rb

Instance Method Summary collapse

Instance Method Details

#act(verb, *args) ⇒ Object



21
22
23
24
25
# File 'lib/txbr/request_methods.rb', line 21

def act(verb, *args)
  connection.send(verb, *args).tap do |response|
    raise_error!(response)
  end
end

#get(url, params = {}) ⇒ Object



13
14
15
# File 'lib/txbr/request_methods.rb', line 13

def get(url, params = {})
  act(:get, url, params)
end

#get_json(url, params = {}) ⇒ Object



3
4
5
6
# File 'lib/txbr/request_methods.rb', line 3

def get_json(url, params = {})
  response = get(url, params)
  JSON.parse(response.body)
end

#post(url, body) ⇒ Object



17
18
19
# File 'lib/txbr/request_methods.rb', line 17

def post(url, body)
  act(:post, url, body)
end

#post_json(url, body = {}) ⇒ Object



8
9
10
11
# File 'lib/txbr/request_methods.rb', line 8

def post_json(url, body = {})
  response = post(url, body.to_json)
  JSON.parse(response.body)
end

#raise_error!(response) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/txbr/request_methods.rb', line 27

def raise_error!(response)
  case response.status
    when 401
      raise BrazeUnauthorizedError, "401 Unauthorized: #{response.env.url}"
    when 404
      raise BrazeNotFoundError, "404 Not Found: #{response.env.url}"
    else
      if (response.status / 100) != 2
        raise Txbr::BrazeApiError.new(
          "HTTP #{response.status}: #{response.env.url}, body: #{response.body}",
          response.status
        )
      end
  end
end