markmcspadden-ruby-kiva 0.1.0

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.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'net/https'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'kiva/loan'))
@@ -0,0 +1,55 @@
1
+ module Kiva
2
+ class Loan
3
+
4
+ def initialize(*args)
5
+ attrs = {}
6
+
7
+ # Get to the Hash
8
+ attrs = args.first if args.first.is_a?(Hash)
9
+
10
+ # Cycle through each pair
11
+ # Creating reader if necessary
12
+ # Setting value passed on args
13
+ attrs.each_pair do |k,v|
14
+ unless self.respond_to?(k.to_sym)
15
+ self.class.class_eval do
16
+ attr_reader k
17
+ end
18
+ end
19
+ instance_variable_set("@#{k.to_s}", v)
20
+ end
21
+ end
22
+
23
+ class << self
24
+ # Search the Loans on Kiva
25
+ # === Accepted Parameters
26
+ # * :q - a string to query based on
27
+ # * :status - the payment status: 'fundraising', 'funded', 'in_repayment', 'paid', 'defaulted'
28
+ # * all the others should work but haven't been verified
29
+ # === Returns
30
+ # Hash
31
+ # * paging - Hash with keys: total, page_size, pages, page
32
+ # * loans - Array of Hashes with keys cooresponding to Kiva
33
+ def search(params = {})
34
+ params_string_array = []
35
+ params.each_pair { |k,v| params_string_array << "#{k}=#{v}" }
36
+ params_string = params_string_array.join("&")
37
+
38
+ uri = URI.parse("http://api.kivaws.org/v1/loans/search.json?#{params_string}")
39
+
40
+ response = Net::HTTP.get_response(uri)
41
+ results = JSON.parse(response.body)
42
+ end
43
+
44
+ # Produce Kiva::Loan objects from the search
45
+ def search_results(params = {})
46
+ loans = []
47
+ search(params)["loans"].each do |loan|
48
+ loans << Kiva::Loan.new(loan)
49
+ end
50
+ loans
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,94 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module LoanSpecHelper
4
+ def search_results_raw
5
+ {"paging"=>{"total"=>32566, "page_size"=>20, "pages"=>1629, "page"=>1},
6
+ "loans"=>[{"borrower_count"=>1,
7
+ "status"=>"paid",
8
+ "name"=>"Waliholla Ali",
9
+ "posted_date"=>"2008-12-30T17:30:20Z",
10
+ "activity"=>"Farming",
11
+ "paid_amount"=>1050,
12
+ "id"=>81893,
13
+ "description"=>{"languages"=>["en"]},
14
+ "partner_id"=>34,
15
+ "use"=>"To increase his agricultural business.",
16
+ "loan_amount"=>1050,
17
+ "funded_amount"=>1050,
18
+ "image"=>{"template_id"=>1, "id"=>248766},
19
+ "location"=>{"country"=>"Afghanistan", "geo"=>{"type"=>"point", "level"=>"town", "pairs"=>"34.42 70.45"},
20
+ "town"=>"Jalalabad"},
21
+ "sector"=>"Agriculture"}]}
22
+ end
23
+ end
24
+
25
+ describe Kiva::Loan do
26
+ include LoanSpecHelper
27
+
28
+ describe "attributes" do
29
+ before(:each) do
30
+ @loan = Kiva::Loan.new(:status => "paid", :loan_amount => 1050)
31
+ end
32
+
33
+ it "should get the status attribute set during initialization" do
34
+ @loan.status.should == "paid"
35
+ end
36
+
37
+ it "should get the loan ammount" do
38
+ @loan.loan_amount.should == 1050
39
+ end
40
+
41
+ it "should NOT set the loan ammount after initialization (Kiva data is currently read only)" do
42
+ lambda{@loan.loan_amount = 1150}.should raise_error(NoMethodError)
43
+ end
44
+ end # attributes
45
+
46
+ describe "search" do
47
+ before(:each) do
48
+ @response = mock("response", :body => "{}")
49
+ end
50
+
51
+ it "should make the search request" do
52
+ Net::HTTP.should_receive(:get_response).with(URI.parse("http://api.kivaws.org/v1/loans/search.json?q=farm")).and_return(@response)
53
+ Kiva::Loan.search(:q => "farm")
54
+ end
55
+
56
+ it "should parse the JSON response" do
57
+ Net::HTTP.stub!(:get_response).and_return(@response)
58
+
59
+ JSON.should_receive(:parse).with(@response.body)
60
+ Kiva::Loan.search(:q => "farm")
61
+ end
62
+
63
+ it "should search loans based on status (fundraising,funded,in_repayment,paid,defaulted)" do
64
+ Net::HTTP.should_receive(:get_response).with(URI.parse("http://api.kivaws.org/v1/loans/search.json?status=paid")).and_return(@response)
65
+ Kiva::Loan.search(:status => "paid")
66
+ end
67
+
68
+ it "should escape any characters that would cause problems in search" do
69
+ pending "not yet implemented"
70
+ end
71
+
72
+ it "should handle non 200 OK responses" do
73
+ pending "not yet implemented"
74
+ end
75
+ end # search
76
+
77
+ describe "search results" do
78
+ before(:each) do
79
+ Kiva::Loan.should_receive(:search).with(:q => "farm").and_return(search_results_raw)
80
+
81
+ @results = Kiva::Loan.search_results(:q => "farm")
82
+ end
83
+ it "should ignore the paging details" do
84
+ @results.should_not include(search_results_raw["paging"])
85
+ end
86
+ it "should get loans that are of type Kiva::Loan" do
87
+ @results.first.class.to_s.should == "Kiva::Loan"
88
+ end
89
+ it "should handle a raw search result with a loans entry" do
90
+ pending "not yet implemented"
91
+ end
92
+ end # search results
93
+
94
+ end # Kiva::Loan
@@ -0,0 +1,7 @@
1
+ require 'spec'
2
+
3
+ require File.dirname(__FILE__) + '/../lib/kiva.rb'
4
+
5
+ Spec::Runner.configure do |config|
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markmcspadden-ruby-kiva
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Mcspadden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-04 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Ruby wrapper for the Kiva API
17
+ email: markmcspadden@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/kiva
27
+ - lib/kiva/loan.rb
28
+ - lib/kiva.rb
29
+ - spec/kiva
30
+ - spec/kiva/loan_spec.rb
31
+ - spec/spec_helper.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/markmcspadden/ruby-kiva
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A Ruby wrapper for the Kiva API
59
+ test_files: []
60
+