kiva 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORS +1 -0
- data/CHANGELOG +5 -0
- data/COPYING +722 -0
- data/LICENSE +58 -0
- data/README +33 -0
- data/Rakefile +128 -0
- data/THANKS +4 -0
- data/TODO +10 -0
- data/lib/kiva.rb +650 -0
- data/test/fixtures.rbf +21 -0
- data/test/generate_fixtures.rb +30 -0
- data/test/test_kiva.rb +167 -0
- metadata +81 -0
data/LICENSE
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
This package is copyrighted free software by Tim Becker <tim@kuriositaet.de>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the GPL
|
3
|
+
(see COPYING.txt file), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) rename any non-standard executables so the names do not conflict
|
21
|
+
with standard executables, which must also be provided.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or executable
|
26
|
+
form, provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the executables and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c) give non-standard executables non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under this terms.
|
43
|
+
|
44
|
+
They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
|
45
|
+
files under the ./missing directory. See each file for the copying
|
46
|
+
condition.
|
47
|
+
|
48
|
+
5. The scripts and library files supplied as input to or produced as
|
49
|
+
output from the software do not automatically fall under the
|
50
|
+
copyright of the software, but belong to whomever generated them,
|
51
|
+
and may be sold commercially, and may be aggregated with this
|
52
|
+
software.
|
53
|
+
|
54
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
55
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
56
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
57
|
+
PURPOSE.
|
58
|
+
|
data/README
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= kiva -- wrapper to the kiva rest API
|
2
|
+
|
3
|
+
You can find more information about the API here[http://developers.wiki.kiva.org/KivaAPI]
|
4
|
+
|
5
|
+
== Installing
|
6
|
+
|
7
|
+
You have to deal with this yourself. Everything seems to work, but it still needs to be
|
8
|
+
tested and gemified, made beautiful and all the other boring stuff.
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
== Mailing List
|
13
|
+
|
14
|
+
In case you discover bugs, spelling errors, offer suggestions for
|
15
|
+
improvements or would like to help out with the project, you can contact
|
16
|
+
me directly (tim@kuriositaet.de).
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
=
|
data/Rakefile
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require "rake/rdoctask"
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require "rake/testtask"
|
4
|
+
require "rake/clean"
|
5
|
+
require "rubygems"
|
6
|
+
|
7
|
+
# Some definitions that you'll need to edit in case you reuse this
|
8
|
+
# Rakefile for your own project.
|
9
|
+
|
10
|
+
SHORTNAME ='kiva' # this should be the rubyforge project name
|
11
|
+
DESC = 'wrapper to kiva api'
|
12
|
+
PKG_VERSION ='0.0.1'
|
13
|
+
LONG_DESC = <<END_DESC
|
14
|
+
Wrapper to the kiva web api. The API is described in
|
15
|
+
more detail here:
|
16
|
+
http://developers.wiki.kiva.org/KivaAPI
|
17
|
+
END_DESC
|
18
|
+
RUBYFORGE_USER ='a2800276'
|
19
|
+
|
20
|
+
# Specifies the default task to execute. This is often the "test" task
|
21
|
+
|
22
|
+
task :default => [:test]
|
23
|
+
|
24
|
+
# The directory to generate +rdoc+ in.
|
25
|
+
RDOC_DIR="doc/html"
|
26
|
+
|
27
|
+
# This global variable contains files that will be erased by the `clean` task.
|
28
|
+
# The `clean` task itself is automatically generated by requiring `rake/clean`.
|
29
|
+
|
30
|
+
CLEAN << RDOC_DIR << "pkg"
|
31
|
+
|
32
|
+
|
33
|
+
# This is the task that generates the +rdoc+ documentation from the
|
34
|
+
# source files. Instantiating Rake::RDocTask automatically generates a
|
35
|
+
# task called `rdoc`.
|
36
|
+
|
37
|
+
Rake::RDocTask.new do |rd|
|
38
|
+
# Options for documenation generation are specified inside of
|
39
|
+
# this block. For example the following line specifies that the
|
40
|
+
# content of the README file should be the main page of the
|
41
|
+
# documenation.
|
42
|
+
rd.main = "README"
|
43
|
+
|
44
|
+
# The following line specifies all the files to extract
|
45
|
+
# documenation from.
|
46
|
+
rd.rdoc_files.include( "README", "AUTHORS", "LICENSE", "TODO",
|
47
|
+
"CHANGELOG", "bin/**/*", "lib/**/*.rb",
|
48
|
+
"examples/**/*rb","test/**/*.rb", "doc/*.rdoc")
|
49
|
+
# This one specifies the output directory ...
|
50
|
+
rd.rdoc_dir = "doc/html"
|
51
|
+
|
52
|
+
# Or the HTML title of the generated documentation set.
|
53
|
+
rd.title = "#{SHORTNAME}: #{DESC}"
|
54
|
+
|
55
|
+
# These are options specifiying how source code inlined in the
|
56
|
+
# documentation should be formatted.
|
57
|
+
|
58
|
+
rd.options = ["--line-numbers", "--inline-source"]
|
59
|
+
|
60
|
+
# Check:
|
61
|
+
# `rdoc --help` for more rdoc options
|
62
|
+
# the {rdoc documenation home}[http://www.ruby-doc.org/stdlib/libdoc/rdoc/rdoc/index.html]
|
63
|
+
# or the documentation for the +Rake::RDocTask+ task[http://rake.rubyforge.org/classes/Rake/RDocTask.html]
|
64
|
+
end
|
65
|
+
|
66
|
+
# The GemPackageTask facilitates getting all your files collected
|
67
|
+
# together into gem archives. You can also use it to generate tarball
|
68
|
+
# and zip archives.
|
69
|
+
|
70
|
+
# First you'll need to assemble a gemspec
|
71
|
+
|
72
|
+
PKG_FILES = FileList['lib/**/*.rb', 'bin/**/*', 'examples/**/*', '[A-Z]*', 'test/**/*'].to_a
|
73
|
+
|
74
|
+
spec = Gem::Specification.new do |s|
|
75
|
+
s.platform = Gem::Platform::RUBY
|
76
|
+
s.summary = "#{SHORTNAME}: #{DESC}"
|
77
|
+
s.name = SHORTNAME
|
78
|
+
s.version = PKG_VERSION
|
79
|
+
s.files = PKG_FILES
|
80
|
+
s.requirements << "none"
|
81
|
+
s.require_path = 'lib'
|
82
|
+
s.description = LONG_DESC
|
83
|
+
s.add_dependency "json"
|
84
|
+
s.add_dependency "simplehttp"
|
85
|
+
end
|
86
|
+
|
87
|
+
# Adding a new GemPackageTask adds a task named `package`, which generates
|
88
|
+
# packages as gems, tarball and zip archives.
|
89
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
90
|
+
pkg.need_zip = true
|
91
|
+
pkg.need_tar_gz = true
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
# This task is used to demonstrate how to upload files to Rubyforge.
|
96
|
+
# Calling `upload_page` creates a current version of the +rdoc+
|
97
|
+
# documentation and uploads it to the Rubyforge homepage of the project,
|
98
|
+
# assuming it's hosted there and naming conventions haven't changed.
|
99
|
+
#
|
100
|
+
# This task uses `sh` to call the `scp` binary, which is plattform
|
101
|
+
# dependant and may not be installed on your computer if you're using
|
102
|
+
# Windows. I'm currently not aware of any pure ruby way to do scp
|
103
|
+
# transfers.
|
104
|
+
|
105
|
+
RubyForgeProject=SHORTNAME
|
106
|
+
|
107
|
+
desc "Upload the web pages to the web."
|
108
|
+
task :upload_pages => ["rdoc"] do
|
109
|
+
if RubyForgeProject then
|
110
|
+
path = "/var/www/gforge-projects/#{RubyForgeProject}"
|
111
|
+
sh "scp -r doc/html/* #{RUBYFORGE_USER}@rubyforge.org:#{path}"
|
112
|
+
sh "scp doc/images/*.png #{RUBYFORGE_USER}@rubyforge.org:#{path}/images"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# This task will run the unit tests provided in files called
|
117
|
+
# `test/test*.rb`. The task itself can be run with a call to `rake test`
|
118
|
+
|
119
|
+
Rake::TestTask.new do |t|
|
120
|
+
t.libs << "test"
|
121
|
+
t.libs << "lib"
|
122
|
+
t.ruby_opts = ["-rubygems"]
|
123
|
+
t.test_files = FileList['test/test_*.rb']
|
124
|
+
t.verbose = true
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
data/THANKS
ADDED
data/TODO
ADDED
data/lib/kiva.rb
ADDED
@@ -0,0 +1,650 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'simplehttp'
|
3
|
+
|
4
|
+
|
5
|
+
module Kiva
|
6
|
+
|
7
|
+
#
|
8
|
+
# central location to perform webservice query
|
9
|
+
# hook into here for error handling, etc.
|
10
|
+
# currently the normal HttpExceptions are just passed
|
11
|
+
# on, which should be ok for starters.
|
12
|
+
#
|
13
|
+
def Kiva.execute url, query=nil
|
14
|
+
#puts url
|
15
|
+
#pp(query) if query
|
16
|
+
SimpleHttp.get(url, query)
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# INTERNAL
|
21
|
+
#
|
22
|
+
# Utility to transfer JSON structs to 'real' classes.
|
23
|
+
#
|
24
|
+
# Takes an instance of a class and a hash.
|
25
|
+
# for each key in hash, we check if the instance
|
26
|
+
# has a corresponding setter, if so, we assign the
|
27
|
+
# value of the key to the instance.
|
28
|
+
#
|
29
|
+
# E.G.
|
30
|
+
#
|
31
|
+
# If the hash is:
|
32
|
+
#
|
33
|
+
# { :one => "bla", :two => "blo", :three => "blu"}
|
34
|
+
#
|
35
|
+
# And an instance if of class:
|
36
|
+
#
|
37
|
+
# class Example
|
38
|
+
# attr_accessor :one
|
39
|
+
# attr_accessor :three
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# `_fill` would set example.one="bla" and example.three="blu"
|
43
|
+
#
|
44
|
+
def Kiva._fill instance, hash
|
45
|
+
hash.keys.each { |key|
|
46
|
+
|
47
|
+
meth = (key+"=").to_sym
|
48
|
+
next unless instance.respond_to? meth
|
49
|
+
|
50
|
+
if ["loan", "lender"].include?(key)
|
51
|
+
value = ("loan"==key ? Kiva::Loan.new : Kiva::Lender.new)
|
52
|
+
Kiva._fill value, hash[key]
|
53
|
+
elsif key =~ /date/
|
54
|
+
begin
|
55
|
+
require 'time'
|
56
|
+
value = Time.parse(hash[key], -1)
|
57
|
+
rescue
|
58
|
+
value = hash[key]
|
59
|
+
end
|
60
|
+
else
|
61
|
+
value = hash[key]
|
62
|
+
end
|
63
|
+
|
64
|
+
instance.__send__ meth, value
|
65
|
+
}
|
66
|
+
return instance
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# INTERNAL
|
71
|
+
#
|
72
|
+
# Utility to transfer JSON structures to normal ruby arrays.
|
73
|
+
#
|
74
|
+
# array contains an array of parsed JSON structs, e.g.
|
75
|
+
#
|
76
|
+
# [{"one"=>3},{"one"=>5},{"one"=>6}]
|
77
|
+
#
|
78
|
+
# given the name of a Class in the `clazz` param, this method
|
79
|
+
# will instantiate one clazz per JSON struct in `array` and attempt
|
80
|
+
# to populate the class. (see `_fill` above)
|
81
|
+
def Kiva._populate clazz, array
|
82
|
+
res = []
|
83
|
+
array.each{ |hash|
|
84
|
+
instance = clazz.new
|
85
|
+
Kiva._fill instance, hash
|
86
|
+
res.push instance
|
87
|
+
}
|
88
|
+
res
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
class Loan
|
93
|
+
attr_accessor :id
|
94
|
+
attr_accessor :status
|
95
|
+
attr_accessor :name
|
96
|
+
attr_accessor :posted_date
|
97
|
+
attr_accessor :activity
|
98
|
+
attr_accessor :borrowers
|
99
|
+
attr_accessor :description
|
100
|
+
attr_accessor :terms
|
101
|
+
attr_accessor :partner_id
|
102
|
+
attr_accessor :use
|
103
|
+
attr_accessor :funded_amount
|
104
|
+
attr_accessor :funded_date
|
105
|
+
attr_accessor :image
|
106
|
+
attr_accessor :location
|
107
|
+
attr_accessor :sector
|
108
|
+
attr_accessor :basket_amount
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
class << self
|
113
|
+
|
114
|
+
KEY = "loans"
|
115
|
+
|
116
|
+
LOAD_FOR_LENDER = "http://api.kivaws.org/v1/lenders/%s/loans.json?"
|
117
|
+
LOAD_NEWEST = "http://api.kivaws.org/v1/loans/newest.json?"
|
118
|
+
LOAD = "http://api.kivaws.org/v1/loans/%s.json"
|
119
|
+
SEARCH = "http://api.kivaws.org/v1/loans/search.json"
|
120
|
+
|
121
|
+
|
122
|
+
#
|
123
|
+
# Returns details for one or more loans.
|
124
|
+
#
|
125
|
+
# PARAMS
|
126
|
+
# `ids` : an instance of `Loan` or a loan id or an array `Loan`s/loan IDs
|
127
|
+
#
|
128
|
+
# RETURNS
|
129
|
+
# an array of `Loan` instances
|
130
|
+
#
|
131
|
+
# CORRESPONDS
|
132
|
+
# http://developers.wiki.kiva.org/KivaAPI#loans/ltidsgt
|
133
|
+
#
|
134
|
+
def load ids
|
135
|
+
case ids
|
136
|
+
when Loan
|
137
|
+
ids = ids.id
|
138
|
+
when Array
|
139
|
+
if ids[0].is_a?(Loan)
|
140
|
+
ids = ids.map{|l| l.id}
|
141
|
+
end
|
142
|
+
ids.join(",")
|
143
|
+
end
|
144
|
+
|
145
|
+
url = LOAD % ids
|
146
|
+
|
147
|
+
raw = Kiva.execute url
|
148
|
+
unw = JSON.parse(raw)
|
149
|
+
|
150
|
+
Kiva._populate Loan, unw[KEY]
|
151
|
+
end
|
152
|
+
|
153
|
+
#
|
154
|
+
# Search for loans matching specific criteria.
|
155
|
+
#
|
156
|
+
# PARAMS
|
157
|
+
# `filter` : an instance of `Filter` describing the search parameter
|
158
|
+
#
|
159
|
+
# RETURNS
|
160
|
+
# an array of `Loan` instances
|
161
|
+
#
|
162
|
+
# CORRESPONDS
|
163
|
+
# http://developers.wiki.kiva.org/KivaAPI#loans/search
|
164
|
+
#
|
165
|
+
def search filter, page=nil
|
166
|
+
url = SEARCH
|
167
|
+
if filter && page
|
168
|
+
filter["page"] = page
|
169
|
+
end
|
170
|
+
|
171
|
+
raw = Kiva.execute url, filter.params
|
172
|
+
unw = JSON.parse(raw)
|
173
|
+
|
174
|
+
Kiva._populate Loan, unw[KEY]
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
#
|
179
|
+
# Returns the most recent fundraising loans.
|
180
|
+
#
|
181
|
+
# PARAMS
|
182
|
+
# `page` : the page position
|
183
|
+
#
|
184
|
+
# RETURNS
|
185
|
+
# an array of `Loan` instances
|
186
|
+
#
|
187
|
+
# CORRESPONDS
|
188
|
+
# http://developers.wiki.kiva.org/KivaAPI#loans/newest
|
189
|
+
#
|
190
|
+
def load_newest page=nil
|
191
|
+
url = LOAD_NEWEST
|
192
|
+
url = page ? url + "page=#{page}&" : url
|
193
|
+
|
194
|
+
raw = Kiva.execute url
|
195
|
+
unw = JSON.parse(raw)
|
196
|
+
|
197
|
+
Kiva._populate Loan, unw[KEY]
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
#
|
202
|
+
# Returns loans sponsored by Lender
|
203
|
+
#
|
204
|
+
# PARAMS
|
205
|
+
# `id` : id of lender or instance of `Lender`
|
206
|
+
# `sort_by`: one of :newest, :oldest
|
207
|
+
# `page` : page position
|
208
|
+
#
|
209
|
+
# RETURNS
|
210
|
+
# array of `Loan`s
|
211
|
+
#
|
212
|
+
# CORRESPONDS
|
213
|
+
# http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidgt/loans
|
214
|
+
#
|
215
|
+
|
216
|
+
def load_for_lender id, sort_by=nil, page=nil
|
217
|
+
id = id.uid if id.is_a?(Lender)
|
218
|
+
|
219
|
+
url = LOAD_FOR_LENDER % id
|
220
|
+
url = page ? url + "page=#{page}&" : url
|
221
|
+
url = [:newest, :oldest].include?(sort_by) ? url + "sort_by=#{sort_by}" : url
|
222
|
+
|
223
|
+
raw = Kiva.execute url
|
224
|
+
unw = JSON.parse(raw)
|
225
|
+
|
226
|
+
|
227
|
+
Kiva._populate Loan, unw[KEY]
|
228
|
+
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
class Lender
|
235
|
+
# url = "http://api.kivaws.org/v1/lenders/%s.json"
|
236
|
+
attr_accessor :uid
|
237
|
+
attr_accessor :lender_id
|
238
|
+
attr_accessor :name
|
239
|
+
attr_accessor :loan_count
|
240
|
+
attr_accessor :occupation
|
241
|
+
attr_accessor :country_code
|
242
|
+
attr_accessor :loan_because
|
243
|
+
attr_accessor :invitee_count
|
244
|
+
attr_accessor :occupational_info
|
245
|
+
attr_accessor :personal_url
|
246
|
+
attr_accessor :whereabouts
|
247
|
+
attr_accessor :image
|
248
|
+
attr_accessor :member_since
|
249
|
+
|
250
|
+
class << self
|
251
|
+
KEY = "lenders"
|
252
|
+
|
253
|
+
LOAD = "http://api.kivaws.org/v1/lenders/%s.json"
|
254
|
+
LOAD_FOR_LOAN = "http://api.kivaws.org/v1/loans/%s/lenders.json?"
|
255
|
+
|
256
|
+
|
257
|
+
#
|
258
|
+
# List public lenders of a loan.
|
259
|
+
#
|
260
|
+
# PARAMS
|
261
|
+
# `loan` : a loan id or an instance of `Loan`
|
262
|
+
#
|
263
|
+
# RETURNS
|
264
|
+
# an array of `Lender` instances.
|
265
|
+
#
|
266
|
+
# CORRESPONDS TO
|
267
|
+
# http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/lenders
|
268
|
+
#
|
269
|
+
def load_for_loan loan, page = nil
|
270
|
+
loan = loan.id if loan.is_a?(Loan)
|
271
|
+
|
272
|
+
url = LOAD_FOR_LOAN % loan
|
273
|
+
|
274
|
+
url = page ? url + "page=#{page}&" : url
|
275
|
+
|
276
|
+
raw = Kiva.execute(url)
|
277
|
+
unw = JSON.parse(raw)
|
278
|
+
|
279
|
+
Kiva._populate Lender, unw[KEY]
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
#
|
284
|
+
# Load details for one or more Lenders.
|
285
|
+
#
|
286
|
+
# PARAMS
|
287
|
+
# `ids` : a lender id or and array of id's
|
288
|
+
#
|
289
|
+
# RETURNS
|
290
|
+
# an array of `Lender` instances.
|
291
|
+
#
|
292
|
+
# CORRESPONDS TO
|
293
|
+
# http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidsgt
|
294
|
+
#
|
295
|
+
def load ids
|
296
|
+
ids = ids.join(",") if ids.is_a?(Array)
|
297
|
+
url = LOAD % ids
|
298
|
+
raw = Kiva.execute(url)
|
299
|
+
unw = JSON.parse(raw)
|
300
|
+
|
301
|
+
Kiva._populate Lender, unw[KEY]
|
302
|
+
end
|
303
|
+
|
304
|
+
end
|
305
|
+
|
306
|
+
end
|
307
|
+
|
308
|
+
class LendingAction
|
309
|
+
attr_accessor :id
|
310
|
+
attr_accessor :date
|
311
|
+
attr_accessor :lender
|
312
|
+
attr_accessor :loan
|
313
|
+
|
314
|
+
class << self
|
315
|
+
KEY = "lending_actions"
|
316
|
+
LOAD_RECENT = "http://api.kivaws.org/v1/lending_actions/recent.json"
|
317
|
+
|
318
|
+
#
|
319
|
+
# Returns the last 100 public actions from Kiva.
|
320
|
+
#
|
321
|
+
# RETURNS
|
322
|
+
# an array of `LendingAction` instances
|
323
|
+
#
|
324
|
+
# CORRESPONDS
|
325
|
+
# http://developers.wiki.kiva.org/KivaAPI#lendingactions/recent
|
326
|
+
#
|
327
|
+
def load
|
328
|
+
raw = Kiva.execute(LOAD_RECENT)
|
329
|
+
unw = JSON.parse(raw)
|
330
|
+
|
331
|
+
Kiva._populate LendingAction, unw[KEY]
|
332
|
+
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
class JournalEntry
|
338
|
+
attr_accessor :id
|
339
|
+
attr_accessor :body
|
340
|
+
attr_accessor :date
|
341
|
+
attr_accessor :comment_count
|
342
|
+
attr_accessor :author
|
343
|
+
attr_accessor :subject
|
344
|
+
attr_accessor :bulk
|
345
|
+
attr_accessor :image
|
346
|
+
attr_accessor :recommendation_count
|
347
|
+
|
348
|
+
class << self
|
349
|
+
KEY = "journal__entries"
|
350
|
+
LOAD = "http://api.kivaws.org/v1/loans/%s/journal_entries.json?"
|
351
|
+
|
352
|
+
#
|
353
|
+
# Load journal entries for a loan.
|
354
|
+
#
|
355
|
+
# PARAMS
|
356
|
+
# `id` : a loan id or an instance of `Loan`
|
357
|
+
#
|
358
|
+
# RETURNS
|
359
|
+
# an array of `JournalEntry` instances.
|
360
|
+
#
|
361
|
+
# CORRESPONDS TO
|
362
|
+
# http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/journalentries
|
363
|
+
#
|
364
|
+
def load id, page = nil, include_bulk = nil
|
365
|
+
id = id.id if id.is_a? Loan
|
366
|
+
url = LOAD % id
|
367
|
+
|
368
|
+
url = page ? url + "page=#{page}&" : url
|
369
|
+
url = (! include_bulk.nil?) ? url + "include_bulk=#{include_bulk}" : url
|
370
|
+
|
371
|
+
raw = raw = Kiva.execute(url)
|
372
|
+
unw = JSON.parse(raw)
|
373
|
+
Kiva._populate JournalEntry, unw[KEY]
|
374
|
+
|
375
|
+
end
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
class Comment
|
380
|
+
# http://api.kivaws.org/v1/journal_entries/<id>/comments.json
|
381
|
+
attr_accessor :body
|
382
|
+
attr_accessor :date
|
383
|
+
attr_accessor :author
|
384
|
+
attr_accessor :id
|
385
|
+
attr_accessor :whereabouts
|
386
|
+
|
387
|
+
class << self
|
388
|
+
KEY = "comments"
|
389
|
+
URL = "http://api.kivaws.org/v1/journal_entries/%s/comments.json?"
|
390
|
+
|
391
|
+
#
|
392
|
+
# Loads an array of comments for a JournalEntry.
|
393
|
+
#
|
394
|
+
# `id` : the numerical id of a Journal Entry or an instance of
|
395
|
+
# the class `JournalEntry`
|
396
|
+
# `page`: which page of comments to load, default is the first.
|
397
|
+
#
|
398
|
+
# Corresponds to the API:
|
399
|
+
#
|
400
|
+
# http://developers.wiki.kiva.org/KivaAPI#journalentries/ltidgt/comments
|
401
|
+
#
|
402
|
+
def load id, page=nil
|
403
|
+
id = id.id if id.is_a?(JournalEntry)
|
404
|
+
|
405
|
+
url = URL % id
|
406
|
+
url = page ? url + "page=#{page}&" : url
|
407
|
+
|
408
|
+
raw = raw = Kiva.execute(url)
|
409
|
+
unw = JSON.parse(raw)
|
410
|
+
Kiva._populate Comment, unw[KEY]
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
class Partner
|
416
|
+
attr_accessor :start_date
|
417
|
+
attr_accessor :rating
|
418
|
+
attr_accessor :status
|
419
|
+
attr_accessor :name
|
420
|
+
attr_accessor :delinquency_rate
|
421
|
+
attr_accessor :id
|
422
|
+
attr_accessor :total_amount_raised
|
423
|
+
attr_accessor :default_rate
|
424
|
+
attr_accessor :loans_posted
|
425
|
+
attr_accessor :countries
|
426
|
+
attr_accessor :image
|
427
|
+
|
428
|
+
class << self
|
429
|
+
KEY = "partners"
|
430
|
+
LOAD = "http://api.kivaws.org/v1/partners.json?"
|
431
|
+
|
432
|
+
#
|
433
|
+
# Load an alphabetically sorted list of partners.
|
434
|
+
#
|
435
|
+
# PARAMS
|
436
|
+
# `page`: page position
|
437
|
+
#
|
438
|
+
# RETURNS
|
439
|
+
# an array of `Partner` instances
|
440
|
+
#
|
441
|
+
# CORRESPONDS
|
442
|
+
# http://developers.wiki.kiva.org/KivaAPI#partners
|
443
|
+
#
|
444
|
+
|
445
|
+
def load page=nil
|
446
|
+
url = page ? LOAD + "page=#{page}" : LOAD
|
447
|
+
raw = raw = Kiva.execute(url)
|
448
|
+
unw = JSON.parse(raw)
|
449
|
+
|
450
|
+
Kiva._populate self, unw[KEY]
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
class Release
|
456
|
+
|
457
|
+
attr_accessor :date
|
458
|
+
attr_accessor :id
|
459
|
+
|
460
|
+
class << self
|
461
|
+
#
|
462
|
+
# Returns release information
|
463
|
+
#
|
464
|
+
#
|
465
|
+
# RETURNS
|
466
|
+
# an array of `Partner` instances
|
467
|
+
#
|
468
|
+
# CORRESPONDS
|
469
|
+
# http://developers.wiki.kiva.org/KivaAPI#releases/api/current
|
470
|
+
#
|
471
|
+
|
472
|
+
def load
|
473
|
+
url = "http://api.kivaws.org/v1/releases/api/current.json"
|
474
|
+
raw = JSON.parse(Kiva.execute(url))
|
475
|
+
Kiva._fill(self.new, raw["release"])
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
class Templates
|
481
|
+
attr_accessor :id
|
482
|
+
attr_accessor :pattern
|
483
|
+
class << self
|
484
|
+
def load
|
485
|
+
url = "http://api.kivaws.org/v1/templates/images.json"
|
486
|
+
unw = JSON.parse(Kiva.execute(url))
|
487
|
+
Kiva._populate self, unw["templates"]
|
488
|
+
end
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
class Filter
|
493
|
+
attr_accessor :params
|
494
|
+
def initialize
|
495
|
+
@params = {}
|
496
|
+
end
|
497
|
+
|
498
|
+
#sort_by
|
499
|
+
def popularity
|
500
|
+
@params["sort_by"]="popularity"
|
501
|
+
return self
|
502
|
+
end
|
503
|
+
def loan_amount
|
504
|
+
@params["sort_by"]="loan_amount"
|
505
|
+
return self
|
506
|
+
end
|
507
|
+
def oldest
|
508
|
+
@params["sort_by"]="oldest"
|
509
|
+
return self
|
510
|
+
end
|
511
|
+
def expiration
|
512
|
+
@params["sort_by"]="expiration"
|
513
|
+
return self
|
514
|
+
end
|
515
|
+
def newest
|
516
|
+
@params["sort_by"]="newest"
|
517
|
+
return self
|
518
|
+
end
|
519
|
+
def amount_remaining
|
520
|
+
@params["sort_by"]="amount_remaining"
|
521
|
+
return self
|
522
|
+
end
|
523
|
+
def repayment_term
|
524
|
+
@params["sort_by"]="repayment_term"
|
525
|
+
return self
|
526
|
+
end
|
527
|
+
|
528
|
+
# GENDER
|
529
|
+
def male
|
530
|
+
@params["gender"]="male"
|
531
|
+
return self
|
532
|
+
end
|
533
|
+
def female
|
534
|
+
@params["gender"]="female"
|
535
|
+
return self
|
536
|
+
end
|
537
|
+
|
538
|
+
# STATUS
|
539
|
+
def fundraising
|
540
|
+
@params["status"]="fundraising"
|
541
|
+
return self
|
542
|
+
end
|
543
|
+
def funded
|
544
|
+
@params["status"]="funded"
|
545
|
+
return self
|
546
|
+
end
|
547
|
+
def in_repayment
|
548
|
+
@params["status"]="in_repayment"
|
549
|
+
return self
|
550
|
+
end
|
551
|
+
def paid
|
552
|
+
@params["status"]="paid"
|
553
|
+
return self
|
554
|
+
end
|
555
|
+
def defaulted
|
556
|
+
@params["status"]="defaulted"
|
557
|
+
return self
|
558
|
+
end
|
559
|
+
|
560
|
+
#REGION
|
561
|
+
|
562
|
+
def north_america
|
563
|
+
@params["region"]="na"
|
564
|
+
return self
|
565
|
+
end
|
566
|
+
def central_america
|
567
|
+
@params["region"]="ca"
|
568
|
+
return self
|
569
|
+
end
|
570
|
+
def south_america
|
571
|
+
@params["region"]="sa"
|
572
|
+
return self
|
573
|
+
end
|
574
|
+
def africa
|
575
|
+
@params["region"]="af"
|
576
|
+
return self
|
577
|
+
end
|
578
|
+
def asia
|
579
|
+
@params["region"]="as"
|
580
|
+
return self
|
581
|
+
end
|
582
|
+
def middle_east
|
583
|
+
@params["region"]="me"
|
584
|
+
return self
|
585
|
+
end
|
586
|
+
def eastern_europe
|
587
|
+
@params["region"]="ee"
|
588
|
+
return self
|
589
|
+
end
|
590
|
+
|
591
|
+
|
592
|
+
def sector= sector
|
593
|
+
@params["sector"]=sector
|
594
|
+
end
|
595
|
+
def country_code= iso_2
|
596
|
+
@params["country_code"]=iso_2
|
597
|
+
end
|
598
|
+
|
599
|
+
#def partner
|
600
|
+
# too lazy
|
601
|
+
#end
|
602
|
+
|
603
|
+
def query= query
|
604
|
+
@params["q"]=query
|
605
|
+
end
|
606
|
+
|
607
|
+
|
608
|
+
end
|
609
|
+
|
610
|
+
|
611
|
+
end
|
612
|
+
|
613
|
+
if $0 == __FILE__
|
614
|
+
url = "http://api.kivaws.org/v1/partners.json?"
|
615
|
+
userid = "14077"
|
616
|
+
#
|
617
|
+
# result = SimpleHttp.get(url % userid)
|
618
|
+
#
|
619
|
+
# #puts(result)
|
620
|
+
#
|
621
|
+
# puts ('-----------')
|
622
|
+
#
|
623
|
+
# results = JSON.parse(result)
|
624
|
+
# pp results
|
625
|
+
#
|
626
|
+
#
|
627
|
+
# results["partners"][0].keys.each { |key|
|
628
|
+
# puts "attr_accessor :#{key}"
|
629
|
+
# }
|
630
|
+
#user = Kiva::Lender.load("tim9918")
|
631
|
+
#pp user
|
632
|
+
user = "tim9918"
|
633
|
+
#loans = Kiva::Loan.load_for_lender(user)
|
634
|
+
#pp loans
|
635
|
+
#pp Kiva::LendingAction.load
|
636
|
+
#pp Kiva::Lender.load_for_loan 95693
|
637
|
+
#pp Kiva::JournalEntry.load 14077
|
638
|
+
|
639
|
+
#je = Kiva::JournalEntry.load 14077
|
640
|
+
#pp je
|
641
|
+
|
642
|
+
#pp Kiva::Comment.load(je[0])
|
643
|
+
|
644
|
+
#pp (Kiva::Partner.load 2).length
|
645
|
+
#pp Kiva::Templates.load
|
646
|
+
|
647
|
+
#filter = Kiva::Filter.new.male.africa
|
648
|
+
#pp (Kiva::Loan.search filter)
|
649
|
+
#pp Kiva::Release.load
|
650
|
+
end
|