Skip to content

Instantly share code, notes, and snippets.

@billinghamj
Created September 14, 2013 00:46
Show Gist options
  • Save billinghamj/6557786 to your computer and use it in GitHub Desktop.
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.
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