kiva 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ = Version 0.0.2
2
+ === Mon Mar 23 16:51:54 CET 2009
3
+ * nicer documentation
4
+
1
5
  = Version 0.0.1
2
6
  === Wed Mar 18 17:59:23 CET 2009
3
7
  * initial version, hope everything works
data/README CHANGED
@@ -1,33 +1,98 @@
1
1
  = kiva -- wrapper to the kiva rest API
2
2
 
3
- You can find more information about the API here[http://developers.wiki.kiva.org/KivaAPI]
3
+ This is a wrapper to the Kiva Web API. You can find more information
4
+ about the API here[http://developers.wiki.kiva.org/KivaAPI].
4
5
 
5
- == Installing
6
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.
7
+ == Example
9
8
 
9
+ The following is an example of retrieving the last 20 loans on Kiva.
10
+ This is about as difficult as things get:
10
11
 
12
+ require 'kiva'
13
+
14
+ newest = Kiva::Loan.load_newest
15
+ newest.each{ |loan|
16
+ puts "#{loan.name} loaned $#{loan.funded_amount} for
17
+ #{loan.activity} in #{loan.location["country"]}"
18
+ }
11
19
 
12
- == Mailing List
20
+ Running the above script yields something similar to the following:
21
+
22
+ $ ruby -rubygems examples/newest.rb
23
+
24
+ Angela Okosun loaned $225 for Cement in Nigeria
25
+ Vitaliy Konovalenko loaned $1650 for Retail in Ukraine
26
+ (...)
27
+ Thi Thoa Do's Group loaned $300 for Food Production/Sales in Viet Nam
28
+ Thi Hoa Nguyen loaned $900 for General Store in Viet Nam
29
+
30
+
31
+ == Prerequisites
32
+
33
+ Kiva doesn't require an account or an API key in order to use their API,
34
+ so there are no prerequisites to using it. This library does depend on
35
+ SimpleHTTP[http://simplehttp.rubyforge.org/] and
36
+ json[http://json.rubyforge.org/] to handle the web api. These should be
37
+ installed automatically if you used +gem+ to install the library, else,
38
+ you'll need to install them manually.
13
39
 
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
40
 
41
+ == Basic Usage
19
42
 
43
+ This library consists of number of classes modelling the data
44
+ objects which the web api returns. Each class is similar, consisting of
45
+ class methods used to call webapi functions and attributes which provide
46
+ access to the (unpacked) json data the webapi returns.
20
47
 
48
+ These classes are (in no particular order) :
21
49
 
50
+ * <code>Kiva::Loan</code>
51
+ * <code>Kiva::Lender</code>
52
+ * <code>Kiva::LendingAction</code>
53
+ * <code>Kiva::Partner</code>
54
+ * <code>Kiva::JournalEntry</code>
55
+ * <code>Kiva::Comment</code>
56
+ * <code>Kiva::Release</code>
22
57
 
58
+ Each of these classes has attributes containing the data returned from
59
+ the web api. For example, a +Comment+ has the attributes:
23
60
 
61
+ * <code>author</code>
62
+ * <code>body</code>
63
+ * <code>date</code>
64
+ * <code>id</code>
65
+ * <code>whereabouts</code>
24
66
 
67
+ Each class has one or more class methods, corresponding to web api
68
+ calls, which may be used to create and load instances of the class. For
69
+ example, +Loan+ has the following list of class methods:
25
70
 
71
+ * +load+ : loans/&lt;ids&gt;[http://developers.wiki.kiva.org/KivaAPI#loans/ltidsgt]
72
+ * +load_for_lender+ : loans/&lt;id&gt;/lenders[http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/lenders]
73
+ * +load_newest+ : loans/newest[http://developers.wiki.kiva.org/KivaAPI#loans/newest]
74
+ * +search+ : loans/search[http://developers.wiki.kiva.org/KivaAPI#loans/search]
26
75
 
27
76
 
28
77
 
78
+ == Installing
79
+
80
+ You should be able to `gem install kiva`. Alternatively, download a distribution
81
+ at rubyforge: http://rubyforge.org/frs/?group_id=8034
82
+
83
+ Finally you can grab the sources from the rubyforge or github repositories:
29
84
 
85
+ git clone git://rubyforge.org/kiva.git
30
86
 
87
+ or
31
88
 
89
+ http://github.com/a2800276/kiva/tree/master#
32
90
 
33
- =
91
+
92
+
93
+ == Mailing List
94
+
95
+ In case you discover bugs, spelling errors, offer suggestions for
96
+ improvements or would like to help out with the project, you can contact
97
+ me directly (tim@kuriositaet.de).
98
+
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ require "rubygems"
9
9
 
10
10
  SHORTNAME ='kiva' # this should be the rubyforge project name
11
11
  DESC = 'wrapper to kiva api'
12
- PKG_VERSION ='0.0.1'
12
+ PKG_VERSION ='0.0.2'
13
13
  LONG_DESC = <<END_DESC
14
14
  Wrapper to the kiva web api. The API is described in
15
15
  more detail here:
data/TODO CHANGED
@@ -4,7 +4,5 @@ Just about everything. Off the top of my head:
4
4
  * multiple search parameters
5
5
 
6
6
  * nicer handling of paging.
7
- * nicer documentation
8
7
 
9
- * gem stuff ?
10
8
 
@@ -0,0 +1,8 @@
1
+ require 'kiva'
2
+
3
+ require 'kiva'
4
+
5
+ newest = Kiva::Loan.load_newest
6
+ newest.each{ |loan|
7
+ puts "#{loan.name} loaned $#{loan.funded_amount} for #{loan.activity} in #{loan.location["country"]}"
8
+ }
data/lib/kiva.rb CHANGED
@@ -1,12 +1,22 @@
1
1
  require 'json'
2
2
  require 'simplehttp'
3
3
 
4
+ # The Kiva module is a namespace to contain all the classes and contains
5
+ # some utility functions to handle json and http.
4
6
 
5
7
  module Kiva
6
8
 
7
9
  #
8
- # central location to perform webservice query
9
- # hook into here for error handling, etc.
10
+ # This is central location where the webservice query
11
+ # is performed. You probably won't need to change this, but in case you
12
+ # you need special error handling or some other behaviour, you can
13
+ # modify everything in one place HERE.
14
+ #
15
+ # For examples on how to do this, have a look at the files: `test/generate_fixtures.rb`
16
+ # and `test/test_kiva.rb`.
17
+ #
18
+ # ===Error Handling
19
+ #
10
20
  # currently the normal HttpExceptions are just passed
11
21
  # on, which should be ok for starters.
12
22
  #
@@ -88,7 +98,18 @@ module Kiva
88
98
  res
89
99
  end
90
100
 
91
-
101
+ # Represents a Loan (whoda guessed.) The accessible parameters are all
102
+ # all there is to this. Depending on how a particular instance of loan
103
+ # was loaded, not all of the attributes need have a value, so remember
104
+ # to check for +nil+.
105
+ #
106
+ # Use one of the class functions:
107
+ # * +load+
108
+ # * +load_for_lender+
109
+ # * +load_newest+
110
+ # * +search+
111
+ #
112
+ # to load loans from Kiva.
92
113
  class Loan
93
114
  attr_accessor :id
94
115
  attr_accessor :status
@@ -122,14 +143,15 @@ module Kiva
122
143
  #
123
144
  # Returns details for one or more loans.
124
145
  #
125
- # PARAMS
126
- # `ids` : an instance of `Loan` or a loan id or an array `Loan`s/loan IDs
146
+ # ====Paramaters
147
+ # +ids+ : an instance of +Loan+ or a loan id or an array +Loan+'s
148
+ # or loan id's
127
149
  #
128
- # RETURNS
129
- # an array of `Loan` instances
150
+ # ====Returns
151
+ # an array of +Loan+ instances
130
152
  #
131
- # CORRESPONDS
132
- # http://developers.wiki.kiva.org/KivaAPI#loans/ltidsgt
153
+ # ====Corresponds
154
+ # http://developers.wiki.kiva.org/KivaAPI#loans/ltidsgt
133
155
  #
134
156
  def load ids
135
157
  case ids
@@ -153,14 +175,14 @@ module Kiva
153
175
  #
154
176
  # Search for loans matching specific criteria.
155
177
  #
156
- # PARAMS
157
- # `filter` : an instance of `Filter` describing the search parameter
178
+ # ====Parameters
179
+ # +filter+ : an instance of +Filter+ describing the search parameter
158
180
  #
159
- # RETURNS
160
- # an array of `Loan` instances
181
+ # ====Returns
182
+ # an array of +Loan+ instances
161
183
  #
162
- # CORRESPONDS
163
- # http://developers.wiki.kiva.org/KivaAPI#loans/search
184
+ # ====Corresponds
185
+ # http://developers.wiki.kiva.org/KivaAPI#loans/search
164
186
  #
165
187
  def search filter, page=nil
166
188
  url = SEARCH
@@ -178,14 +200,14 @@ module Kiva
178
200
  #
179
201
  # Returns the most recent fundraising loans.
180
202
  #
181
- # PARAMS
182
- # `page` : the page position
203
+ # ====Parameters
204
+ # +page+ : the page position
183
205
  #
184
- # RETURNS
185
- # an array of `Loan` instances
206
+ # ====Returns
207
+ # an array of +Loan+ instances
186
208
  #
187
- # CORRESPONDS
188
- # http://developers.wiki.kiva.org/KivaAPI#loans/newest
209
+ # ====Corresponds
210
+ # http://developers.wiki.kiva.org/KivaAPI#loans/newest
189
211
  #
190
212
  def load_newest page=nil
191
213
  url = LOAD_NEWEST
@@ -199,18 +221,18 @@ module Kiva
199
221
 
200
222
 
201
223
  #
202
- # Returns loans sponsored by Lender
224
+ # Returns loans sponsored by a specified lender
203
225
  #
204
- # PARAMS
205
- # `id` : id of lender or instance of `Lender`
206
- # `sort_by`: one of :newest, :oldest
207
- # `page` : page position
226
+ # ====Parameters
227
+ # * +id+ : id of lender or instance of +Lender+
228
+ # * +sort_by+: one of <code>:newest, :oldest</code>
229
+ # * +page+ : page position
208
230
  #
209
- # RETURNS
210
- # array of `Loan`s
231
+ # ====Returns
232
+ # array of +Loan+'s
211
233
  #
212
- # CORRESPONDS
213
- # http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidgt/loans
234
+ # ====Corresponds
235
+ # http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidgt/loans
214
236
  #
215
237
 
216
238
  def load_for_lender id, sort_by=nil, page=nil
@@ -230,6 +252,17 @@ module Kiva
230
252
  end
231
253
 
232
254
  end
255
+
256
+ # Represents a Lender (whoda guessed.) The accessible parameters are all
257
+ # all there is to this. Depending on how a particular instance of loan
258
+ # was loaded, not all of the attributes need have a value, so remember
259
+ # to check for +nil+.
260
+ #
261
+ # Use one of the class functions:
262
+ # * +load+
263
+ # * +load_for_loan+
264
+ #
265
+ # to load lender information from Kiva.
233
266
 
234
267
  class Lender
235
268
  # url = "http://api.kivaws.org/v1/lenders/%s.json"
@@ -257,14 +290,14 @@ module Kiva
257
290
  #
258
291
  # List public lenders of a loan.
259
292
  #
260
- # PARAMS
261
- # `loan` : a loan id or an instance of `Loan`
293
+ # ====Parameters
294
+ # +loan+ : a loan id or an instance of +Loan+
262
295
  #
263
- # RETURNS
264
- # an array of `Lender` instances.
296
+ # ====Returns
297
+ # an array of +Lender+ instances.
265
298
  #
266
- # CORRESPONDS TO
267
- # http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/lenders
299
+ # ====Corresponds
300
+ # http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/lenders
268
301
  #
269
302
  def load_for_loan loan, page = nil
270
303
  loan = loan.id if loan.is_a?(Loan)
@@ -283,14 +316,14 @@ module Kiva
283
316
  #
284
317
  # Load details for one or more Lenders.
285
318
  #
286
- # PARAMS
287
- # `ids` : a lender id or and array of id's
319
+ # ====Parameters
320
+ # +ids+ : a lender id or and array of id's
288
321
  #
289
- # RETURNS
290
- # an array of `Lender` instances.
322
+ # ====Returns
323
+ # an array of +Lender+ instances.
291
324
  #
292
- # CORRESPONDS TO
293
- # http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidsgt
325
+ # ====Corresponds
326
+ # http://developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidsgt
294
327
  #
295
328
  def load ids
296
329
  ids = ids.join(",") if ids.is_a?(Array)
@@ -305,6 +338,10 @@ module Kiva
305
338
 
306
339
  end
307
340
 
341
+ # +LendingAction+ are basically a showcase of recent Kiva activity.
342
+ # Use the class method +load+ to loan the last 100 lenders and the
343
+ # loan they sponsored.
344
+
308
345
  class LendingAction
309
346
  attr_accessor :id
310
347
  attr_accessor :date
@@ -318,11 +355,11 @@ module Kiva
318
355
  #
319
356
  # Returns the last 100 public actions from Kiva.
320
357
  #
321
- # RETURNS
322
- # an array of `LendingAction` instances
358
+ # ====Returns
359
+ # an array of +LendingAction+ instances
323
360
  #
324
- # CORRESPONDS
325
- # http://developers.wiki.kiva.org/KivaAPI#lendingactions/recent
361
+ # ====corresponds
362
+ # http://developers.wiki.kiva.org/KivaAPI#lendingactions/recent
326
363
  #
327
364
  def load
328
365
  raw = Kiva.execute(LOAD_RECENT)
@@ -333,7 +370,11 @@ module Kiva
333
370
  end
334
371
  end
335
372
  end
336
-
373
+
374
+ # Journal entries are attached to loans. Using the class method +load+
375
+ # you can retrieve all entries attached to a particular loan.
376
+ #
377
+
337
378
  class JournalEntry
338
379
  attr_accessor :id
339
380
  attr_accessor :body
@@ -345,6 +386,22 @@ module Kiva
345
386
  attr_accessor :image
346
387
  attr_accessor :recommendation_count
347
388
 
389
+ #
390
+ # Retrieve an array of +Comment+ instances
391
+ # associated with this entry. N.B. this leads to
392
+ # a further network call.
393
+ def comments
394
+ unless @comments
395
+ return nil if id.is_nil?
396
+ if @comment_count != 0
397
+ @comments = Comment.load(this)
398
+ else
399
+ @commetns = []
400
+ end
401
+ end
402
+ @comments
403
+ end
404
+
348
405
  class << self
349
406
  KEY = "journal__entries"
350
407
  LOAD = "http://api.kivaws.org/v1/loans/%s/journal_entries.json?"
@@ -352,14 +409,14 @@ module Kiva
352
409
  #
353
410
  # Load journal entries for a loan.
354
411
  #
355
- # PARAMS
356
- # `id` : a loan id or an instance of `Loan`
412
+ # ====Parameters
413
+ # +id+ : a loan id or an instance of +Loan+
357
414
  #
358
- # RETURNS
359
- # an array of `JournalEntry` instances.
415
+ # ====Returns
416
+ # an array of +JournalEntry+ instances.
360
417
  #
361
- # CORRESPONDS TO
362
- # http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/journalentries
418
+ # ====Corresponds
419
+ # http://developers.wiki.kiva.org/KivaAPI#loans/ltidgt/journalentries
363
420
  #
364
421
  def load id, page = nil, include_bulk = nil
365
422
  id = id.id if id.is_a? Loan
@@ -375,7 +432,8 @@ module Kiva
375
432
  end
376
433
  end
377
434
  end
378
-
435
+
436
+ # User comment made in response to a JournalEntry.
379
437
  class Comment
380
438
  # http://api.kivaws.org/v1/journal_entries/<id>/comments.json
381
439
  attr_accessor :body
@@ -391,12 +449,14 @@ module Kiva
391
449
  #
392
450
  # Loads an array of comments for a JournalEntry.
393
451
  #
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.
452
+ # ====Parameters
453
+ # * +id+ : the numerical id of a Journal Entry or an instance of the class +JournalEntry+
454
+ # * +page+: which page of comments to load, default is the first.
397
455
  #
398
- # Corresponds to the API:
456
+ # ====Returns
457
+ # array of +Comments+
399
458
  #
459
+ # ====Corresponds
400
460
  # http://developers.wiki.kiva.org/KivaAPI#journalentries/ltidgt/comments
401
461
  #
402
462
  def load id, page=nil
@@ -412,6 +472,7 @@ module Kiva
412
472
  end
413
473
  end
414
474
 
475
+ # Kiva field Partner
415
476
  class Partner
416
477
  attr_accessor :start_date
417
478
  attr_accessor :rating
@@ -432,14 +493,14 @@ module Kiva
432
493
  #
433
494
  # Load an alphabetically sorted list of partners.
434
495
  #
435
- # PARAMS
436
- # `page`: page position
496
+ # ====Parameters
497
+ # +page+: page position
437
498
  #
438
- # RETURNS
439
- # an array of `Partner` instances
499
+ # ====Returns
500
+ # an array of +Partner+ instances
440
501
  #
441
- # CORRESPONDS
442
- # http://developers.wiki.kiva.org/KivaAPI#partners
502
+ # ====Corresponds
503
+ # http://developers.wiki.kiva.org/KivaAPI#partners
443
504
  #
444
505
 
445
506
  def load page=nil
@@ -452,6 +513,8 @@ module Kiva
452
513
  end
453
514
  end
454
515
 
516
+ # Release information concerning the webapi. This is not the
517
+ # release number of the ruby library!
455
518
  class Release
456
519
 
457
520
  attr_accessor :date
@@ -461,12 +524,11 @@ module Kiva
461
524
  #
462
525
  # Returns release information
463
526
  #
527
+ # ====Returns
528
+ # an array of +Partner+ instances
464
529
  #
465
- # RETURNS
466
- # an array of `Partner` instances
467
- #
468
- # CORRESPONDS
469
- # http://developers.wiki.kiva.org/KivaAPI#releases/api/current
530
+ # ====Corresponds
531
+ # http://developers.wiki.kiva.org/KivaAPI#releases/api/current
470
532
  #
471
533
 
472
534
  def load
@@ -477,6 +539,7 @@ module Kiva
477
539
  end
478
540
  end
479
541
 
542
+ # Templates which may be used to construct html image tags.
480
543
  class Templates
481
544
  attr_accessor :id
482
545
  attr_accessor :pattern
@@ -489,100 +552,120 @@ module Kiva
489
552
  end
490
553
  end
491
554
 
555
+ # Filter to be used in order to describe the intended search results
556
+ # of <code>Loan.search</code>
492
557
  class Filter
493
558
  attr_accessor :params
559
+
560
+ # Create a new instance of filter with no attributes.
494
561
  def initialize
495
562
  @params = {}
496
563
  end
497
564
 
498
- #sort_by
565
+ # sort_by
499
566
  def popularity
500
567
  @params["sort_by"]="popularity"
501
568
  return self
502
569
  end
570
+ # sort_by
503
571
  def loan_amount
504
572
  @params["sort_by"]="loan_amount"
505
573
  return self
506
574
  end
575
+ # sort_by
507
576
  def oldest
508
577
  @params["sort_by"]="oldest"
509
578
  return self
510
579
  end
580
+ # sort_by
511
581
  def expiration
512
582
  @params["sort_by"]="expiration"
513
583
  return self
514
584
  end
585
+ # sort_by
515
586
  def newest
516
587
  @params["sort_by"]="newest"
517
588
  return self
518
589
  end
590
+ # sort_by
519
591
  def amount_remaining
520
592
  @params["sort_by"]="amount_remaining"
521
593
  return self
522
594
  end
595
+ # sort_by
523
596
  def repayment_term
524
597
  @params["sort_by"]="repayment_term"
525
598
  return self
526
599
  end
527
600
 
528
- # GENDER
601
+ # restrict to male loan recipients
529
602
  def male
530
603
  @params["gender"]="male"
531
604
  return self
532
605
  end
606
+ # restrict to female loan recipients
533
607
  def female
534
608
  @params["gender"]="female"
535
609
  return self
536
610
  end
537
611
 
538
- # STATUS
612
+ # restrict status
539
613
  def fundraising
540
614
  @params["status"]="fundraising"
541
615
  return self
542
616
  end
617
+ # restrict status
543
618
  def funded
544
619
  @params["status"]="funded"
545
620
  return self
546
621
  end
622
+ # restrict status
547
623
  def in_repayment
548
624
  @params["status"]="in_repayment"
549
625
  return self
550
626
  end
627
+ # restrict status
551
628
  def paid
552
629
  @params["status"]="paid"
553
630
  return self
554
631
  end
632
+ # restrict status
555
633
  def defaulted
556
634
  @params["status"]="defaulted"
557
635
  return self
558
636
  end
559
637
 
560
- #REGION
561
-
638
+ #restrict region
562
639
  def north_america
563
640
  @params["region"]="na"
564
641
  return self
565
642
  end
643
+ #restrict region
566
644
  def central_america
567
645
  @params["region"]="ca"
568
646
  return self
569
647
  end
648
+ #restrict region
570
649
  def south_america
571
650
  @params["region"]="sa"
572
651
  return self
573
652
  end
653
+ #restrict region
574
654
  def africa
575
655
  @params["region"]="af"
576
656
  return self
577
657
  end
658
+ #restrict region
578
659
  def asia
579
660
  @params["region"]="as"
580
661
  return self
581
662
  end
663
+ #restrict region
582
664
  def middle_east
583
665
  @params["region"]="me"
584
666
  return self
585
667
  end
668
+ #restrict region
586
669
  def eastern_europe
587
670
  @params["region"]="ee"
588
671
  return self
@@ -4,6 +4,7 @@ require 'pp'
4
4
  require File.dirname(__FILE__) + '/../lib/kiva'
5
5
 
6
6
  module Kiva
7
+ # Modified `execute` method to generate fixtures!
7
8
  def Kiva.execute url, query=nil
8
9
  result = SimpleHttp.get(url, query)
9
10
  key = [url, query]
data/test/test_kiva.rb CHANGED
@@ -3,11 +3,12 @@ require 'pp'
3
3
  require File.dirname(__FILE__) + '/../lib/kiva'
4
4
 
5
5
 
6
- # Patch up execute method to use dummies and not connect to real web api.
7
6
  module Kiva
8
7
  fixtures = File.open(File.dirname(__FILE__)+"/fixtures.rbf").readlines.join
9
8
  $fixtures = eval(fixtures)
10
9
 
10
+ # Modified `execute` method for TESTING!
11
+ # Patch up execute method to use dummies and not connect to real web api.
11
12
  def Kiva.execute url, query=nil
12
13
  key = [url, query]
13
14
  $fixtures[key]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kiva
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-20 00:00:00 +01:00
12
+ date: 2009-03-23 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,6 +40,7 @@ extra_rdoc_files: []
40
40
 
41
41
  files:
42
42
  - lib/kiva.rb
43
+ - examples/newest.rb
43
44
  - AUTHORS
44
45
  - CHANGELOG
45
46
  - COPYING