Class: Moneybird::OAuth
- Inherits:
-
Object
- Object
- Moneybird::OAuth
- Defined in:
- lib/moneybird/oauth.rb
Constant Summary collapse
- AUTHORIZE_URL =
"https://moneybird.com/oauth/authorize"- TOKEN_URL =
"https://moneybird.com/oauth/token"- SCOPES =
%w[sales_invoices documents estimates bank time_entries settings].freeze
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
Instance Method Summary collapse
-
#authorize_url(scope: nil, state: nil) ⇒ Object
Returns the URL to redirect the user to for authorization.
-
#initialize(client_id:, client_secret:, redirect_uri:) ⇒ OAuth
constructor
A new instance of OAuth.
-
#refresh(refresh_token) ⇒ Object
Refresh an existing token Returns a new Token object.
-
#token(code) ⇒ Object
Exchange authorization code for tokens Returns a Token object.
Constructor Details
#initialize(client_id:, client_secret:, redirect_uri:) ⇒ OAuth
Returns a new instance of OAuth.
15 16 17 18 19 |
# File 'lib/moneybird/oauth.rb', line 15 def initialize(client_id:, client_secret:, redirect_uri:) @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
13 14 15 |
# File 'lib/moneybird/oauth.rb', line 13 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
13 14 15 |
# File 'lib/moneybird/oauth.rb', line 13 def client_secret @client_secret end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
13 14 15 |
# File 'lib/moneybird/oauth.rb', line 13 def redirect_uri @redirect_uri end |
Instance Method Details
#authorize_url(scope: nil, state: nil) ⇒ Object
Returns the URL to redirect the user to for authorization
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/moneybird/oauth.rb', line 22 def (scope: nil, state: nil) scope = scope.is_a?(Array) ? scope.join(" ") : (scope || "sales_invoices") params = { client_id: client_id, redirect_uri: redirect_uri, response_type: "code", scope: scope } params[:state] = state if state uri = URI.parse(AUTHORIZE_URL) uri.query = URI.encode_www_form(params) uri.to_s end |
#refresh(refresh_token) ⇒ Object
Refresh an existing token Returns a new Token object
53 54 55 56 57 58 59 60 61 |
# File 'lib/moneybird/oauth.rb', line 53 def refresh(refresh_token) response = post_token( grant_type: "refresh_token", client_id: client_id, client_secret: client_secret, refresh_token: refresh_token ) build_token(response) end |
#token(code) ⇒ Object
Exchange authorization code for tokens Returns a Token object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/moneybird/oauth.rb', line 40 def token(code) response = post_token( grant_type: "authorization_code", client_id: client_id, client_secret: client_secret, code: code, redirect_uri: redirect_uri ) build_token(response) end |