Class: ApartmentRatings::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/apartment_ratings/client.rb', line 8

def initialize(options = {})
  path = options.fetch(:api_base_path)

  @connection = Faraday.new(path) do |faraday|
    faraday.request :url_encoded # Unfortunately, we need to encode post and put params
    faraday.request :json

    # NOTE: Unfortunately, the response content type is not json... But we know it is.
    faraday.response :json, content_type: /\Atext\/html;charset=ISO-8859-1\z/
    faraday.response :logger if ApartmentRatings.config.debug

    faraday.adapter ApartmentRatings.config.faraday_adapter || Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/apartment_ratings/client.rb', line 6

def connection
  @connection
end

Instance Method Details

#refresh_tokenObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/apartment_ratings/client.rb', line 27

def refresh_token
  @token = nil
  result = JSON.parse @connection.post('login', ApartmentRatings.credentials.merge(format: 'json')).body

  if result['success']
    @token = result['response']
  else
    fail ApartmentRatings::Errors::InvalidToken
  end
end

#tokenObject



23
24
25
# File 'lib/apartment_ratings/client.rb', line 23

def token
  @token ||= refresh_token
end