Class: Onlia::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/onlia/client.rb

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
# File 'lib/onlia/client.rb', line 11

def initialize
  self.refresh_token
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



9
10
11
# File 'lib/onlia/client.rb', line 9

def api_token
  @api_token
end

#decoded_tokenObject

Returns the value of attribute decoded_token.



9
10
11
# File 'lib/onlia/client.rb', line 9

def decoded_token
  @decoded_token
end

#raw_tokenObject

Returns the value of attribute raw_token.



9
10
11
# File 'lib/onlia/client.rb', line 9

def raw_token
  @raw_token
end

Instance Method Details

#activate_agreement(params) ⇒ Object



76
77
78
# File 'lib/onlia/client.rb', line 76

def activate_agreement(params)
  post("/Auto/activate", params, @api_token)
end

#auto_lookup(params) ⇒ Object



64
65
66
# File 'lib/onlia/client.rb', line 64

def auto_lookup(params)
  post("/Auto/lookup", params, @api_token)
end

#bind_agreement(params) ⇒ Object



68
69
70
# File 'lib/onlia/client.rb', line 68

def bind_agreement(params)
  post("/Auto/bind", params, @api_token)
end

#get_quote(params) ⇒ Object



60
61
62
# File 'lib/onlia/client.rb', line 60

def get_quote(params)
  post("/Auto/quote", params, @api_token)
end

#get_tokenObject



55
56
57
58
# File 'lib/onlia/client.rb', line 55

def get_token
  request_body = { "apiKey": Onlia.configuration.api_key }
  post("/login", request_body)
end

#post(endpoint, body, token = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/onlia/client.rb', line 27

def post(endpoint, body, token = nil)
  self.refresh_if_required
  endpoint_url = "#{Onlia.configuration.base_url}/#{endpoint}"
  headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json",
  }
  headers["Authorization"] = "Bearer #{token}" if !token.nil?
  begin
    response = RestClient::Request.execute(method: :post, url: endpoint_url, payload: body.to_json, headers: headers)
    if endpoint === "/Auto/activate"
      if response&.body&.include?("%PDF")
        response.body
      else
        JSON.parse(response.body)
      end
    else
      JSON.parse(response.body)
    end
  rescue RestClient::ExceptionWithResponse => exception
    JSON.parse(exception.response.body)
  rescue JSON::ParserError => exception
    puts exception.response
  rescue RestClient::Unauthorized, RestClient::Forbidden => exception
    puts exception.response
  end
end

#property_address_validate(params) ⇒ Object



72
73
74
# File 'lib/onlia/client.rb', line 72

def property_address_validate(params)
  post("/Property/address/validate", params, @api_token)
end

#refresh_if_requiredObject



21
22
23
24
25
# File 'lib/onlia/client.rb', line 21

def refresh_if_required
  if @decoded_token && @decoded_token["exp"] < Time.now.to_i
    self.refresh_token
  end
end

#refresh_tokenObject



15
16
17
18
19
# File 'lib/onlia/client.rb', line 15

def refresh_token
  @raw_token = get_token
  @api_token = @raw_token["token"]
  @decoded_token = JWT.decode(@api_token, nil, false).first
end

#terminate_agreement(params) ⇒ Object



80
81
82
# File 'lib/onlia/client.rb', line 80

def terminate_agreement(params)
  post("/auto/terminate", params, @api_token)
end