Module: Fortnox::API::Repository::Loaders

Included in:
Base
Defined in:
lib/fortnox/api/repositories/base/loaders.rb

Instance Method Summary collapse

Instance Method Details

#allObject



8
9
10
11
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 8

def all()
  response_hash = get( self.class::URI )
  instantiate_collection_response( response_hash )
end

#escape(key, value) ⇒ Object



52
53
54
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 52

def escape( key, value )
  "#{ CGI.escape(key.to_s) }=#{ CGI.escape(value.to_s) }"
end

#find(id_or_hash) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 26

def find( id_or_hash )
  return find_all_by( id_or_hash ) if id_or_hash.is_a? Hash

  id = Integer( id_or_hash )
  find_one_by( id )

rescue ArgumentError
  raise ArgumentError, "find only accepts a number or hash as argument"
end

#find_all_by(hash) ⇒ Object



41
42
43
44
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 41

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



36
37
38
39
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 36

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



13
14
15
16
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 13

def only( filter )
  response_hash = get( "#{ self.class::URI }?filter=#{ filter }" )
  instantiate_collection_response( response_hash )
end

#search(hash) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 18

def search( hash )
  attribute, value = hash.first
  uri_encoded_value = URI.encode(value)
  uri = "#{ self.class::URI }?#{ attribute }=#{ uri_encoded_value }".freeze
  response_hash = get( uri )
  instantiate_collection_response( response_hash )
end

#to_query(hash) ⇒ Object



46
47
48
49
50
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 46

def to_query( hash )
  hash.collect do |key, value|
    escape( key, value )
  end.sort * '&'
end