Class: LDA::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lda_gov/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, status) ⇒ Response

Returns a new instance of Response.



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

def initialize(body, status)
  @raw    = body
  @status = status
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



7
8
9
# File 'lib/lda_gov/response.rb', line 7

def raw
  @raw
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/lda_gov/response.rb', line 7

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



67
68
69
70
71
# File 'lib/lda_gov/response.rb', line 67

def [](key)
  return nil unless raw.is_a?(Hash)

  raw[key]
end

#countObject Also known as: total_count



33
34
35
36
37
# File 'lib/lda_gov/response.rb', line 33

def count
  return raw['count'] if raw.is_a?(Hash) && raw.key?('count')

  size
end

#dig(*keys) ⇒ Object



73
74
75
76
77
# File 'lib/lda_gov/response.rb', line 73

def dig(*keys)
  return nil unless raw.is_a?(Hash)

  raw.dig(*keys)
end

#eachObject



14
15
16
# File 'lib/lda_gov/response.rb', line 14

def each(&)
  results.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/lda_gov/response.rb', line 29

def empty?
  results.empty?
end

#inspectObject



91
92
93
# File 'lib/lda_gov/response.rb', line 91

def inspect
  "#<#{self.class} status=#{status} size=#{size} count=#{count} next_page?=#{next_page?}>"
end

#next_page?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/lda_gov/response.rb', line 52

def next_page?
  !next_url.nil?
end

#next_page_numberObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/lda_gov/response.rb', line 56

def next_page_number
  return nil unless next_url

  uri = URI.parse(next_url)
  params = URI.decode_www_form(uri.query || '')
  page_param = params.find { |k, _| k == 'page' }
  page_param ? page_param[1].to_i : nil
rescue URI::InvalidURIError
  nil
end

#next_urlObject



40
41
42
43
44
# File 'lib/lda_gov/response.rb', line 40

def next_url
  return nil unless raw.is_a?(Hash)

  raw['next']
end

#previous_urlObject



46
47
48
49
50
# File 'lib/lda_gov/response.rb', line 46

def previous_url
  return nil unless raw.is_a?(Hash)

  raw['previous']
end

#resultsObject



18
19
20
21
22
# File 'lib/lda_gov/response.rb', line 18

def results
  return raw if raw.is_a?(Array)

  raw['results'] || []
end

#sizeObject Also known as: length



24
25
26
# File 'lib/lda_gov/response.rb', line 24

def size
  results.size
end

#success?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/lda_gov/response.rb', line 87

def success?
  status >= 200 && status < 300
end

#to_hObject



79
80
81
# File 'lib/lda_gov/response.rb', line 79

def to_h
  raw.is_a?(Hash) ? raw : { 'results' => raw }
end

#to_jsonObject



83
84
85
# File 'lib/lda_gov/response.rb', line 83

def to_json(*)
  to_h.to_json
end