Class: Spearly::Auth::Client

Inherits:
Object
  • Object
show all
Includes:
Team, TeamMember, TeamSubscription, Token, User
Defined in:
lib/spearly/auth/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from User

#get_profile, #get_user

Methods included from Token

#get_token_info, #get_tokens, #revoke_token

Methods included from TeamSubscription

#get_team_subscriptions

Methods included from TeamMember

#get_team_members, #invite_user_to_team

Methods included from Team

#create_team, #get_team, #get_teams, #regenerate_team_api_access_token, #update_team

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
# File 'lib/spearly/auth/client.rb', line 20

def initialize(token)
  @token = token

  base_url = ENV.fetch('SPEARLY_API_URL', nil)
  @client = Faraday.new(base_url)
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



18
19
20
# File 'lib/spearly/auth/client.rb', line 18

def token
  @token
end

Instance Method Details

#process_response(response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spearly/auth/client.rb', line 39

def process_response(response)
  case response.status
  when 200, 201, 422
    JSON.parse(response.body)
  when 401
    raise AuthorizationError, 'Unauthorized'
  when 404
    raise NotFoundError, 'Not Found'
  when 503
    raise ServiceUnavailableError, 'Service Unavailable'
  else
    raise ServerError, 'Server Error'
  end
end

#run_request(method:, path:, params: nil) ⇒ Object



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

def run_request(method:, path:, params: nil)
  path_parsed = Addressable::URI.parse(path).normalize.to_s

  @client.run_request(method.to_sym,
                      path_parsed,
                      params,
                      'Accept' => 'application/vnd.spearly.v2+json',
                      'Authorization' => @token)
rescue Faraday::ConnectionFailed => e
  raise ServerError, e.message
end