Class: Blurb::Request

Inherits:
BaseClass show all
Defined in:
lib/blurb/request.rb

Constant Summary

Constants inherited from BaseClass

BaseClass::CAMPAIGN_TYPE_CODES

Instance Method Summary collapse

Constructor Details

#initialize(url:, request_type:, payload: nil, headers:, url_params: nil) ⇒ Request

Returns a new instance of Request.



11
12
13
14
15
16
# File 'lib/blurb/request.rb', line 11

def initialize(url:, request_type:, payload: nil, headers:, url_params: nil)
  @url = setup_url(url, url_params)
  @payload = convert_payload(payload)
  @headers = headers
  @request_type = request_type
end

Instance Method Details

#make_requestObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/blurb/request.rb', line 38

def make_request
  begin
    resp = RestClient::Request.execute(request_config())
    log("response", resp)
  rescue RestClient::TooManyRequests => err
    raise RequestThrottled.new(JSON.parse(err.response.body))
  rescue RestClient::TemporaryRedirect => err
    return RestClient.get(err.response.headers[:location])  # If this happens, then we are downloading a report from the api, so we can simply download the location
  rescue RestClient::NotAcceptable => err
    if @url.include?("report")
      raise InvalidReportRequest.new(JSON.parse(err.response.body))
    else
      raise err
    end
  rescue RestClient::ExceptionWithResponse => err
    if err.response.present?
      raise FailedRequest.new(JSON.parse(err.response.body))
    else 
      raise err
    end
  end
  resp = convert_response(resp)
  return resp
end

#request_configObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blurb/request.rb', line 18

def request_config
  request_config = {
    method: @request_type,
    url: @url,
    headers: @headers
  }

  case @request_type
  when :get
    request_config[:max_redirects] = 0
  when :post, :put
    request_config[:payload] = @payload if @payload
  end
  log("request type", @request_type)
  log("request url", @url)
  log("headers", @headers)
  log("payload", @payload) if @payload
  return request_config
end