Module: Fortnox::API::Repository::Loaders
- Included in:
- Base
- Defined in:
- lib/fortnox/api/repositories/base/loaders.rb
Instance Method Summary collapse
- #all ⇒ Object
- #escape(key, value) ⇒ Object
- #find(id_or_hash) ⇒ Object
- #find_all_by(hash) ⇒ Object
- #find_one_by(id) ⇒ Object
- #only(filter) ⇒ Object
- #search(hash) ⇒ Object
- #to_query(hash) ⇒ Object
Instance Method Details
#all ⇒ Object
9 10 11 12 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 9 def all response_hash = get(self.class::URI) instantiate_collection_response(response_hash) end |
#escape(key, value) ⇒ Object
49 50 51 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 49 def escape(key, value) "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" end |
#find(id_or_hash) ⇒ Object
27 28 29 30 31 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 27 def find(id_or_hash) return find_all_by(id_or_hash) if id_or_hash.is_a? Hash find_one_by(id_or_hash) end |
#find_all_by(hash) ⇒ Object
38 39 40 41 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 38 def find_all_by(hash) response_hash = get("#{self.class::URI}?#{to_query(hash)}") instantiate_collection_response(response_hash) end |
#find_one_by(id) ⇒ Object
33 34 35 36 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 33 def find_one_by(id) response_hash = get("#{self.class::URI}#{id}") instantiate(@mapper.wrapped_json_hash_to_entity_hash(response_hash)) end |
#only(filter) ⇒ Object
14 15 16 17 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 14 def only(filter) response_hash = get("#{self.class::URI}?filter=#{filter}") instantiate_collection_response(response_hash) end |
#search(hash) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 19 def search(hash) attribute, value = hash.first uri_encoded_value = CGI.escape(value) uri = "#{self.class::URI}?#{attribute}=#{uri_encoded_value}" response_hash = get(uri) instantiate_collection_response(response_hash) end |
#to_query(hash) ⇒ Object
43 44 45 46 47 |
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 43 def to_query(hash) hash.collect do |key, value| escape(key, value) end.sort * '&' end |