Class: FrilansFinansAPI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/frilans_finans_api/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#jsonObject (readonly)

Returns the value of attribute json.



7
8
9
# File 'lib/frilans_finans_api/document.rb', line 7

def json
  @json
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/frilans_finans_api/document.rb', line 7

def status
  @status
end

#uriObject (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

Returns:

  • (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

#countObject



63
64
65
# File 'lib/frilans_finans_api/document.rb', line 63

def count
  json.dig('meta', 'pagination', 'items')
end

#current_pageObject



47
48
49
# File 'lib/frilans_finans_api/document.rb', line 47

def current_page
  json.dig('meta', 'pagination', 'page', 'number')
end

#dataObject



39
40
41
# File 'lib/frilans_finans_api/document.rb', line 39

def data
  json['data']
end

#error_status?Boolean

Returns:

  • (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


43
44
45
# File 'lib/frilans_finans_api/document.rb', line 43

def next_page_link
  json.dig('links', 'next')
end

#per_pageObject



51
52
53
# File 'lib/frilans_finans_api/document.rb', line 51

def per_page
  json.dig('meta', 'pagination', 'page', 'size')
end

#resourceObject



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

#resourcesObject



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

#totalObject



59
60
61
# File 'lib/frilans_finans_api/document.rb', line 59

def total
  json.dig('meta', 'pagination', 'items')
end

#total_pagesObject



55
56
57
# File 'lib/frilans_finans_api/document.rb', line 55

def total_pages
  json.dig('meta', 'pagination', 'pages')
end