Class: BellPepper::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bell_pepper/request.rb', line 13

def initialize(**args)
  @endpoint = args[:endpoint]
  @verbose = args[:verbose]
  
  @id = args[:id]
  
  @city = args[:city]
  @continent = args[:continent]
  @county = args[:county]
  @country = args[:country]
  @country_code = args[:country_code]
  @give_me = args[:give_me]
  @locality = args[:locality]
  @state_province = args[:state_province]
  
  @options = args[:options] # TODO: not added at bell_pepper.rb
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/bell_pepper/request.rb', line 8

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/bell_pepper/request.rb', line 11

def options
  @options
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/bell_pepper/request.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#performObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bell_pepper/request.rb', line 31

def perform

  for_location = {
    id: @id, 
    city: @city, 
    continent: @continent,
    county: @county,
    country: @country, 
    countrycode: @country_code, 
    locality: @locality, 
    stateprovince: @state_province
    }
  for_location = for_location.delete_if { |_k, v| v.nil? }
  body = {"give_me": @give_me, "for_location": for_location}

  conn = if verbose
           Faraday.new(url: BellPepper.base_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |f|
             f.response :logger
             f.request :json
             f.use Faraday::BellPepperErrors::Middleware
             f.adapter Faraday.default_adapter
           end
         else
           Faraday.new(url: BellPepper.base_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |f|
             f.use Faraday::BellPepperErrors::Middleware
             f.request :json
             f.adapter Faraday.default_adapter
           end
         end

  conn.headers['Authorization'] = "Bearer #{@token}" unless @token.nil?
  conn.headers['Accept'] = 'application/json,*/*'
  conn.headers[:user_agent] = make_user_agent
  conn.headers["X-USER-AGENT"] = make_user_agent

  res = conn.post endpoint, body.to_json

  # Handles endpoints that do not return JSON
  begin
    MultiJson.load(res.body)
  rescue MultiJson::ParseError
    res.body
  end
  
end