Class: Cf::Form

Inherits:
Thor
  • Object
show all
Includes:
Config
Defined in:
lib/cf/cli/form.rb

Overview

:nodoc: all

Instance Method Summary collapse

Methods included from Config

#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri

Instance Method Details

#generateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cf/cli/form.rb', line 47

def generate
  line_destination = Dir.pwd
  unless File.exist?("#{line_destination}/line.yml")
    say("The current directory is not a valid line directory.", :red) and return
  end

  FileUtils.rm_rf("#{line_destination}/station#{options[:station]}.html", :verbose => true) if options.force? && File.exist?("#{line_destination}/station#{options[:station]}.html")
  if File.exist?("#{line_destination}/station#{options[:station]}.html")
    say "Skipping the form generation because the station#{options[:station]}.html already exists with its custom form.\nUse the -f flag to force it to overwrite or check and delete the station#{options[:station]}.html file manually.", :red
  else
    say "Generating form for station #{options[:station]}", :green
    Cf::Newform.start([options[:station], options[:labels], options[:fields]])
    say "A new custom task form has been created named station#{options[:station]}.html !!!", :green
  end
end

#previewObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cf/cli/form.rb', line 65

def preview
  line_destination = Dir.pwd
  unless File.exist?("#{line_destination}/line.yml")
    say("The current directory is not a valid line directory.", :red) and return
  end
  if File.exist?("#{line_destination}/station#{options[:station]}.html")
    say "Generating preview form for station #{options[:station]}", :green
    form_content = File.read("station#{options[:station]}.html")
    Cf::FormPreview.start([options[:station], form_content])
  else
    say "No form exists for station #{options[:station]}", :red
    say "Generate the form for station 2 and then preview it.", :red
  end
end

#update(form = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cf/cli/form.rb', line 82

def update(form = nil)
  station_index = options[:station]
  line_destination = Dir.pwd
  yaml_source = "#{line_destination}/line.yml"
  unless File.exist?(yaml_source)
    say("The current directory is not a valid line directory.", :red) and return
  end


  set_target_uri(false)
  set_api_key(yaml_source)
  CF. = CF::Account.info['name']
  line_dump = YAML::load(File.read(yaml_source).strip)
  line_title = line_dump['title'].parameterize

  station_hash = line_dump["stations"][station_index-1]["station"]

  say "Updating form for the station#{station_index}", :green

  if File.exist?("#{line_destination}/station#{station_index}.html")
    form_content = station_hash["custom_task_form"]
    form_content["title"] = form_content.delete("form_title")
    form_content["raw_html"] =  File.read("station#{station_index}.html")
    resp = CF::CustomTaskForm.update(line_title, station_index, form_content)
  elsif station_hash.has_key?("task_form")
    say "No support for task form.", :red
  else
    say "No form exists for station #{station_index}", :red
  end

  exit(1) unless resp

  if resp.code == 200
    say "Form updated sucessfully!", :green
  else
    say "Form can not be updated because #{resp["error"]["message"]}", :red
  end
end