Class: Netologiest::Resource
- Inherits:
-
Object
- Object
- Netologiest::Resource
- Defined in:
- lib/netologiest/resource.rb
Overview
This class describe client for Netology API. It contains finder actions and special handlers methods
Direct Known Subclasses
Class Attribute Summary collapse
-
.resource_name ⇒ Object
Returns the value of attribute resource_name.
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_expire ⇒ Object
readonly
Returns the value of attribute token_expire.
Class Method Summary collapse
Instance Method Summary collapse
-
#authorize! ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics//MethodLength.
- #detail(id) ⇒ Object
- #handle_detail(_response) ⇒ Object
- #handle_list(_response) ⇒ Object
-
#initialize ⇒ Resource
constructor
A new instance of Resource.
- #list ⇒ Object
-
#token_expired? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics//MethodLength.
Constructor Details
#initialize ⇒ Resource
Returns a new instance of Resource.
16 17 18 |
# File 'lib/netologiest/resource.rb', line 16 def initialize end |
Class Attribute Details
.resource_name ⇒ Object
Returns the value of attribute resource_name.
11 12 13 |
# File 'lib/netologiest/resource.rb', line 11 def resource_name @resource_name end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
14 15 16 |
# File 'lib/netologiest/resource.rb', line 14 def token @token end |
#token_expire ⇒ Object (readonly)
Returns the value of attribute token_expire.
14 15 16 |
# File 'lib/netologiest/resource.rb', line 14 def token_expire @token_expire end |
Class Method Details
.detail(id) ⇒ Object
24 25 26 |
# File 'lib/netologiest/resource.rb', line 24 def self.detail(id) new.detail(id) end |
.list ⇒ Object
20 21 22 |
# File 'lib/netologiest/resource.rb', line 20 def self.list new.list end |
Instance Method Details
#authorize! ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics//MethodLength
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/netologiest/resource.rb', line 45 def url = build_url('gettoken') params = { client_secret: Netologiest.config.api_key } RestClient.get(url, params: params) do |response, _request, _result| case response.code when 200 body = JSON.parse(response.body) @token_expire = Time.now.to_i + body.fetch('expires_in').to_i @token = body['access_token'] when 401 fail Netologiest::Unauthorized, response.body else response end end end |
#detail(id) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/netologiest/resource.rb', line 36 def detail(id) handle_detail( get( build_url(self.class.resource_name, id) ) ) end |
#handle_detail(_response) ⇒ Object
70 |
# File 'lib/netologiest/resource.rb', line 70 def handle_detail(_response); end |
#handle_list(_response) ⇒ Object
68 |
# File 'lib/netologiest/resource.rb', line 68 def handle_list(_response); end |
#list ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/netologiest/resource.rb', line 28 def list handle_list( get( build_url(self.class.resource_name) ) ) end |
#token_expired? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics//MethodLength
63 64 65 66 |
# File 'lib/netologiest/resource.rb', line 63 def token_expired? return true unless token_expire.present? token_expire < Time.now.to_i end |