Class: DearInventory::Response

Inherits:
Object
  • Object
show all
Extended by:
IsASubclass, T::Sig
Defined in:
lib/dear_inventory/response.rb

Direct Known Subclasses

TestResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IsASubclass

is_a?

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

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/dear_inventory/response.rb', line 10

def request
  @request
end

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

#errorObject



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

#fieldsObject



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

#headersObject



65
66
67
# File 'lib/dear_inventory/response.rb', line 65

def headers
  @response.headers
end

#http_statusObject



70
71
72
# File 'lib/dear_inventory/response.rb', line 70

def http_status
  @http_status ||= @response.status.code
end

#next_pageObject



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

Returns:

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



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

Returns:

  • (Boolean)


75
76
77
# File 'lib/dear_inventory/response.rb', line 75

def paginated?
  @model.respond_to?(:page)
end

#raise_not_paginatedObject



120
121
122
# File 'lib/dear_inventory/response.rb', line 120

def raise_not_paginated
  raise DearInventory::NotPaginatedError.new(uri: uri)
end

#success?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/dear_inventory/response.rb', line 125

def success?
  http_status == 200
end

#uriObject



130
131
132
# File 'lib/dear_inventory/response.rb', line 130

def uri
  @uri ||= @response.uri.to_s
end