Class: Chooser
- Inherits:
-
Object
- Object
- Chooser
- Defined in:
- lib/manufacturer/bad-dragon.rb,
lib/chooser.rb
Overview
Copyright 2014 Maxine Red [email protected]
This file is part of toy-chooser.
toy-chooser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
toy-chooser is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with toy-chooser. If not, see <http://www.gnu.org/licenses/>.
Instance Method Summary collapse
-
#add(toys) ⇒ Object
Add a toy to the 'owned' list.
- #fetch_bd ⇒ Object
-
#initialize ⇒ Chooser
constructor
A new instance of Chooser.
-
#list(toy_list = "toys") ⇒ Object
List existing toys.
- #remove_aliases(column) ⇒ Object
-
#show(toys) ⇒ Object
Show measurements of toys.
-
#suggest(noargs = []) ⇒ Object
Suggests toys for future purchases guessed from what is owned already.
-
#update(manu) ⇒ Object
Update database for given manufacturer.
Constructor Details
#initialize ⇒ Chooser
Returns a new instance of Chooser.
28 29 30 31 32 33 |
# File 'lib/chooser.rb', line 28 def initialize @db_file = File.("~/.toys.sqlite") if !File.exist?(@db_file) then database_create end end |
Instance Method Details
#add(toys) ⇒ Object
Add a toy to the 'owned' list. Toys are in the format sku/size.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/chooser.rb', line 44 def add(toys) toys.each do |toy| sku, size = toy.split("/") sku, size = SQLite3::Database.quote(sku), SQLite3::Database.quote(size.upcase) SQLite3::Database.new(@db_file) do |db| query = "SELECT id, name FROM toys WHERE sku = '#{sku}'" query += " AND size = '#{size}';" id, name = db.execute(query).flatten if !id then $stderr.puts "Unknown toy or size \"#{toy}\"." next end query = "INSERT INTO owned (sku, size, toy_id) VALUES" query += " ('#{sku}','#{size}',#{id});" db.execute(query) puts "Added \"#{name}\" (#{size}) to owned list." end end end |
#fetch_bd ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/manufacturer/bad-dragon.rb', line 21 def fetch_bd net = Net::HTTP.new("bad-dragon.com",80) toys = JSON.parser.new(net.get("/products/getproducts").body).parse toys.reject!{|c|c["cat_id"].to_i!=1} toys.map!{|c|c["link_url"].sub(/.+bad-dragon.com/,"")} toys.each do |t| body = net.get(t).body File.open("cole.html","w"){|f|f.print body} html = Nokogiri::HTML.parse(body) id = html.css("span[itemprop=productID]").first.text name = html.css("span[itemprop=model]").first.text sizes = html.css("table.divided th").map{|th|th.text} sizes.shift # Just drop the first item. table = html.css("table.divided tr").map do |tr| tr.css("td").map{|td|td.text} end table.shift sql_query = "INSERT INTO toys"; sizes.count.times do |i| size = sizes[i][0,1].sub("E","XL") size = "XS" if sizes[i] == "Mini" query = Hash.new query.store("sku",id) query.store("name",SQLite3::Database.quote(name)) query.store("size",size) price = html.css("#size_inputs label").map do |x| if x.text.match(/^#{sizes[i]}/i) then x.css("i").text.sub("$","").to_i end end.compact.first price = html.css("span[itemprop=price]").first.text.to_i if !price query.store("price",price) table.each do |t| next if t[0].match(/thinnest|smallest/i) # We look only for biggest measures. col = t[0].downcase.sub(/meter of /,"_").sub(/umference of /,"_") col.gsub!(" ","_") col.gsub!(/_\(.+\)/,"") inches = (t[i+1].to_f*100).round.to_i query.store(col,inches) end if i == 0 then sql_query += " (#{query.keys.join(",")}) VALUES\n" end sql_query += " ('#{query.values.join("','")}'),\n" end sql_query[-2,2] = ";" SQLite3::Database.new(@db_file) do |db| begin db.execute(sql_query) rescue p sql_query raise end end puts "Updated \"#{name}\" successfully." end end |
#list(toy_list = "toys") ⇒ Object
List existing toys.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/chooser.rb', line 103 def list(toy_list="toys") toy_list = toy_list.first toy_list = "toys" unless toy_list if !["toys","owned"].include?(toy_list) then $stderr.puts "No such list." abort end SQLite3::Database.new(@db_file) do |db| draw_list(db.execute("SELECT sku, size FROM #{toy_list};")) end end |
#remove_aliases(column) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/manufacturer/bad-dragon.rb', line 79 def remove_aliases(column) column = column.sub("usuable","usable").sub("avg._","") column = column.sub(/(thickest|largest)_part_of_/,"") column = column.sub(/_(base)/,"") # Other common aliases column = column.sub("tip","head").sub("middle","shaft","bottom","knot") column = column.sub("shaft_ridges","knot").sub("medial_ring","knot") column = column.sub("bulb","knot") end |
#show(toys) ⇒ Object
Show measurements of toys.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/chooser.rb', line 64 def show(toys) toys.each do |toy| sku, size = toy.split("/") sku, size = SQLite3::Database.quote(sku), SQLite3::Database.quote(size.to_s.upcase) SQLite3::Database.new(@db_file) do |db| query = "SELECT sku, size, price, dia_head, dia_shaft, dia_knot, " query += "circ_head, circ_shaft, circ_knot, usable_length, " query += "total_length FROM toys WHERE sku = '#{sku}';" draw_show(db.execute(query)) end end end |
#suggest(noargs = []) ⇒ Object
Suggests toys for future purchases guessed from what is owned already.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/chooser.rb', line 77 def suggest(noargs=[]) i, sizes = 0, Array.new(7) {0} ranges = [0.5, 0.4, 0.3, 0.6, 0.5, 0.4, 0.9] cols = ["dia_head","dia_shaft","dia_knot","circ_head","circ_shaft", "circ_knot","usable_length"] SQLite3::Database.new(@db_file) do |db| query = "SELECT #{cols.join(", ")} FROM toys, owned " query += "WHERE toys.id = owned.toy_id;" db.execute(query) do |row| row.each_with_index do |c,id| sizes[id] += c.to_i end i += 1 end sizes = sizes.map.with_index do |x,id| avg = (x/i.to_f).round.to_i line = "((#{cols[id]} >= #{(avg*(1-ranges[id])).round.to_i} " line += "AND #{cols[id]} <= #{(avg*(1+ranges[id])).round.to_i}) " line += "OR #{cols[id]} IS NULL)" line end query = "SELECT sku, size FROM toys WHERE #{sizes.join(" AND ")};" draw_list(db.execute(query)) end end |
#update(manu) ⇒ Object
Update database for given manufacturer.
35 36 37 38 39 40 41 42 |
# File 'lib/chooser.rb', line 35 def update(manu) case manu.first when "bad-dragon" then fetch_bd else $stderr.puts "Manufacturer unkown.","Known manufacturers are: bad-dragon" abort end end |