Class: SortableAnswer::Answer
- Inherits:
-
Object
- Object
- SortableAnswer::Answer
- Defined in:
- lib/sortable_answer.rb
Instance Attribute Summary collapse
-
#listings ⇒ Object
Returns the value of attribute listings.
-
#matches ⇒ Object
Returns the value of attribute matches.
-
#products ⇒ Object
Returns the value of attribute products.
Instance Method Summary collapse
- #eqal_product(product_hash, challenge_hash) ⇒ Object
- #find_all_matches ⇒ Object
- #find_product_match(product, listings) ⇒ Object
- #hashify(json) ⇒ Object
-
#initialize(product_file, listing_file) ⇒ Answer
constructor
A new instance of Answer.
- #jsonfiy ⇒ Object
- #make_match(product_hash, challenge_hash) ⇒ Object
- #make_regex(str) ⇒ Object
- #make_title_match(product_hash, challenge_hash) ⇒ Object
- #model_match(product_hash, challenge_hash) ⇒ Object
- #product_matcher(product_hash, challenge_hash) ⇒ Object
Constructor Details
#initialize(product_file, listing_file) ⇒ Answer
Returns a new instance of Answer.
8 9 10 11 12 |
# File 'lib/sortable_answer.rb', line 8 def initialize(product_file, listing_file) self.products = File.readlines(product_file) self.listings = File.readlines(listing_file) self.matches = [] end |
Instance Attribute Details
#listings ⇒ Object
Returns the value of attribute listings.
6 7 8 |
# File 'lib/sortable_answer.rb', line 6 def listings @listings end |
#matches ⇒ Object
Returns the value of attribute matches.
6 7 8 |
# File 'lib/sortable_answer.rb', line 6 def matches @matches end |
#products ⇒ Object
Returns the value of attribute products.
6 7 8 |
# File 'lib/sortable_answer.rb', line 6 def products @products end |
Instance Method Details
#eqal_product(product_hash, challenge_hash) ⇒ Object
38 39 40 |
# File 'lib/sortable_answer.rb', line 38 def eqal_product(product_hash, challenge_hash) "#{product_hash["product_name"].downcase.tr('_', ' ')}" == challenge_hash["title"].downcase end |
#find_all_matches ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/sortable_answer.rb', line 62 def find_all_matches products = hashify(self.products) listings = hashify(self.listings) self.matches = products.map do |product| find_product_match(product, listings) end self.matches.last[:listings].each { |rm| listings.delete(rm) } end |
#find_product_match(product, listings) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/sortable_answer.rb', line 53 def find_product_match(product, listings) products_and_listings = {product_name: product["product_name"], listings: []} listings.each do |listing| match = product_matcher(product, listing) products_and_listings[:listings].push(match) unless match.nil? end products_and_listings end |
#hashify(json) ⇒ Object
14 15 16 |
# File 'lib/sortable_answer.rb', line 14 def hashify(json) json.map { |j| JSON.parse(j) } end |
#jsonfiy ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/sortable_answer.rb', line 71 def jsonfiy out_str = "" self.matches.each do |match| out_str += "#{match.to_json}\n" end out_str end |
#make_match(product_hash, challenge_hash) ⇒ Object
26 27 28 |
# File 'lib/sortable_answer.rb', line 26 def make_match(product_hash, challenge_hash) make_regex(product_hash["manufacturer"]).match(challenge_hash["manufacturer"]) end |
#make_regex(str) ⇒ Object
18 19 20 |
# File 'lib/sortable_answer.rb', line 18 def make_regex(str) Regexp.new("#{str}", Regexp::IGNORECASE) end |
#make_title_match(product_hash, challenge_hash) ⇒ Object
22 23 24 |
# File 'lib/sortable_answer.rb', line 22 def make_title_match(product_hash, challenge_hash) make_regex(product_hash["manufacturer"]).match(challenge_hash["title"]) end |
#model_match(product_hash, challenge_hash) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/sortable_answer.rb', line 30 def model_match(product_hash, challenge_hash) if product_hash["model"].length <= 4 Regexp.new("\b#{product_hash["model"]}[[:space:]|[:alpha:]|\"]", Regexp::EXTENDED | Regexp::IGNORECASE) =~ challenge_hash["title"] else Regexp.new("#{product_hash["model"]}[[:space:]|[:alpha:]|\"]", Regexp::EXTENDED | Regexp::IGNORECASE) =~ challenge_hash["title"] end end |
#product_matcher(product_hash, challenge_hash) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sortable_answer.rb', line 42 def product_matcher(product_hash, challenge_hash) if make_match(product_hash, challenge_hash) if make_title_match(product_hash, challenge_hash) && model_match(product_hash, challenge_hash) return challenge_hash elsif eqal_product(product_hash, challenge_hash) return challenge_hash end end end |