Class: DearInventory::Response
- Inherits:
-
Object
- Object
- DearInventory::Response
- Extended by:
- IsASubclass, T::Sig
- 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
- #headers ⇒ Object
- #http_status ⇒ Object
-
#initialize(request:, response:, num_previous_records: 0) ⇒ Response
constructor
A new instance of Response.
- #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
Returns a new instance of Response.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dear_inventory/response.rb', line 23 def initialize(request:, response:, num_previous_records: 0) @request = T.let(request, DearInventory::Models::Request) @response = T.let(response, HTTP::Response) @num_previous_records = T.let(num_previous_records, Integer) @fields = T.let(nil, T.nilable(T::Array[Symbol])) @http_status = T.let(nil, T.nilable(Integer)) @load_full_record = T.let(nil, T.nilable(T::Boolean)) @num_records_paged = T.let(nil, T.nilable(Integer)) @uri = T.let(nil, T.nilable(String)) raise_error unless success? body_copy = T.cast(body, T.nilable(T::Hash[String, T.untyped])) @model = T.let(@request.model.new(body_copy), DearInventory::Model) assign_values end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
10 11 12 |
# File 'lib/dear_inventory/response.rb', line 10 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
12 13 14 |
# File 'lib/dear_inventory/response.rb', line 12 def response @response end |
Instance Method Details
#each(&block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dear_inventory/response.rb', line 80 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
55 56 57 58 59 60 61 62 |
# File 'lib/dear_inventory/response.rb', line 55 def error if body.respond_to?(:fetch) body_copy = T.cast(body, T::Hash[String, T.untyped]) return body_copy.fetch("Exception", nil) end T.cast(body, T.nilable(String)) end |
#fields ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/dear_inventory/response.rb', line 44 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
65 66 67 |
# File 'lib/dear_inventory/response.rb', line 65 def headers @response.headers end |
#http_status ⇒ Object
70 71 72 |
# File 'lib/dear_inventory/response.rb', line 70 def http_status @http_status ||= @response.status.code end |
#next_page ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/dear_inventory/response.rb', line 93 def next_page raise_not_paginated unless paginated? raise DearInventory::NoMorePagesError unless next_page? request = @request.dup T.unsafe(request.params).page = T.unsafe(@model).page + 1 DearInventory::Request.(request, num_previous_records: num_records_paged) end |
#next_page? ⇒ Boolean
104 105 106 107 108 |
# File 'lib/dear_inventory/response.rb', line 104 def next_page? raise_not_paginated unless paginated? num_records_paged < T.unsafe(self).total end |
#num_records_paged ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/dear_inventory/response.rb', line 111 def num_records_paged @num_records_paged ||= begin raise_not_paginated unless paginated? @num_previous_records + T.unsafe(self).records.count end end |
#paginated? ⇒ Boolean
75 76 77 |
# File 'lib/dear_inventory/response.rb', line 75 def paginated? @model.respond_to?(:page) end |
#raise_not_paginated ⇒ Object
120 121 122 |
# File 'lib/dear_inventory/response.rb', line 120 def raise_not_paginated raise DearInventory::NotPaginatedError.new(uri: uri) end |
#success? ⇒ Boolean
125 126 127 |
# File 'lib/dear_inventory/response.rb', line 125 def success? http_status == 200 end |
#uri ⇒ Object
130 131 132 |
# File 'lib/dear_inventory/response.rb', line 130 def uri @uri ||= @response.uri.to_s end |