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
12
13
14
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 8

def all()
  response_hash = get( @options.uri )
  entities_hash = response_hash[ @options.json_collection_wrapper ]
  entities_hash.map do |entity_hash|
    hash_to_entity( entity_hash )
  end
end

#escape(key, value) ⇒ Object



50
51
52
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 50

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
38
# File 'lib/fortnox/api/repositories/base/loaders.rb', line 34

def find_one_by( id )
  response_hash = get( "#{@options.uri}#{id}" )
  entity_hash = response_hash[ @options.json_entity_wrapper ]
  hash_to_entity( entity_hash )
end

#only(filter) ⇒ Object



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

def only( filter )
  response_hash = get( @options.uri + "?filter=#{ filter }" )
  entities_hash = response_hash[ @options.json_collection_wrapper ]
  entities_hash.map do |entity_hash|
    hash_to_entity( entity_hash )
  end
end

#to_query(hash) ⇒ Object

end



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

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