Class: Feathr::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/feathr/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Feathr::Config.default) ⇒ Client

Returns a new instance of Client.



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

def initialize(config: Feathr::Config.default)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/feathr/client.rb', line 7

def config
  @config
end

Class Method Details

.defaultObject



13
14
15
# File 'lib/feathr/client.rb', line 13

def self.default
  @@default ||= Feathr::Client.new
end

Instance Method Details

#api_token_authObject



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

def api_token_auth
  @api_token_auth ||= Feathr::Api::ApiTokenAuth.new(client: self)
end

#api_token_refreshObject



84
85
86
# File 'lib/feathr/client.rb', line 84

def api_token_refresh
  @api_token_refresh ||= Feathr::Api::ApiTokenRefresh.new(client: self)
end

#api_token_verifyObject



88
89
90
# File 'lib/feathr/client.rb', line 88

def api_token_verify
  @api_token_verify ||= Feathr::Api::ApiTokenVerify.new(client: self)
end

#auth_headersObject



21
22
23
# File 'lib/feathr/client.rb', line 21

def auth_headers
  { Authorization: "Bearer #{ auth_token }" }
end

#auth_tokenObject



25
26
27
28
29
30
31
# File 'lib/feathr/client.rb', line 25

def auth_token
  @auth_token ||= if defined?(Rails)
    fetch_from_cache!
  else
    authenticate!
  end
end

#authenticate!Object



33
34
35
36
37
38
# File 'lib/feathr/client.rb', line 33

def authenticate!
  @auth_token = api_token_auth.authenticate(
    email:    config.api_email,
    password: config.api_password
  )['token']
end

#cashoutsObject



96
97
98
# File 'lib/feathr/client.rb', line 96

def cashouts
  @cashouts ||= Feathr::Api::Cashouts.new(client: self)
end

#contractorsObject



112
113
114
# File 'lib/feathr/client.rb', line 112

def contractors
  @contractors ||= Feathr::Api::Contractors.new(client: self)
end

#default_headersObject



17
18
19
# File 'lib/feathr/client.rb', line 17

def default_headers
  { 'Content-Type' => 'application/json' }
end


128
129
130
# File 'lib/feathr/client.rb', line 128

def featured_platforms
  @featured_platforms ||= Feathr::Api::FeaturedPlatforms.new(client: self)
end

#fetch_from_cache!Object



48
49
50
# File 'lib/feathr/client.rb', line 48

def fetch_from_cache!
  Rails.cache.fetch('qwil_api_token', expires_in: 24.hours) { authenticate! }
end

#incomesObject



116
117
118
# File 'lib/feathr/client.rb', line 116

def incomes
  @incomes ||= Feathr::Api::Incomes.new(client: self)
end

#invoicesObject



136
137
138
# File 'lib/feathr/client.rb', line 136

def invoices
  @invoices ||= Feathr::Api::Invoices.new(client: self)
end

#managersObject



100
101
102
# File 'lib/feathr/client.rb', line 100

def managers
  @managers ||= Feathr::Api::Managers.new(client: self)
end

#membershipsObject



104
105
106
# File 'lib/feathr/client.rb', line 104

def memberships
  @memberships ||= Feathr::Api::Memberships.new(client: self)
end

#platformsObject



108
109
110
# File 'lib/feathr/client.rb', line 108

def platforms
  @platforms ||= Feathr::Api::Platforms.new(client: self)
end

#rebatesObject



120
121
122
# File 'lib/feathr/client.rb', line 120

def rebates
  @rebates ||= Feathr::Api::Rebates.new(client: self)
end

#receiveablesObject



124
125
126
# File 'lib/feathr/client.rb', line 124

def receiveables
  @receiveables ||= Feathr::Api::Receiveables.new(client: self)
end

#refresh_token!Object



40
41
42
# File 'lib/feathr/client.rb', line 40

def refresh_token!
  @auth_token = api_token_refresh.refresh!(auth_token)['token']
end

#request(method: :get, path:, query: {}, headers: {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/feathr/client.rb', line 52

def request(method: :get, path:, query: {}, headers: {})
  url = [config.base_url, path].join('/')



  unless path == Feathr::Api::ApiTokenAuth.new.api_path
    headers = default_headers.merge(auth_headers).merge(headers)
    query   = query.to_json
  end

  response = self.class.send(
    method,
    url,
    body:     query,
    headers:  headers
  )

  if response.success? && block_given?
    yield response.parsed_response
  elsif response.success?
    response.parsed_response
  else
    raise Feathr::Api::FeathrError.new(
      status: response.code,
      errors: response.parsed_response)
  end
end

#reset_passwordObject



92
93
94
# File 'lib/feathr/client.rb', line 92

def reset_password
  @reset_password ||= Feathr::Api::ResetPassword.new(client: self)
end

#usersObject



144
145
146
# File 'lib/feathr/client.rb', line 144

def users
  @users ||= Feathr::Api::Users.new(client: self)
end

#verify_token!Object



44
45
46
# File 'lib/feathr/client.rb', line 44

def verify_token!
  api_token_verify.verify(auth_token)
end