Module: Maidcafe::CUI

Defined in:
lib/cui.rb

Class Method Summary collapse

Class Method Details

.prompt(api, condition, mandatory = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cui.rb', line 3

def prompt(api, condition, mandatory=false)
  puts
  unless mandatory
    print "#{{:tid => '都道府県名', :cid => 'カテゴリ', :sid => 'お店番号'}[condition]}を指定しますか (y/N)"
    return nil unless gets =~ /[yY]/
  end
  rs = api.invoke nil, condition => 'list'
  rs.items.each_with_index do |t, index|
    puts "#{index+1} : #{t.name}"
  end
  print '選択してください: '
  input = gets
  exit if input =~ /^\s*(quit|exit)\s*$/i
  type_id = input.to_i
  type_id <= 0 ? nil : rs.items[type_id - 1]
end

.puts_name(obj) ⇒ Object



20
21
22
# File 'lib/cui.rb', line 20

def puts_name(obj)
  puts obj.name if obj
end

.show_opt(opt, key, obj) ⇒ Object



24
25
26
27
# File 'lib/cui.rb', line 24

def show_opt(opt, key, obj)
  puts_name obj
  opt[key] = obj.id if obj
end

.startObject



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
# File 'lib/cui.rb', line 29

def start
  api = Maidcafe::API.new
  type = nil
  while !type
    type = prompt api, :type, true
  end
  puts_name type
  
  opts = {}
  
  prefecture = prompt api, :tid
  show_opt opts, :tid, prefecture
  
  category = prompt api, :cid
  show_opt opts, :cid, category
  
  shop = prompt api, :sid
  show_opt opts, :sid, shop
  
  rs = api.invoke type.keytype, opts
  while true
    if rs.items.empty?
      puts "データが存在しません"
      exit
    end
    rs.items.each_with_index do |i, index|
      puts "#{index+1} : #{i.name} - #{i.description.split("\n")[0]}"
    end
    print '選択してください: '
    input = gets
    exit if input =~ /^\s*(quit|exit)\s*$/i
    item_id = input.to_i
    if 0 < item_id
      puts rs.items[item_id - 1].to_s
    end
    puts "\n[push enter key]"
    gets
  end
end