Class: BaselineRequest

Inherits:
Object
  • Object
show all
Includes:
BaselineEndpoints
Defined in:
lib/baseline_request.rb

Constant Summary

Constants included from BaselineEndpoints

BaselineEndpoints::BASE_URL, BaselineEndpoints::ENDPOINTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ BaselineRequest

Returns a new instance of BaselineRequest.



5
6
7
8
9
10
# File 'lib/baseline_request.rb', line 5

def initialize(endpoint, options = {})
  raise "#{endpoint} is not a valid endpoint" unless ENDPOINTS[endpoint]
  @endpoint = endpoint
  @api_info = ENDPOINTS[@endpoint]
  @options = options.with_indifferent_access
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



4
5
6
# File 'lib/baseline_request.rb', line 4

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/baseline_request.rb', line 4

def options
  @options
end

#parametersObject

Returns the value of attribute parameters.



4
5
6
# File 'lib/baseline_request.rb', line 4

def parameters
  @parameters
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/baseline_request.rb', line 4

def uri
  @uri
end

Instance Method Details

#call_apiObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/baseline_request.rb', line 21

def call_api
  path, error_array = construct_path
  query = @options.slice(*@api_info[:query_params])
  @api_info[:required_params].each do |req_group|
    error_array << "Missing one of the following required parameters: #{req_group.to_s}" unless req_group.any? {query.keys.include? _1 }
  end

  return error_array unless error_array.empty?
  HTTParty.get(path, {
    :query => query,
    :query_string_normalizer => -> (h) {
      h.map do |key, value|
        if value.is_a? Array
          query_string = value.map {|v| "#{key}=#{v}"}.join('&')
        else
          query_string = "#{key}=#{value}"
        end
        query_string
      end.join('&')
    }
  })
end

#construct_pathObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/baseline_request.rb', line 44

def construct_path
  error_array = []
  url = @api_info[:url].dup
  @api_info[:path_params].each do |path_key, value|
    case value[:type]
    when "bool"
      path_value = value[@options[path_key].to_s] || value[value[:default].to_s]
    else
      path_value = @options[path_key] || value[:default]
    end
    error_array << "Missing required path parameter: #{path_key}" if value[:required] && path_value.blank?
    path_value = path_value.to_s
    path_value = "/" + path_value if path_value.present? && value[:leading_slash]
    url.sub! "{#{path_key}}", path_value
  end

  [url, error_array]
end

#infoObject



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

def info
  @api_info
end

#paramsObject



63
64
65
# File 'lib/baseline_request.rb', line 63

def params
  @api_info[:query_params] + @api_info[:path_params].keys
end

#set_option(key, value, overwrite = true) ⇒ Object



67
68
69
70
71
# File 'lib/baseline_request.rb', line 67

def set_option(key, value, overwrite = true)
  raise "#{key} not a valid parameter for #{@endpoint} endpoint" unless params.include? key
  @options[key] = overwrite ? value : @options[key] || value
  self
end

#testObject



12
13
14
15
# File 'lib/baseline_request.rb', line 12

def test
  @options = @api_info[:test_params]
  call_api
end