simplificator-ruby-kiva 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Kiva
2
+ class Partner
3
+ include Api
4
+ include DynamicInitializer
5
+ attr_accessor :status, :name, :rating, :delinquency_rate, :id, :total_amount_raised, :default_rate, :loans_posted
6
+ typed_attr_accessor :image, Kiva::Image
7
+ typed_attr_accessor :start_date, Time, :parse
8
+ typed_attr_accessor :countries, Kiva::Country, :new, true
9
+
10
+ # Find a partner
11
+ # either by :id or all of them
12
+ # Since kiva does not offer pagination or search,
13
+ # finding by id is implemented in memory/ruby.
14
+ # Items are cached but can be reloaded by passing :reload => true (caching is suggested by kiva...)
15
+ def self.find(params = {})
16
+ if params[:id]
17
+ find().detect() {|item| item.id == params[:id]}
18
+ else
19
+ find_all(params)
20
+ end
21
+ end
22
+
23
+
24
+ private
25
+
26
+ def self.find_all(params)
27
+ if params[:reload] || !@partners
28
+ @partners = json_to_paged_array(get('/partners.json', base_options(params)), 'partners', true)
29
+ end
30
+ @partners
31
+ end
32
+ end
33
+ end
34
+
35
+ #{"start_date"=>"2005-04-15T17:00:00Z",
36
+ #{}"rating"=>0,
37
+ #{}"status"=>"closed",
38
+ #{}"name"=>"East Africa Beta",
39
+ #{}"delinquency_rate"=>0,
40
+ #{}"id"=>1,
41
+ #{}"total_amount_raised"=>26600,
42
+ #{}"default_rate"=>9.1917293233083,
43
+ #{}"loans_posted"=>62,
44
+ #{}"countries"=>[{"name"=>"Uganda", "region"=>"Africa", "iso_code"=>"UG", "location"=>{"geo"=>{"type"=>"point", "level"=>"country", "pairs"=>"2 33"}}}, {"name"=>"Kenya", "region"=>"Africa", "iso_code"=>"KE", "location"=>{"geo"=>{"type"=>"point", "level"=>"country", "pairs"=>"1 38"}}}, {"name"=>"Tanzania", "region"=>"Africa", "iso_code"=>"TZ", "location"=>{"geo"=>{"type"=>"point", "level"=>"country", "pairs"=>"-6 35"}}}],
45
+ #{}"image"=>{"template_id"=>1, "id"=>58088}}
@@ -0,0 +1,11 @@
1
+ module Kiva
2
+ class Payment
3
+ include DynamicInitializer
4
+ attr_accessor :amount
5
+ typed_attr_accessor :due_date, Time, :parse
6
+
7
+ def to_s
8
+ "<Payment due at #{self.due_date} : #{self.amount}>"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module Kiva
2
+ class Team
3
+ include DynamicInitializer
4
+ include Api
5
+
6
+ attr_accessor :id, :shortname, :name, :category, :image, :whereabouts, :loan_because, :description,
7
+ :website_url, :membership_type, :member_count, :loan_count, :loaned_amount
8
+
9
+ typed_attr_accessor :team_since, Time, :parse
10
+ typed_attr_accessor :image, Kiva::Image
11
+
12
+ def lenders(params = {})
13
+ @lenders ||= Lender.find(params.merge({:team_id => self.id}))
14
+ end
15
+ def loans(params = {})
16
+ @loans ||= Loan.find(params.merge({:team_id => self.id}))
17
+ end
18
+
19
+ # find a team by :id or :shortname
20
+ def self.find(params)
21
+ if params[:id]
22
+ json_to_paged_array(get("/teams/#{params[:id]}.json", :query => base_options(params)), 'teams', false)
23
+ elsif params[:shortname]
24
+ json_to_paged_array(get("/teams/using_shortname/#{params[:shortname]}.json", :query => base_options(params)), 'teams', false)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module Kiva
2
+ class Terms
3
+ include DynamicInitializer
4
+
5
+ attr_accessor :disbursal_amount, :loss_liability, :loan_amount, :disbursal_currency
6
+
7
+ typed_attr_accessor :disbursal_date, Time, :parse
8
+ typed_attr_accessor :scheduled_payments, Kiva::Payment, :new, true
9
+ typed_attr_accessor :local_payments, Kiva::Payment, :new, true
10
+
11
+ end
12
+ end
13
+
14
+ # {"disbursal_date"=>"2010-02-12T08:00:00Z",
15
+ # "scheduled_payments"=>[{"amount"=>"366.67", "due_date"=>"2011-04-01T07:00:00Z"}, {"amount"=>"366.66", "due_date"=>"2012-04-01T07:00:00Z"}, {"amount"=>"366.67", "due_date"=>"2013-04-01T07:00:00Z"}],
16
+ # "disbursal_amount"=>600000,
17
+ # "local_payments"=>[{"amount"=>200000, "due_date"=>"2011-02-12T08:00:00Z"}, {"amount"=>200000, "due_date"=>"2012-02-12T08:00:00Z"}, {"amount"=>200000, "due_date"=>"2013-02-12T08:00:00Z"}],
18
+ # "loss_liability"=>{"currency_exchange_coverage_rate"=>0.2, "nonpayment"=>"lender", "currency_exchange"=>"shared"},
19
+ # "loan_amount"=>1100,
20
+ # "disbursal_currency"=>"CRC"}
@@ -0,0 +1,7 @@
1
+ module Kiva
2
+ class Video
3
+ include DynamicInitializer
4
+ attr_accessor :youtube_id, :id
5
+ end
6
+ end
7
+ #{"youtube_id"=>"LH8Izddvgds", "id"=>1025}
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+
4
+ %w( dynamic_initializer api payment borrower location country image paged_array video terms loan
5
+ lender partner lending_action journal_entry comment team).each do |name|
6
+ require File.join(File.dirname(__FILE__), 'ruby-kiva', name)
7
+ end
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simplificator-ruby-kiva}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["simplificator"]
12
+ s.date = %q{2010-04-12}
13
+ s.description = %q{a ruby wrapper for the kiva.org api}
14
+ s.email = %q{info@simplificator.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/ruby-kiva/api.rb",
27
+ "lib/ruby-kiva/borrower.rb",
28
+ "lib/ruby-kiva/comment.rb",
29
+ "lib/ruby-kiva/country.rb",
30
+ "lib/ruby-kiva/dynamic_initializer.rb",
31
+ "lib/ruby-kiva/image.rb",
32
+ "lib/ruby-kiva/journal_entry.rb",
33
+ "lib/ruby-kiva/lender.rb",
34
+ "lib/ruby-kiva/lending_action.rb",
35
+ "lib/ruby-kiva/loan.rb",
36
+ "lib/ruby-kiva/location.rb",
37
+ "lib/ruby-kiva/paged_array.rb",
38
+ "lib/ruby-kiva/partner.rb",
39
+ "lib/ruby-kiva/payment.rb",
40
+ "lib/ruby-kiva/team.rb",
41
+ "lib/ruby-kiva/terms.rb",
42
+ "lib/ruby-kiva/video.rb",
43
+ "lib/simplificator-ruby-kiva.rb",
44
+ "simplificator-ruby-kiva.gemspec",
45
+ "test/helper.rb",
46
+ "test/test_simplificator-ruby-kiva.rb",
47
+ "test/unit/borrower_test.rb",
48
+ "test/unit/comment_test.rb",
49
+ "test/unit/country_test.rb",
50
+ "test/unit/image_test.rb",
51
+ "test/unit/journal_entry_test.rb",
52
+ "test/unit/lender_test.rb",
53
+ "test/unit/payment_test.rb"
54
+ ]
55
+ s.homepage = %q{http://github.com/simplificator/simplificator-ruby-kiva}
56
+ s.rdoc_options = ["--charset=UTF-8"]
57
+ s.require_paths = ["lib"]
58
+ s.rubygems_version = %q{1.3.6}
59
+ s.summary = %q{a ruby wrapper for the kiva.org api}
60
+ s.test_files = [
61
+ "test/helper.rb",
62
+ "test/test_simplificator-ruby-kiva.rb",
63
+ "test/unit/borrower_test.rb",
64
+ "test/unit/comment_test.rb",
65
+ "test/unit/country_test.rb",
66
+ "test/unit/image_test.rb",
67
+ "test/unit/journal_entry_test.rb",
68
+ "test/unit/lender_test.rb",
69
+ "test/unit/payment_test.rb"
70
+ ]
71
+
72
+ if s.respond_to? :specification_version then
73
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
74
+ s.specification_version = 3
75
+
76
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
77
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
78
+ else
79
+ s.add_dependency(%q<shoulda>, [">= 0"])
80
+ end
81
+ else
82
+ s.add_dependency(%q<shoulda>, [">= 0"])
83
+ end
84
+ end
85
+
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'simplificator-ruby-kiva'
8
+ include Kiva
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSimplificatorRubyKiva < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ require '../helper'
2
+ class BorrowerTest < Test::Unit::TestCase
3
+ DATA = {"gender"=>"M", "pictured"=>true, "first_name"=>"José Manuel", "last_name"=>"Blanco Chinchilla"}
4
+
5
+ context 'a new borrower' do
6
+ setup do
7
+ @borrower = Borrower.new()
8
+ end
9
+ should 'have nil amount and due_date' do
10
+ assert !@borrower.gender
11
+ assert !@borrower.pictured
12
+ assert !@borrower.first_name
13
+ assert !@borrower.last_name
14
+ end
15
+
16
+ should 'neither be male nor female' do
17
+ assert !@borrower.male?
18
+ assert !@borrower.female?
19
+ end
20
+
21
+ should 'not be pictured?' do
22
+ assert !@borrower.pictured?
23
+ end
24
+
25
+ end
26
+
27
+ context 'a new borrower with data' do
28
+ setup do
29
+ @borrower = Borrower.new(DATA)
30
+ end
31
+
32
+ should 'be pictured?' do
33
+ assert @borrower.pictured?
34
+ end
35
+
36
+ should 'be male' do
37
+ assert @borrower.male?
38
+ end
39
+
40
+ should 'have a first name' do
41
+ assert_equal 'José Manuel', @borrower.first_name
42
+ end
43
+
44
+ should 'have a last name' do
45
+ assert_equal 'Blanco Chinchilla', @borrower.last_name
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,43 @@
1
+ require '../helper'
2
+ class CommentTest < Test::Unit::TestCase
3
+ DATA = {"body"=>"Ana,\r\n\r\nI wish you continued success with your business. Keep up the great work!\r\n\r\n", "date"=>"2007-11-23T06:31:45Z", "author"=>"Dee", "id"=>29197, "whereabouts"=>"Monroe, WA USA"}
4
+
5
+ context 'a new comment' do
6
+ setup do
7
+ @comment = Comment.new()
8
+ end
9
+ should 'have nil values' do
10
+ assert !@comment.body
11
+ assert !@comment.date
12
+ assert !@comment.author
13
+ assert !@comment.id
14
+ assert !@comment.whereabouts
15
+ end
16
+ end
17
+
18
+ context 'a new comment with data' do
19
+ setup do
20
+ @comment = Comment.new(DATA)
21
+ end
22
+
23
+ should 'have a body' do
24
+ assert @comment.body
25
+ end
26
+
27
+ should 'have a date' do
28
+ assert_equal Time.parse('2007-11-23T06:31:45Z'), @comment.date
29
+ end
30
+
31
+ should 'have an author' do
32
+ assert_equal 'Dee', @comment.author
33
+ end
34
+
35
+ should 'have an id' do
36
+ assert_equal 29197, @comment.id
37
+ end
38
+
39
+ should 'have whereabouts' do
40
+ assert_equal 'Monroe, WA USA', @comment.whereabouts
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ require '../helper'
2
+ class CountryTest < Test::Unit::TestCase
3
+ DATA = {"name"=>"Uganda", "region"=>"Africa", "iso_code"=>"UG", "location"=>{"geo"=>{"type"=>"point", "level"=>"country", "pairs"=>"2 33"}}}
4
+
5
+ context 'a new country' do
6
+ setup do
7
+ @country = Country.new()
8
+ end
9
+ should 'have nil attributes' do
10
+ assert !@country.name
11
+ assert !@country.region
12
+ assert !@country.iso_code
13
+ assert !@country.location
14
+ end
15
+ end
16
+
17
+ context 'a new country with data' do
18
+ setup do
19
+ @country = Country.new(DATA)
20
+ end
21
+
22
+ should 'have a code' do
23
+ assert_equal 'UG',@country.iso_code
24
+ end
25
+
26
+ should 'have a name' do
27
+ assert_equal 'Uganda', @country.name
28
+ end
29
+
30
+ should 'have a region' do
31
+ assert_equal 'Africa', @country.region
32
+ end
33
+
34
+ should 'have a location' do
35
+ assert @country.location
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,48 @@
1
+ require '../helper'
2
+ class ImageTest < Test::Unit::TestCase
3
+ DATA = {:template_id => 1, :id => 1234}
4
+ context 'a new image' do
5
+ setup do
6
+ @image = Kiva::Image.new()
7
+ end
8
+ should 'have nil attributes' do
9
+ assert !@image.id
10
+ assert !@image.template_id
11
+ end
12
+
13
+ should 'raise when calling url' do
14
+ assert_raise RuntimeError do
15
+ @image.url
16
+ end
17
+ end
18
+ end
19
+
20
+ context 'a new image with data' do
21
+ setup do
22
+ @image = Kiva::Image.new(DATA)
23
+ end
24
+
25
+ should 'have an id' do
26
+ assert_equal 1234, @image.id
27
+ end
28
+
29
+ should 'have a template_id' do
30
+ assert_equal 1, @image.template_id
31
+ end
32
+
33
+ should 'generate image url for default size' do
34
+ assert_equal 'http://www.kiva.org/img/w80h80/1234.jpg', @image.url
35
+ end
36
+
37
+ should 'generate image url for default w200h200' do
38
+ assert_equal 'http://www.kiva.org/img/w200h200/1234.jpg', @image.url(:w200h200)
39
+ end
40
+
41
+ should 'raise on unknown size' do
42
+ assert_raise RuntimeError do
43
+ @image.url(:foobar)
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,60 @@
1
+ require '../helper'
2
+ class JournalEntryTest < Test::Unit::TestCase
3
+ DATA = {"id" => 46861, "subject" => "Loan Refunded","body" => "This loan has been refunded for the following reason: \n\nDue to an administrative error, the loan amount posted was incorrect. We apologize for any inconvenience.\n","date" => "2008-04-01T19:47:28Z","author" => "Daniel Kahn","bulk" => true,"comment_count" => 0,"recommendation_count" =>0}
4
+
5
+ context 'a new journal entry' do
6
+ setup do
7
+ @journal_entry = JournalEntry.new()
8
+ end
9
+ should 'have nil attributes' do
10
+ assert !@journal_entry.id
11
+ assert !@journal_entry.subject
12
+ assert !@journal_entry.body
13
+ assert !@journal_entry.date
14
+ assert !@journal_entry.author
15
+ assert !@journal_entry.bulk
16
+ assert !@journal_entry.comment_count
17
+ assert !@journal_entry.recommendation_count
18
+ end
19
+ end
20
+
21
+ context 'a new Journal Entry with data' do
22
+ setup do
23
+ @journal_entry = JournalEntry.new(DATA)
24
+ end
25
+
26
+ should 'have an ID' do
27
+ assert_equal 46861, @journal_entry.id
28
+ end
29
+
30
+ should 'have a subject' do
31
+ assert_equal 'Loan Refunded', @journal_entry.subject
32
+ end
33
+
34
+ should 'have a body' do
35
+ assert @journal_entry.body
36
+ end
37
+
38
+ should 'be bulk' do
39
+ assert @journal_entry.bulk?
40
+ end
41
+
42
+ should 'have a date' do
43
+ assert_equal Time.parse('2008-04-01T19:47:28Z'), @journal_entry.date
44
+ end
45
+
46
+ should 'have an author' do
47
+ assert_equal 'Daniel Kahn', @journal_entry.author
48
+ end
49
+
50
+ should 'have recommendation count of 0' do
51
+ assert_equal 0, @journal_entry.recommendation_count
52
+ end
53
+
54
+ should 'have a comment count of 0' do
55
+ assert_equal 0, @journal_entry.comment_count
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,82 @@
1
+ require '../helper'
2
+ class LenderTest < Test::Unit::TestCase
3
+ DATA = {"loan_count"=>93, "occupation"=>"Entrepreneur", "country_code"=>"US",
4
+ "name"=>"Matt",
5
+ "loan_because"=>"I love the stories. ",
6
+ "lender_id"=>"matt",
7
+ "invitee_count"=>23,
8
+ "occupational_info"=>"I co-founded a startup nonprofit (this one!) and I work with an amazing group of people dreaming up ways to alleviate poverty through personal lending. ",
9
+ "personal_url"=>"www.socialedge.org/blogs/kiva-chronicles",
10
+ "uid"=>"matt",
11
+ "whereabouts"=>"San Francisco CA",
12
+ "image"=>{"template_id"=>1, "id"=>12829},
13
+ "member_since"=>"2006-01-01T09:01:01Z"}
14
+
15
+ context 'a new lender' do
16
+ setup do
17
+ @lender = Lender.new()
18
+ end
19
+ should 'have nil attributes' do
20
+ assert !@lender.loan_count
21
+ assert !@lender.occupation
22
+ assert !@lender.country_code
23
+ assert !@lender.name
24
+ assert !@lender.loan_because
25
+ assert !@lender.lender_id
26
+ assert !@lender.invitee_count
27
+ assert !@lender.occupational_info
28
+ assert !@lender.personal_url
29
+ assert !@lender.uid
30
+ assert !@lender.whereabouts
31
+ assert !@lender.image
32
+ assert !@lender.member_since
33
+ end
34
+ end
35
+
36
+ context 'a new lender with data' do
37
+ setup do
38
+ @lender = Lender.new(DATA)
39
+ end
40
+
41
+
42
+ should 'have a name' do
43
+ assert_equal 'Matt', @lender.name
44
+ end
45
+ should 'have reason for the loans' do
46
+ assert_equal 'I love the stories. ', @lender.loan_because
47
+ end
48
+
49
+ should 'have a lender_id' do
50
+ assert_equal 'matt', @lender.lender_id
51
+ end
52
+
53
+ should 'have an invitee count' do
54
+ assert 23, @lender.invitee_count
55
+ end
56
+
57
+ should 'have occupational_info' do
58
+ assert @lender.occupational_info
59
+ end
60
+
61
+ should 'have a personal_url' do
62
+ assert @lender.personal_url
63
+ end
64
+
65
+ should 'have an uid' do
66
+ assert_equal 'matt', @lender.uid
67
+ end
68
+
69
+ should 'have whereabouts' do
70
+ assert_equal 'San Francisco CA', @lender.whereabouts
71
+ end
72
+
73
+ should 'have an image' do
74
+ assert @lender.image
75
+ end
76
+
77
+ should 'have member_since' do
78
+ assert_equal Time.parse('2006-01-01T09:01:01Z'), @lender.member_since
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,28 @@
1
+ require '../helper'
2
+ class PaymentTest < Test::Unit::TestCase
3
+ DATA = {"amount"=> 366.67, "due_date"=>"2011-04-01T07:00:00Z"}
4
+
5
+ context 'a new payment' do
6
+ setup do
7
+ @payment = Payment.new()
8
+ end
9
+ should 'have nil amount and due_date' do
10
+ assert !@payment.amount
11
+ assert !@payment.due_date
12
+ end
13
+ end
14
+
15
+ context 'a new payment with data' do
16
+ setup do
17
+ @payment = Payment.new(DATA)
18
+ end
19
+ should 'have an amount of 366.67' do
20
+ assert_equal 366.67, @payment.amount
21
+ end
22
+
23
+ should 'have a due date' do
24
+ assert_equal Time.parse('2011-04-01T07:00:00Z'), @payment.due_date
25
+ end
26
+ end
27
+
28
+ end