Class: FrilansFinansAPI::Document
- Inherits:
-
Object
- Object
- FrilansFinansAPI::Document
- Defined in:
- lib/frilans_finans_api/document.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #collection? ⇒ Boolean
- #count ⇒ Object
- #current_page ⇒ Object
- #data ⇒ Object
- #error_status? ⇒ Boolean
-
#initialize(response) ⇒ Document
constructor
A new instance of Document.
- #next_page_link ⇒ Object
- #per_page ⇒ Object
- #resource ⇒ Object
- #resources ⇒ Object
- #total ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(response) ⇒ Document
Returns a new instance of Document.
9 10 11 12 13 |
# File 'lib/frilans_finans_api/document.rb', line 9 def initialize(response) @json = JSON.parse(response.body) @status = response.code @uri = response.request.uri.to_s end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
7 8 9 |
# File 'lib/frilans_finans_api/document.rb', line 7 def json @json end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/frilans_finans_api/document.rb', line 7 def status @status end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/frilans_finans_api/document.rb', line 7 def uri @uri end |
Instance Method Details
#collection? ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/frilans_finans_api/document.rb', line 32 def collection? data = json['data'] return false if data.nil? || data.is_a?(Hash) true end |
#count ⇒ Object
63 64 65 |
# File 'lib/frilans_finans_api/document.rb', line 63 def count json.dig('meta', 'pagination', 'items') end |
#current_page ⇒ Object
47 48 49 |
# File 'lib/frilans_finans_api/document.rb', line 47 def current_page json.dig('meta', 'pagination', 'page', 'number') end |
#data ⇒ Object
39 40 41 |
# File 'lib/frilans_finans_api/document.rb', line 39 def data json['data'] end |
#error_status? ⇒ Boolean
67 68 69 70 |
# File 'lib/frilans_finans_api/document.rb', line 67 def error_status? # Consider HTTP status 3XX, 4XX and 5XX as errors status >= 300 end |
#next_page_link ⇒ Object
43 44 45 |
# File 'lib/frilans_finans_api/document.rb', line 43 def next_page_link json.dig('links', 'next') end |
#per_page ⇒ Object
51 52 53 |
# File 'lib/frilans_finans_api/document.rb', line 51 def per_page json.dig('meta', 'pagination', 'page', 'size') end |
#resource ⇒ Object
25 26 27 28 29 30 |
# File 'lib/frilans_finans_api/document.rb', line 25 def resource @resource ||= begin resource_data = collection? ? data.first : data Resource.new(resource_data) end end |
#resources ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/frilans_finans_api/document.rb', line 15 def resources @resources ||= begin if collection? data.map { |resource| Resource.new(resource) } else [Resource.new(data)] end end end |
#total ⇒ Object
59 60 61 |
# File 'lib/frilans_finans_api/document.rb', line 59 def total json.dig('meta', 'pagination', 'items') end |
#total_pages ⇒ Object
55 56 57 |
# File 'lib/frilans_finans_api/document.rb', line 55 def total_pages json.dig('meta', 'pagination', 'pages') end |