Class: Conexa::Result
- Inherits:
-
ConexaObject
- Object
- ConexaObject
- Conexa::Result
- Defined in:
- lib/conexa/resources/result.rb
Constant Summary
Constants inherited from ConexaObject
Instance Attribute Summary
Attributes inherited from ConexaObject
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Delegate empty? to data array instead of checking @attributes This fixes the case where Conexa returns empty results but.
-
#has_next? ⇒ Boolean
Always a boolean — it used to yield nil when there was no pagination at all, which is falsy but not
false, and leaks out of any caller that serialises or compares the result. - #inspect ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #next_page ⇒ Object
- #pagination ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Methods inherited from ConexaObject
#==, #[]=, convert, #initialize, #to_hash, #unsaved_attributes
Constructor Details
This class inherits a constructor from Conexa::ConexaObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/conexa/resources/result.rb', line 66 def method_missing(name, *args, &block) name = Util.to_snake_case(name.to_s) if @attributes["data"] && @attributes["data"].respond_to?(name) && args != ["data"] return @attributes["data"].public_send name, *args, &block end unless block_given? if name.end_with?('=') && args.size == 1 attribute_name = name[0...-1] return self[attribute_name] = args[0] end if args.size == 0 if @attributes.key?(name) return @attributes[name] elsif @attributes.key?(name.to_sym) return @attributes[name.to_sym] end return nil end end if attributes.respond_to? name return attributes.public_send name, *args, &block end super name, *args, &block end |
Instance Method Details
#empty? ⇒ Boolean
Delegate empty? to data array instead of checking @attributes This fixes the case where Conexa returns empty results but
13 14 15 |
# File 'lib/conexa/resources/result.rb', line 13 def empty? data.nil? || data.empty? end |
#has_next? ⇒ Boolean
Returns always a boolean — it used to yield nil when there was no
pagination at all, which is falsy but not false, and leaks out of any
caller that serialises or compares the result.
24 25 26 |
# File 'lib/conexa/resources/result.rb', line 24 def has_next? pagination.respond_to?(:has_next) && pagination.has_next == true end |
#inspect ⇒ Object
6 7 8 |
# File 'lib/conexa/resources/result.rb', line 6 def inspect self.data.inspect end |
#next_page ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/conexa/resources/result.rb', line 28 def next_page raise StopIteration, "No more pages" unless has_next? raise Conexa::RequestError, "No query context available for next_page" unless @query_context limit = pagination.limit offset = pagination.offset unless limit.is_a?(Integer) && offset.is_a?(Integer) raise Conexa::ResponseError.new(@query_context[:params], nil, "pagination is missing limit/offset " \ "(limit=#{limit.inspect}, offset=#{offset.inspect}), " \ "so the next page cannot be computed") end resource_class = @query_context[:resource_class] begin next_params = Marshal.load(Marshal.dump(@query_context[:params])) rescue TypeError # A non-marshallable filter (a Proc, an IO) in the original query. next_params = @query_context[:params].dup end next_params[:limit] = limit next_params[:offset] = offset + limit next_params.delete(:page) next_params.delete(:size) resource_class.find_by(next_params) end |
#pagination ⇒ Object
17 18 19 |
# File 'lib/conexa/resources/result.rb', line 17 def pagination @attributes["pagination"] end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/conexa/resources/result.rb', line 58 def respond_to_missing?(name, include_private = false) name_str = Util.to_snake_case(name.to_s) return true if name_str.end_with?('=') return true if @attributes["data"]&.respond_to?(name_str) @attributes.key?(name_str) || @attributes.key?(name_str.to_sym) || super end |