Class: DearInventory::Response
- Inherits:
-
Object
- Object
- DearInventory::Response
- Extended by:
- IsASubclass
- Defined in:
- lib/dear_inventory/response.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #error ⇒ Object
-
#fields ⇒ Object
rubocop:enable Metrics/MethodLength.
- #headers ⇒ Object
- #http_status ⇒ Object
-
#initialize(request:, response:, num_previous_records: 0) ⇒ Response
constructor
rubocop:disable Metrics/MethodLength.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #num_records_paged ⇒ Object
- #paginated? ⇒ Boolean
- #raise_not_paginated ⇒ Object
- #success? ⇒ Boolean
- #uri ⇒ Object
Methods included from IsASubclass
Constructor Details
#initialize(request:, response:, num_previous_records: 0) ⇒ Response
rubocop:disable Metrics/MethodLength
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dear_inventory/response.rb', line 10 def initialize(request:, response:, num_previous_records: 0) @request = request @response = response @num_previous_records = num_previous_records @fields = nil @http_status = nil @load_full_record = nil @num_records_paged = nil @uri = nil raise_error unless success? @model = @request.model.new(body) assign_values end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/dear_inventory/response.rb', line 7 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/dear_inventory/response.rb', line 7 def response @response end |
Instance Method Details
#each(&block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dear_inventory/response.rb', line 59 def each(&block) raise_not_paginated unless paginated? response = self loop do iterate_over_records(response, block) break unless response.next_page? response = response.next_page end end |
#error ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/dear_inventory/response.rb', line 38 def error if body.respond_to?(:fetch) body_copy = body return body_copy.fetch("Exception", nil) end body end |
#fields ⇒ Object
rubocop:enable Metrics/MethodLength
28 29 30 31 32 33 34 35 36 |
# File 'lib/dear_inventory/response.rb', line 28 def fields @fields ||= begin values = @request.model.const_get(:FIELDS).values.map do |field| field[:name] end values.unshift(:records) if @model.respond_to?(:records) values end end |
#headers ⇒ Object
47 48 49 |
# File 'lib/dear_inventory/response.rb', line 47 def headers @response.headers end |
#http_status ⇒ Object
51 52 53 |
# File 'lib/dear_inventory/response.rb', line 51 def http_status @http_status ||= @response.status.code end |
#next_page ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/dear_inventory/response.rb', line 71 def next_page raise_not_paginated unless paginated? raise DearInventory::NoMorePagesError unless next_page? request = @request.dup request.params.page = @model.page + 1 DearInventory::Request.(request, num_previous_records: num_records_paged) end |
#next_page? ⇒ Boolean
81 82 83 84 85 |
# File 'lib/dear_inventory/response.rb', line 81 def next_page? raise_not_paginated unless paginated? num_records_paged < total end |
#num_records_paged ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/dear_inventory/response.rb', line 87 def num_records_paged @num_records_paged ||= begin raise_not_paginated unless paginated? @num_previous_records + records.count end end |
#paginated? ⇒ Boolean
55 56 57 |
# File 'lib/dear_inventory/response.rb', line 55 def paginated? @model.respond_to?(:page) end |
#raise_not_paginated ⇒ Object
95 96 97 |
# File 'lib/dear_inventory/response.rb', line 95 def raise_not_paginated raise DearInventory::NotPaginatedError.new(uri) end |
#success? ⇒ Boolean
99 100 101 |
# File 'lib/dear_inventory/response.rb', line 99 def success? http_status == 200 end |
#uri ⇒ Object
103 104 105 |
# File 'lib/dear_inventory/response.rb', line 103 def uri @uri ||= @response.uri.to_s end |