Class: Kuaidi100::Generator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/kuaidi100/generator.rb

Instance Method Summary collapse

Instance Method Details

#create_controller_fileObject


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
32
33
34
35
36
37
38
39
# File 'lib/kuaidi100/generator.rb', line 4

def create_controller_file
  route "post '#{file_name}/show'"
  route "get '#{file_name}/index'"
  create_file "app/controllers/#{file_name}_controller.rb", "class \#{class_name}Controller < ApplicationController\n\ndef index\nend\n\ndef show\n\n  url = URI.parse('http://api.kuaidi100.com/api')\n\n  Net::HTTP.start(url.host, url.port) do |http|\n\n  req = Net::HTTP::Post.new(url.path)\n\n  req.set_form_data({\n    id: \"\",\n    com: params[:com],\n    nu: params[:nu],\n    valicode:\"\",\n    show:\"2\",\n    muti:\"1\",\n    order:\"desc\"\n  })\n\n  puts http.request(req).body\n\n  @content = http.request(req).body.force_encoding(\"UTF-8\")\n\n  end\nend\nend\n  FILE\nend\n"

#create_helper_fileObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kuaidi100/generator.rb', line 61

def create_helper_file
  create_file "app/helpers/#{file_name}_helper.rb", "module \#{class_name}Helper\n  def get_com_hash\n@com_hash ={\n    \"tiantian\"=>\"\u5929\u5929\",\n    \"shentong\"=>\"\u7533\u901A\",\n    \"yuantong\"=>\"\u5706\u901A\",\n    \"shunfeng\"=>\"\u987A\u4E30\",\n    \"debangwuliu\"=>\"\u5FB7\u90A6\",\n    \"yunda\"=>\"\u97F5\u8FBE\"\n    }\n  end\nend\n  FILE\nend\n"

#create_view_fileObject


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

def create_view_file
  create_file "app/views/#{file_name}/index.html.haml", "=form_tag \#{file_name}_show_path,role:\"form\" do |f|\n  .form-group\n%label \u5FEB\u9012\u5355\u53F7:\n=text_field_tag :nu\n  .form-group\n.col-md-3\n  =select_tag 'com', options_for_select(get_com_hash.collect{ |ch| [ ch[1], ch[0] ] }), class:\"form-control\"\n  .form-group\n=submit_tag \"\u81EA\u52A9\u67E5\u8BE2\", class: \"btn btn-success\"\n  FILE\n\n  create_file \"app/views/\#{file_name}/show.html.haml\", <<-FILE\n%hr\n:javascript\ndocument.write(\"\#{'\#{@content}'}\");\n  FILE\nend\n"