Module: Trello::Helpers

Included in:
Main
Defined in:
lib/trello/helpers.rb

Instance Method Summary collapse

Instance Method Details

#format_api_url(url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/trello/helpers.rb', line 7

def format_api_url(url)
  url = 'http://' + url unless url.start_with?('http://')
  url += '/' unless url.end_with?('/')
  begin
    # ensure it's a valid url before returning
    URI.parse(url).to_s
  rescue
    abort "#{url} is not a valid URL."
  end
end

#prompt_for_list_from_options(prompt_message) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trello/helpers.rb', line 22

def prompt_for_list_from_options(prompt_message)
  var = nil
  options = yield
  options.each_with_index do |opt, i|
    puts "#{i}: #{opt[:name]}"
  end
  until var
    response = ask(prompt_message).split(',').map(&:to_i)
    if response.all?{|id| id && options[id] }
      var = options.values_at(*response).map{|option| option[:id]}
    else
      bad_options = response.select{|id| !(id && options[id])}
      puts "Invalid options #{bad_options.join(', ')}. Please give numbers between 0 and #{options.length} separated by commas."
    end
  end

  var
end

#prompt_from_options(prompt_message) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trello/helpers.rb', line 41

def prompt_from_options(prompt_message)
  var = nil
  options = yield
  options.each_with_index do |opt, i|
    puts "#{i}: #{opt[:name]}"
  end
  until var
    response = to_i(ask(prompt_message))
    if response && options[response]
      var = options[response][:id]
    else
      say "Invalid option - please give a number between 0 and #{options.length}."
    end
  end

  var
end

#rootObject



3
4
5
# File 'lib/trello/helpers.rb', line 3

def root
  File.dirname __dir__
end

#user_checkObject



18
19
20
# File 'lib/trello/helpers.rb', line 18

def user_check
  param_check('user', "No user defined! Specify a user with trello config --user <username>.")
end

#write_json_to_file(hash, filename) ⇒ Object



59
60
61
62
# File 'lib/trello/helpers.rb', line 59

def write_json_to_file(hash, filename)
  puts hash
  File.open(filename, 'w') {|f| f.write JSON.pretty_generate(hash)}
end