Created
September 14, 2013 00:46
-
-
Save billinghamj/6557786 to your computer and use it in GitHub Desktop.
Extension of Numeric with generic data retrieval. Currently limited to pricing from Amazon.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'nokogiri' | |
class PlauralObject | |
def initialize(name, number) | |
@name = name | |
@number = number | |
end | |
def cost | |
resp = HTTParty.get "http://www.amazon.com/s/?keywords=#{@name}" | |
html = Nokogiri::HTML.parse resp.body | |
prices = html.css('.newp span.bld.red').map { |e| e.content.strip.gsub(/[^\d\.]/, '').to_f } | |
average = prices.reduce(:+).to_f / prices.count | |
average * @number | |
end | |
def price | |
cost | |
end | |
end | |
class Numeric | |
def method_missing(name, *args, &block) | |
PlauralObject.new name, self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment