class Billomat::Search
This class provides the possibility to query the resources
Public Class Methods
new(resource, hash)
click to toggle source
Creates a new search object
@param [Class] resource The resource class to be queried @param [Hash] hash The query
# File lib/billomat/search.rb, line 14 def initialize(resource, hash) @resource = resource @hash = hash end
Public Instance Methods
count(resp)
click to toggle source
@param [Hash] resp The response from the gateway @return [Integer] The number of records found
# File lib/billomat/search.rb, line 63 def count(resp) return 0 if resp.nil? resp["#{name}s"]['@total'].to_i end
name()
click to toggle source
@return [String] The name of the resource
# File lib/billomat/search.rb, line 57 def name @resource.resource_name end
path()
click to toggle source
@return [String] The path including the query
# File lib/billomat/search.rb, line 20 def path "#{@resource.base_path}?#{hash_to_query}" end
run()
click to toggle source
Runs the query and calls the gateway Currently it will always return an empty array when no query is provided
@return [Array<Billomat::Model::Base>]
# 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)
click to toggle source
Corrects the response to always return an array
@todo Due to a strange API behaviour we have to fix the reponse here.
This may be fixed in a new API version.
@param [Hash] resp The response from the gateway @return [Array<Billomat::Model::Base>]
# 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
Private Instance Methods
hash_to_query()
click to toggle source
@return [String] The query as www encoded string
# File lib/billomat/search.rb, line 71 def hash_to_query URI.encode_www_form(@hash) end