Class: Dotloop::Authenticate

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dotloop/authenticate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, app_secret:, application: 'dotloop') ⇒ Authenticate

Returns a new instance of Authenticate.



13
14
15
16
17
18
19
# File 'lib/dotloop/authenticate.rb', line 13

def initialize(app_id:, app_secret:, application: 'dotloop')
  @app_id      = app_id
  @app_secret  = app_secret
  @application = application
  raise 'Please enter an APP id' unless @app_id
  raise 'Please enter an APP secret' unless @app_secret
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



9
10
11
# File 'lib/dotloop/authenticate.rb', line 9

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



10
11
12
# File 'lib/dotloop/authenticate.rb', line 10

def app_secret
  @app_secret
end

#applicationObject

Returns the value of attribute application.



11
12
13
# File 'lib/dotloop/authenticate.rb', line 11

def application
  @application
end

Instance Method Details

#acquire_access_and_refresh_token(code, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/dotloop/authenticate.rb', line 21

def acquire_access_and_refresh_token(code, options = {})
  params = {
    grant_type:   'authorization_code',
    code:         code,
    redirect_uri: options[:redirect_uri]
  }

  raw('/token', params)
end

#handle_dotloop_error(response_code) ⇒ Object

Raises:

  • (error)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dotloop/authenticate.rb', line 54

def handle_dotloop_error(response_code)
  error = case response_code
          when 400
            Dotloop::BadRequest
          when 401
            Dotloop::Unauthorized
          when 403
            Dotloop::Forbidden
          else
            StandardError
          end
  raise error, "Error communicating: Response code #{response_code}"
end

#raw(page, params = {}) ⇒ Object



48
49
50
51
52
# File 'lib/dotloop/authenticate.rb', line 48

def raw(page, params = {})
  response = self.class.post(page, query: params, headers: headers, timeout: 60)
  handle_dotloop_error(response.code) if response.code != 200
  response.parsed_response
end

#refresh_access_token(refresh_token) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/dotloop/authenticate.rb', line 31

def refresh_access_token(refresh_token)
  params = {
    grant_type:    'refresh_token',
    refresh_token: refresh_token
  }

  raw('/token', params)
end

#revoke_access(access_token) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/dotloop/authenticate.rb', line 40

def revoke_access(access_token)
  params = {
    token: access_token
  }

  raw('/token/revoke', params)
end

#url_for_authentication(redirect_uri, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dotloop/authenticate.rb', line 68

def url_for_authentication(redirect_uri, options = {})
  params = {
    client_id:     @app_id,
    response_type: 'code',
    redirect_uri:  redirect_uri
  }

  options.key?(:state) && params[:state] = options[:state]
  options.key?(:redirect_on_deny) && params[:redirect_on_deny] = options[:redirect_on_deny]

  "https://auth.dotloop.com/oauth/authorize?#{params.to_query}"
end