Permalink
Browse files

Switching to 'money' gem for proper handling of currencies

  • Loading branch information...
1 parent 67df98e commit d116af3eab314936bae2e3e3139eeb7d3fa6a6df @smudge committed Jan 31, 2013
Showing with 22 additions and 5 deletions.
  1. +1 −0 kickstarter.gemspec
  2. +3 −1 lib/kickstarter.rb
  3. +6 −2 lib/kickstarter/project.rb
  4. +8 −1 lib/kickstarter/project_card.rb
  5. +4 −1 lib/kickstarter/tier.rb
View
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = "kickstarter"
s.add_dependency "nokogiri"
+ s.add_dependency "money"
s.add_development_dependency "fakeweb"
s.files = `git ls-files`.split("\n")
View
@@ -2,14 +2,16 @@
require "nokogiri"
require 'open-uri'
require 'date'
+require 'money'
require_relative "kickstarter/version"
require_relative "kickstarter/backer"
require_relative "kickstarter/project"
require_relative "kickstarter/project_card"
require_relative "kickstarter/tier"
module Kickstarter
- BASE_URL = "http://www.kickstarter.com"
+ BASE_URL = "http://www.kickstarter.com"
+ Money.assume_from_symbol = true
Categories = {
:art => "art",
@@ -33,8 +33,12 @@ def image_url
@image_url ||= details_page.css('#video-section img').attr('src').value
end
+ def currency
+ @currency ||= details_page.css("#pledged data").attr('data-currency').value
+ end
+
def pledge_amount
- @pledge_amount ||= Float(details_page.css("#pledged").attr("data-pledged").value)
+ @pledge_amount ||= Money.new(details_page.css("#pledged").attr("data-pledged").value, currency)*100
end
def pledge_percent
@@ -46,7 +50,7 @@ def pledge_deadline
end
def pledge_goal
- @pledge_goal ||= Float(details_page.css("#pledged").attr('data-goal').value)
+ @pledge_goal ||= Money.new(details_page.css("#pledged").attr('data-goal').value, currency)*100
end
def duration
@@ -35,8 +35,15 @@ def image_url
@image_url ||= thumbnail_url.gsub(/photo-little\.jpg/,'photo-full.jpg')
end
+ def currency
+ @currency ||= pledge_amount.currency.to_s
+ end
+
def pledge_amount
- @pledge_amount ||= /\$([0-9\,]+)/.match(node.css('.project-stats li')[1].css('strong').inner_html)[1].gsub(/\,/,"").to_i
+ @pledge_amount ||= begin
+ Money.assume_from_symbol = true
+ Money.parse(node.css('.project-stats li')[1].css('strong').inner_html)
+ end
end
def pledge_percent
View
@@ -12,7 +12,10 @@ def id
end
def minimum_pledge
- @minimum_pledge ||= Integer(node.css('h3').text[/\$[0-9\.\,]+/].gsub(/\$/,"").gsub(/\,/,""))
+ @minimum_pledge ||= begin
+ Money.assume_from_symbol = true
+ Money.parse(node.css('h3').text[/\D[0-9\.\,]+/].gsub(/\,/,""))
+ end
end
def backer_count

0 comments on commit d116af3

Please sign in to comment.