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



78
79
80
# File 'lib/feathr/client.rb', line 78

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

#api_token_refreshObject



82
83
84
# File 'lib/feathr/client.rb', line 82

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

#api_token_verifyObject



86
87
88
# File 'lib/feathr/client.rb', line 86

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



94
95
96
# File 'lib/feathr/client.rb', line 94

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

#contractorsObject



110
111
112
# File 'lib/feathr/client.rb', line 110

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


126
127
128
# File 'lib/feathr/client.rb', line 126

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



114
115
116
# File 'lib/feathr/client.rb', line 114

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

#invoicesObject



134
135
136
# File 'lib/feathr/client.rb', line 134

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

#managersObject



98
99
100
# File 'lib/feathr/client.rb', line 98

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

#membershipsObject



102
103
104
# File 'lib/feathr/client.rb', line 102

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

#platformsObject



106
107
108
# File 'lib/feathr/client.rb', line 106

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

#rebatesObject



118
119
120
# File 'lib/feathr/client.rb', line 118

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

#receiveablesObject



122
123
124
# File 'lib/feathr/client.rb', line 122

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
# 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



90
91
92
# File 'lib/feathr/client.rb', line 90

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

#usersObject



142
143
144
# File 'lib/feathr/client.rb', line 142

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