Class: Figo::Connection
- Inherits:
-
Object
- Object
- Figo::Connection
- Defined in:
- lib/figo.rb
Overview
Represents a non user-bound connection to the figo Connect API.
It’s main purpose is to let user login via OAuth 2.0.
Instance Method Summary collapse
-
#create_user(name, email, password, language = 'de', send_newsletter = True) ⇒ Hash
Create a new figo Account.
-
#initialize(client_id, client_secret, redirect_uri = nil) ⇒ Connection
constructor
Create connection object with client credentials.
-
#login_url(state, scope = nil) ⇒ String
Get the URL a user should open in the web browser to start the login process.
-
#obtain_access_token(authorization_code_or_refresh_token, scope = nil) ⇒ Hash
Exchange authorization code or refresh token for access token.
-
#query_api(path, data = nil) ⇒ Hash
Helper method for making a OAuth 2.0 request.
-
#revoke_token(refresh_token_or_access_token) ⇒ nil
Revoke refresh token or access token.
Constructor Details
#initialize(client_id, client_secret, redirect_uri = nil) ⇒ Connection
Create connection object with client credentials.
115 116 117 118 119 120 |
# File 'lib/figo.rb', line 115 def initialize(client_id, client_secret, redirect_uri = nil) @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @https = HTTPS.new("figo-#{client_id}") end |
Instance Method Details
#create_user(name, email, password, language = 'de', send_newsletter = True) ⇒ Hash
Create a new figo Account
207 208 209 210 |
# File 'lib/figo.rb', line 207 def create_user(name, email, password, language='de', =True) data = { 'name' => name, 'email' => email, 'password' => password, 'language' => language, 'send_newsletter' => , 'affiliate_client_id' => @client_id} return query_api("/auth/user", data) end |
#login_url(state, scope = nil) ⇒ String
Get the URL a user should open in the web browser to start the login process.
When the process is completed, the user is redirected to the URL provided to the constructor and passes on an authentication code. This code can be converted into an access token for data access.
158 159 160 161 162 163 |
# File 'lib/figo.rb', line 158 def login_url(state, scope = nil) data = { "response_type" => "code", "client_id" => @client_id, "state" => state } data["redirect_uri"] = @redirect_uri unless @redirect_uri.nil? data["scope"] = scope unless scope.nil? return "https://#{$api_endpoint}/auth/code?" + URI.encode_www_form(data) end |
#obtain_access_token(authorization_code_or_refresh_token, scope = nil) ⇒ Hash
Exchange authorization code or refresh token for access token.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/figo.rb', line 175 def obtain_access_token(, scope = nil) # Authorization codes always start with "O" and refresh tokens always start with "R". if [0] == "O" data = { "grant_type" => "authorization_code", "code" => } data["redirect_uri"] = @redirect_uri unless @redirect_uri.nil? elsif [0] == "R" data = { "grant_type" => "refresh_token", "refresh_token" => } data["scope"] = scope unless scope.nil? end return query_api("/auth/token", data) end |
#query_api(path, data = nil) ⇒ Hash
Helper method for making a OAuth 2.0 request.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/figo.rb', line 127 def query_api(path, data = nil) uri = URI("https://#{$api_endpoint}#{path}") # Setup HTTP request. request = Net::HTTP::Post.new(path) request.basic_auth(@client_id, @client_secret) request["Accept"] = "application/json" request["Content-Type"] = "application/x-www-form-urlencoded" request["User-Agent"] = "ruby-figo" request.body = URI.encode_www_form(data) unless data.nil? # Send HTTP request. response = @https.request(uri, request) # Evaluate HTTP response. return response.body == "" ? {} : JSON.parse(response.body) end |
#revoke_token(refresh_token_or_access_token) ⇒ nil
this action has immediate effect, i.e. you will not be able use that token anymore after this call.
Revoke refresh token or access token.
193 194 195 196 197 |
# File 'lib/figo.rb', line 193 def revoke_token(refresh_token_or_access_token) data = { "token" => refresh_token_or_access_token } query_api("/auth/revoke?" + URI.encode_www_form(data)) return nil end |