Module: Kiva
- Defined in:
- lib/kiva.rb
Overview
The Kiva module is a namespace to contain all the classes and contains some utility functions to handle json and http.
Defined Under Namespace
Classes: Comment, Filter, JournalEntry, JournalFilter, Lender, LendingAction, Loan, LoanFilter, Partner, Release, Templates
Class Method Summary collapse
-
._fill(instance, hash) ⇒ Object
INTERNAL.
-
._populate(clazz, array) ⇒ Object
INTERNAL.
-
.execute(url, query = nil) ⇒ Object
This is central location where the webservice query is performed.
Class Method Details
._fill(instance, hash) ⇒ Object
INTERNAL
Utility to transfer JSON structs to 'real' classes.
Takes an instance of a class and a hash.
for each key in hash, we check if the instance
has a corresponding setter, if so, we assign the
value of the key to the instance.
E.G.
If the hash is:
{ :one => "bla", :two => "blo", :three => "blu"}
And an instance if of class:
class Example
attr_accessor :one
attr_accessor :three
end
`_fill` would set example.one="bla" and example.three="blu"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kiva.rb', line 54 def Kiva._fill instance, hash hash.keys.each { |key| meth = (key+"=").to_sym next unless instance.respond_to? meth if ["loan", "lender"].include?(key) value = ("loan"==key ? Kiva::Loan.new : Kiva::Lender.new) Kiva._fill value, hash[key] elsif key =~ /date/ begin require 'time' value = Time.parse(hash[key], -1) rescue value = hash[key] end else value = hash[key] end instance.__send__ meth, value } return instance end |
._populate(clazz, array) ⇒ Object
INTERNAL
Utility to transfer JSON structures to normal ruby arrays.
array contains an array of parsed JSON structs, e.g.
[{"one"=>3},{"one"=>5},{"one"=>6}]
given the name of a Class in the ‘clazz` param, this method will instantiate one clazz per JSON struct in `array` and attempt to populate the class. (see `_fill` above)
91 92 93 94 95 96 97 98 99 |
# File 'lib/kiva.rb', line 91 def Kiva._populate clazz, array res = [] array.each{ |hash| instance = clazz.new Kiva._fill instance, hash res.push instance } res end |
.execute(url, query = nil) ⇒ Object
This is central location where the webservice query is performed. You probably won’t need to change this, but in case you you need special error handling or some other behaviour, you can modify everything in one place HERE.
For examples on how to do this, have a look at the files: ‘test/generate_fixtures.rb` and `test/test_kiva.rb`.
Error Handling
currently the normal HttpExceptions are just passed on, which should be ok for starters.
23 24 25 26 27 |
# File 'lib/kiva.rb', line 23 def Kiva.execute url, query=nil #puts url #pp(query) if query SimpleHttp.get(url, query) end |