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.
-
#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.
10 11 12 13 |
# File 'lib/billomat/search.rb', line 10 def initialize(resource, hash) @resource = resource @hash = hash end |
Instance Method Details
#count(resp) ⇒ Integer
Returns the number of records found.
56 57 58 59 60 |
# File 'lib/billomat/search.rb', line 56 def count(resp) return 0 if resp.nil? resp["#{name}s"]['@total'].to_i end |
#name ⇒ String
Returns the name of the resource.
50 51 52 |
# File 'lib/billomat/search.rb', line 50 def name @resource.resource_name end |
#path ⇒ String
Returns the path including the query.
16 17 18 |
# File 'lib/billomat/search.rb', line 16 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.
24 25 26 27 28 |
# File 'lib/billomat/search.rb', line 24 def run return [] if @hash.compact.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 response here. This may be fixed in a new API version.
Corrects the response to always return an array.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/billomat/search.rb', line 37 def to_array(resp) data = resp["#{name}s"][name] case data when Hash [@resource.new(data)] when Array data.map { |d| @resource.new(d) } else [] end end |