Class: FIS::Client::Base

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

Constant Summary collapse

BASE_URL =
ENV.fetch('FIS_CLIENT_BASE_URL', 'https://portal.flatironschool.com')
API_ROOT =
'/api/v1'
CLIENT_ERROR_STATUSES =
[400, 401, 403, 407, 422].freeze
SERVER_ERROR_STATUSES =
(500..511).to_a.freeze
ERROR_STATUSES =
(CLIENT_ERROR_STATUSES + SERVER_ERROR_STATUSES).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fis/client/base.rb', line 16

def initialize(token:)
  url = [BASE_URL, API_ROOT].join

  @client = Faraday.new(url: url) do |conn|
    conn.authorization :Bearer, token

    conn.response :json, 
                  content_type: 'application/json',
                  parser_options: {
                    object_class: OpenStruct 
                  }
    
    conn.request :retry,
                 max: 3,
                 interval: 2,
                 interval_randomness: 0.5,
                 backoff_factor: 2,
                 methods: Faraday::Request::Retry::IDEMPOTENT_METHODS + [:post],
                 retry_statuses: ERROR_STATUSES,
                 exceptions: Faraday::Request::Retry::DEFAULT_EXCEPTIONS

    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/fis/client/base.rb', line 14

def client
  @client
end

Instance Method Details

#userObject



45
46
47
# File 'lib/fis/client/base.rb', line 45

def user
  @user ||= User.new(base: self)
end

#valid_token?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fis/client/base.rb', line 41

def valid_token?
  !!user.data
end