Class: PopularDeals::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/popular_deals/cli.rb

Overview

CLI Controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#product_urlObject

Returns the value of attribute product_url.



4
5
6
# File 'lib/popular_deals/cli.rb', line 4

def product_url
  @product_url
end

Instance Method Details

#callObject

BROWSER=(/usr/local/bin/firefox-bin -new-tab ‘%s’:/usr/local/bin/google-chrome-stable)



7
8
9
10
# File 'lib/popular_deals/cli.rb', line 7

def call
  list_deals
  menu
end

#disply_deal(input, product_url) ⇒ Object

def open_deal_in_browser

uri = "http://www.ruby-lang.org"
Launchy.open( uri ) do |exception|
  puts "Attempted to open #{uri} and failed because #{exception}"
end

end



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/popular_deals/cli.rb', line 71

def disply_deal(input, product_url)
  @deal = PopularDeals::NewDeals.deal_page(input, product_url)
  keys = @deal.keys
  puts ""
  puts "DEAL:".magenta.bold.gsub(/^/, "    ")
  puts "#{@deal[keys[0]]}".gsub(/^/, "    ")
  puts ""
  puts "Description:".upcase.magenta.bold.gsub(/^/, "    ")
  puts "#{@deal[keys[1]]}".gsub(/^/, "    ")
  puts ""
  if @deal[keys[2]].nil?
    puts "To lock this deal, please visit:".upcase.magenta.bold.gsub(/^/, "    ")
    puts "#{product_url}".gsub(/^/, "    ")
  else
    puts "To lock this deal, please visit:".upcase.magenta.bold.gsub(/^/, "    ")
    puts "#{@deal[keys[2]]}".gsub(/^/, "    ")
  end
  puts ""
  puts "-----------------------------------------------------------------------------------------------------------"
  puts ""
end

#goodbyeObject



93
94
95
96
97
98
99
100
101
# File 'lib/popular_deals/cli.rb', line 93

def goodbye
  shopping = ["We could give up shopping but we are not a quitter.", "When in doubt, go shopping.", "I still believe in the Holy Trinity, except now it's Target, Trader Joe's, and IKEA.", "I love shopping. There is a little bit of magic found in buying something new. It is instant gratification, a quick fix.", "If you can not stop thinking about it, Buy it!", "Shopping is cheaper than therapy."]
  line = shopping.sample
  puts ""
  print "#{line}".yellow
  puts "\u{1f609}"
  puts "Come back again for more deals. Have a great day!".yellow
  puts ""
end

#list_dealsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/popular_deals/cli.rb', line 12

def list_deals
  puts ""
  puts "Today's popular deals are:".upcase.yellow
  puts ""
    @deals = PopularDeals::NewDeals.new_deals
    @deals.each do |info|
      info.each.with_index(1) do |deal, i|
          if i < 10
            puts "#{i}. #{deal.title}".cyan.bold
            puts "Deal rating: #{deal.deal_rating}.".gsub(/^/, "   ")
            puts "Deal value - #{deal.price}".gsub(/^/, "   ")
            puts "#{deal.posted}".gsub(/^/, "   ")
            puts ""
        elsif i >= 10
          puts "#{i}. #{deal.title}".cyan.bold
          puts "Deal rating: #{deal.deal_rating}.".gsub(/^/, "    ")
          puts "Deal value - #{deal.price}".gsub(/^/, "    ")
          puts "#{deal.posted}".gsub(/^/, "    ")
          puts ""
        end
      end
   end
end


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
# File 'lib/popular_deals/cli.rb', line 36

def menu
  input = nil
  while input != "exit"

    puts ""
    puts "Enter the number of deal you would like more info on or type Exit".light_blue.bold
    puts ""
      input = gets.strip.downcase
      puts ""

      if input.to_i > 0 && input.to_i <= 20
        puts ""
        puts "-----------------------------------------------------------------------------------------------------------"
        puts ""
        puts "Please see below details of deal no. #{input}".upcase.cyan.bold
          disply_deal(input, product_url)
          #open_deal_in_browser
      elsif input == "list"
        list_deals
      elsif input == "exit"
        goodbye
      else
        puts "Don't understand your command. Type list to see the list or exit".colorize(:color => :white, :background => :red)
        puts ""
      end
  end
end