Class: Kiva::Loan

Inherits:
Object
  • Object
show all
Defined in:
lib/kiva.rb

Overview

Represents a Loan (whoda guessed.) The accessible parameters are all all there is to this. Depending on how a particular instance of loan was loaded, not all of the attributes need have a value, so remember to check for nil.

Use one of the class functions:

  • load

  • load_for_lender

  • load_newest

  • search

to load loans from Kiva.

Constant Summary collapse

KEY =
"loans"
LOAD_FOR_LENDER =
"http://api.kivaws.org/v1/lenders/%s/loans.json?"
LOAD_NEWEST =
"http://api.kivaws.org/v1/loans/newest.json?"
LOAD =
"http://api.kivaws.org/v1/loans/%s.json"
SEARCH =
"http://api.kivaws.org/v1/loans/search.json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#activityObject

Returns the value of attribute activity.



118
119
120
# File 'lib/kiva.rb', line 118

def activity
  @activity
end

#basket_amountObject

Returns the value of attribute basket_amount.



129
130
131
# File 'lib/kiva.rb', line 129

def basket_amount
  @basket_amount
end

#borrower_countObject

Returns the value of attribute borrower_count.



131
132
133
# File 'lib/kiva.rb', line 131

def borrower_count
  @borrower_count
end

#borrowersObject

Returns the value of attribute borrowers.



119
120
121
# File 'lib/kiva.rb', line 119

def borrowers
  @borrowers
end

#descriptionObject

Returns the value of attribute description.



120
121
122
# File 'lib/kiva.rb', line 120

def description
  @description
end

#funded_amountObject

Returns the value of attribute funded_amount.



124
125
126
# File 'lib/kiva.rb', line 124

def funded_amount
  @funded_amount
end

#funded_dateObject

Returns the value of attribute funded_date.



125
126
127
# File 'lib/kiva.rb', line 125

def funded_date
  @funded_date
end

#idObject

Returns the value of attribute id.



114
115
116
# File 'lib/kiva.rb', line 114

def id
  @id
end

#imageObject

Returns the value of attribute image.



126
127
128
# File 'lib/kiva.rb', line 126

def image
  @image
end

#loan_amountObject

Returns the value of attribute loan_amount.



132
133
134
# File 'lib/kiva.rb', line 132

def loan_amount
  @loan_amount
end

#locationObject

Returns the value of attribute location.



127
128
129
# File 'lib/kiva.rb', line 127

def location
  @location
end

#nameObject

Returns the value of attribute name.



116
117
118
# File 'lib/kiva.rb', line 116

def name
  @name
end

#partner_idObject

Returns the value of attribute partner_id.



122
123
124
# File 'lib/kiva.rb', line 122

def partner_id
  @partner_id
end

#posted_dateObject

Returns the value of attribute posted_date.



117
118
119
# File 'lib/kiva.rb', line 117

def posted_date
  @posted_date
end

#sectorObject

Returns the value of attribute sector.



128
129
130
# File 'lib/kiva.rb', line 128

def sector
  @sector
end

#statusObject

Returns the value of attribute status.



115
116
117
# File 'lib/kiva.rb', line 115

def status
  @status
end

#termsObject

Returns the value of attribute terms.



121
122
123
# File 'lib/kiva.rb', line 121

def terms
  @terms
end

#useObject

Returns the value of attribute use.



123
124
125
# File 'lib/kiva.rb', line 123

def use
  @use
end

Class Method Details

.load(ids) ⇒ Object

Returns details for one or more loans.

Paramaters

ids : an instance of Loan or a loan id or an array Loan‘s or loan id’s

Returns

an array of Loan instances

Corresponds

developers.wiki.kiva.org/KivaAPI#loans/ltidsgt



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kiva.rb', line 160

def load ids
  case ids
  when Loan
    ids = ids.id
  when Array
    if ids[0].is_a?(Loan)
      ids = ids.map{|l| l.id}
    end
    ids.join(",")
  end

  url = LOAD % ids

  raw = Kiva.execute url
  unw = JSON.parse(raw)

  Kiva._populate  Loan, unw[KEY]
end

.load_for_lender(id, sort_by = nil, page = nil) ⇒ Object

Returns loans sponsored by a specified lender

Parameters

  • id : id of lender or instance of Lender

  • sort_by: one of :newest, :oldest

  • page : page position

Returns

array of Loan‘s

Corresponds

developers.wiki.kiva.org/KivaAPI#lenders/ltlenderidgt/loans



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/kiva.rb', line 242

def load_for_lender id, sort_by=nil, page=nil
  id = id.uid if id.is_a?(Lender)
 
  url = LOAD_FOR_LENDER % id
  url = page ? url + "page=#{page}&" : url
  url = [:newest, :oldest].include?(sort_by) ? url + "sort_by=#{sort_by}" : url

  raw = Kiva.execute url
  unw = JSON.parse(raw)
  

  Kiva._populate  Loan, unw[KEY]
  
end

.load_newest(page = nil) ⇒ Object

Returns the most recent fundraising loans.

Parameters

page : the page position

Returns

an array of Loan instances

Corresponds

developers.wiki.kiva.org/KivaAPI#loans/newest



216
217
218
219
220
221
222
223
224
# File 'lib/kiva.rb', line 216

def load_newest page=nil
  url = LOAD_NEWEST
  url = page ? url + "page=#{page}&" : url
  
  raw = Kiva.execute url
  unw = JSON.parse(raw)

  Kiva._populate  Loan, unw[KEY]
end

.search(filter, page = nil) ⇒ Object

Search for loans matching specific criteria.

Parameters

filter : an instance of Filter describing the search parameter

Returns

an array of Loan instances

Corresponds

developers.wiki.kiva.org/KivaAPI#loans/search



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/kiva.rb', line 191

def search filter, page=nil
  url = SEARCH
  if filter && page
    filter["page"] = page
  end
  
  raw = Kiva.execute url, filter.params
  unw = JSON.parse(raw)

  Kiva._populate Loan, unw[KEY]

end