Class: PizzaSelector

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

Class Method Summary collapse

Class Method Details

.add_pizza(pizza) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pizza_selector.rb', line 49

def self.add_pizza(pizza)
  params = pizza.params.merge(
    "pageId" => "PIZZA_DETAIL",
    # TODO: Allow cut type, number of slices and quantity selection
    "cutTypeC" => 1, # Type of cut: 1=Round Cut
    "cutSu" => 8, # Number of slices
    "figure" => 1 # Quantity
  )

  response = Request.post(
    "https://order.dominos.jp/eng/cart/add/pizza/", params,
    expect: :redirect, to: %r{\Ahttps?://order\.dominos\.jp/eng/cart/added/\z},
    failure: "Couldn't add the pizza you selected"
  )

  # For some reason we need to GET this URL otherwise it doesn't count as added <_<
  Request.get(response["Location"],
              expect: :redirect, to: "https://order.dominos.jp/eng/cart/",
              failure: "Couldn't add the pizza you selected")
end

.customize_pizza(pizza) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pizza_selector.rb', line 33

def self.customize_pizza(pizza)
  puts "#{"".colorize(:green)} #{pizza.name.colorize(:blue)}"

  # TODO: Allow toppings selection

  # Choosing the size
  selected_size_index = Ask.list "Choose the size", pizza.available_sizes.map(&:list_item)
  pizza.size = pizza.available_sizes[selected_size_index]

  # Choosing the crust
  selected_crust_index = Ask.list "Choose the crust", pizza.available_crusts.map(&:list_item)
  pizza.crust = pizza.available_crusts[selected_crust_index]

  pizza
end

.select_pizzasObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pizza_selector.rb', line 3

def self.select_pizzas
  return unless Ask.confirm "Add a pizza?"

  response = Request.get("https://order.dominos.jp/eng/pizza/search/",
                         expect: :ok, failure: "Couldn't get pizza list page")

  pizzas = Pizzas.from(response.body)

  cli = HighLine.new
  choices = pizzas.selection_list

  loop do
    puts "-" * 42
    cli.choose do |menu|
      menu.prompt = "Add a pizza via number:"
      menu.choices(*(choices + ["Cancel"])) do |choice|
        index = choices.index(choice)

        if index && index < choices.count
          selected_pizza = pizzas[index]
          add_pizza(customize_pizza(selected_pizza))
        end
      end
      menu.default = "Cancel"
    end

    break unless Ask.confirm "Add another pizza?"
  end
end