Class: Bci::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bci/base.rb

Overview

The base class for all api clients

Direct Known Subclasses

Beneficios, Consumo, Hipotecario, Stats

Constant Summary collapse

BASE_URL =
'https://apidevelopers.bci.cl'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/bci/base.rb', line 6

def initialize(key = nil)
  @api_key = key
end

Instance Method Details

#connectorObject



14
15
16
17
# File 'lib/bci/base.rb', line 14

def connector
  raise 'Undefined key' unless @api_key
  RestClient
end

#headersObject



19
20
21
22
23
24
25
# File 'lib/bci/base.rb', line 19

def headers
  {
    'X-IBM-Client-Id' => @api_key,
    accept: 'application/json',
    content_type: 'application/json'
  }
end

#response_checker(response) ⇒ Object



27
28
29
30
31
32
# File 'lib/bci/base.rb', line 27

def response_checker(response)
  response_code = response.code.to_s
  raise 'Not a valid response' unless %w[200 201].include? response_code

  JSON.parse(response.body)
end

#submoduleObject



34
35
36
# File 'lib/bci/base.rb', line 34

def submodule
  raise 'This should be overriden'
end

#urlObject



10
11
12
# File 'lib/bci/base.rb', line 10

def url
  "#{BASE_URL}/#{submodule}"
end