Class: Dadatas::ApiCall

Inherits:
Object
  • Object
show all
Defined in:
lib/dadatas/api_call.rb

Overview

Call API DADATA

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, config, params) ⇒ ApiCall

Returns a new instance of ApiCall.



11
12
13
14
15
# File 'lib/dadatas/api_call.rb', line 11

def initialize(url, config, params)
  @url = url
  @config = config
  @params = params
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/dadatas/api_call.rb', line 9

def config
  @config
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/dadatas/api_call.rb', line 9

def params
  @params
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/dadatas/api_call.rb', line 9

def url
  @url
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
# File 'lib/dadatas/api_call.rb', line 17

def call
  response = perform_request(url, params)

  return response_handler(response) if response.status.eql?(200)

  error_response(response, response.body)
end

#error_response(response, response_body) ⇒ Object



41
42
43
# File 'lib/dadatas/api_call.rb', line 41

def error_response(response, response_body)
  [false, { code: response.status, message: response_body }]
end

#headerObject



58
59
60
61
62
63
64
65
# File 'lib/dadatas/api_call.rb', line 58

def header
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => "Token #{@config[:api_key]}",
    'X-Secret' => @config[:secret_key].to_s
  }
end

#location_condition?(response_body) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/dadatas/api_call.rb', line 37

def location_condition?(response_body)
  response_body.key?(:location) && !response_body[:location].nil?
end

#perform_request(url, params) ⇒ Object



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

def perform_request(url, params)
  Faraday.post(url, params.to_json, header)
rescue StandardError => e
  e
end

#response_handler(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dadatas/api_call.rb', line 25

def response_handler(response)
  response_body = JSON.parse(response.body, symbolize_names: true)

  return success(response_body) if response_body.is_a?(Array)

  return success(response_body[:suggestions]) if response_body.key?(:suggestions)

  return special_success(response_body[:location]) if location_condition?(response_body)

  error_response(response, response_body)
end

#special_success(response) ⇒ Object



54
55
56
# File 'lib/dadatas/api_call.rb', line 54

def special_success(response)
  [response.any?, response]
end

#success(response) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/dadatas/api_call.rb', line 45

def success(response)
  new_response = response.map do |result|
    result[:data].merge!(req_value: result[:value]) if result.key?(:data)
    result
  end

  [response.any?, new_response]
end