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 )
  instansiate_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



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

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_one_by(id) ⇒ Object



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

def find_one_by( id )
  response_hash = get( "#{ self.class::URI }#{ id }" )
  instansiate( @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 }" )
  instansiate_collection_response( response_hash )
end

#search(hash) ⇒ Object



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

def search( hash )
  attribute, value = hash.first
  response_hash = get( "#{ self.class::URI }?#{ attribute }=#{ value }" )
  instansiate_collection_response( response_hash )
end

#to_query(hash) ⇒ Object

end



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