Class: Badgrcat::Client
- Inherits:
-
Footrest::Client
- Object
- Footrest::Client
- Badgrcat::Client
- Defined in:
- lib/badgrcat/client.rb,
lib/badgrcat/client/methods.rb
Defined Under Namespace
Modules: Methods
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #authenticate! ⇒ Object
-
#delete(path, options = {}) ⇒ Object
Modify delete request to send params in body instead of query params.
-
#initialize(options = {}, &block) ⇒ Client
constructor
A new instance of Client.
-
#request(method, &block) ⇒ Object
Override Footrest request for ApiArray support.
-
#request_with_params_in_body(method, path, options) ⇒ Object
Override Footrest request for sending params in body as json.
Constructor Details
#initialize(options = {}, &block) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 |
# File 'lib/badgrcat/client.rb', line 15 def initialize( = {}, &block) super @refresh_token = config[:refresh_token] if config[:refresh_token].present? if config[:access_token].present? connection.headers[:authorization] = "Bearer #{config[:access_token]}" connection.headers['Content-Type'] = 'application/json' end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
13 14 15 |
# File 'lib/badgrcat/client.rb', line 13 def access_token @access_token end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
13 14 15 |
# File 'lib/badgrcat/client.rb', line 13 def expires_in @expires_in end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
13 14 15 |
# File 'lib/badgrcat/client.rb', line 13 def refresh_token @refresh_token end |
Instance Method Details
#authenticate! ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/badgrcat/client.rb', line 56 def authenticate! connection.headers[:authorization] = nil connection.headers['Content-Type'] = nil auth_url = config[:auth_url] tok_params = { scope: config[:scope], client_id: config[:client_id], client_secret: config[:client_secret], grant_type: config[:grant_type], } tok_params.reject! { |_k, v| v.nil? } if @refresh_token tok_params.merge!({ grant_type: "refresh_token", refresh_token: @refresh_token, }) else tok_params.merge!({ username: config[:username], password: config[:password], }) end authresp = connection.send(:post, auth_url, tok_params) authdata = authresp.body @access_token = authdata["access_token"] @refresh_token = authdata["refresh_token"].presence || @refresh_token @expires_in = authdata["expires_in"] connection.headers[:authorization] = "#{authdata['token_type']} #{@access_token}" connection.headers['Content-Type'] = "application/json" end |
#delete(path, options = {}) ⇒ Object
Modify delete request to send params in body instead of query params
52 53 54 |
# File 'lib/badgrcat/client.rb', line 52 def delete(path, = {}) request_with_params_in_body(:delete, path, ) end |
#request(method, &block) ⇒ Object
Override Footrest request for ApiArray support
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/badgrcat/client.rb', line 25 def request(method, &block) response = begin connection.send(method, &block) rescue Footrest::HttpError::BadRequest, Footrest::HttpError::NotFound => e raise e unless connection.headers[:authorization].nil? # Reauthenticate and retry if authorization header is nil authenticate! connection.send(method, &block) rescue Footrest::HttpError::Unauthorized # Reauthenticate and retry authenticate! connection.send(method, &block) end Badgrcat::ApiArray.process_response(response, self) end |
#request_with_params_in_body(method, path, options) ⇒ Object
Override Footrest request for sending params in body as json
44 45 46 47 48 49 |
# File 'lib/badgrcat/client.rb', line 44 def request_with_params_in_body(method, path, ) request(method) do |r| r.path = fullpath(path) r.body = .to_json unless .empty? end end |