Class: Billomat::Search
- Inherits:
-
Object
- Object
- Billomat::Search
- Defined in:
- lib/billomat/search.rb
Overview
This class provides the possibility to query the resources
Instance Method Summary collapse
-
#count(resp) ⇒ Integer
The number of records found.
-
#initialize(resource, hash) ⇒ Search
constructor
Creates a new search object.
-
#name ⇒ String
The name of the resource.
-
#path ⇒ String
The path including the query.
-
#run ⇒ Array<Billomat::Model::Base>
Runs the query and calls the gateway Currently it will always return an empty array when no query is provided.
-
#to_array(resp) ⇒ Array<Billomat::Model::Base>
Corrects the response to always return an array.
Constructor Details
#initialize(resource, hash) ⇒ Search
Creates a new search object
14 15 16 17 |
# File 'lib/billomat/search.rb', line 14 def initialize(resource, hash) @resource = resource @hash = hash end |
Instance Method Details
#count(resp) ⇒ Integer
Returns The number of records found.
63 64 65 66 |
# File 'lib/billomat/search.rb', line 63 def count(resp) return 0 if resp.nil? resp["#{name}s"]['@total'].to_i end |
#name ⇒ String
Returns The name of the resource.
57 58 59 |
# File 'lib/billomat/search.rb', line 57 def name @resource.resource_name end |
#path ⇒ String
Returns The path including the query.
20 21 22 |
# File 'lib/billomat/search.rb', line 20 def path "#{@resource.base_path}?#{hash_to_query}" end |
#run ⇒ Array<Billomat::Model::Base>
Runs the query and calls the gateway Currently it will always return an empty array when no query is provided
29 30 31 32 |
# File 'lib/billomat/search.rb', line 29 def run return [] if @hash.reject { |k, v| v.nil? }.empty? to_array(Billomat::Gateway.new(:get, path).run) end |
#to_array(resp) ⇒ Array<Billomat::Model::Base>
TODO:
Due to a strange API behaviour we have to fix the reponse here. This may be fixed in a new API version.
Corrects the response to always return an array
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/billomat/search.rb', line 42 def to_array(resp) case count(resp) when 0 [] when 1 # Necessary due to strange API behaviour [@resource.new(resp["#{name}s"][name])] else resp["#{name}s"][name].map do |c| @resource.new(c) end end end |