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", <<-FILE
class #{class_name}Controller < ApplicationController

def index
end

def show

  url = URI.parse('http://api.kuaidi100.com/api')

  Net::HTTP.start(url.host, url.port) do |http|

  req = Net::HTTP::Post.new(url.path)

  req.set_form_data({
    id: "",
    com: params[:com],
    nu: params[:nu],
    valicode:"",
    show:"2",
    muti:"1",
    order:"desc"
  })

  puts http.request(req).body

  @content = http.request(req).body.force_encoding("UTF-8")

  end
end
end
  FILE
end

#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", <<-FILE
module #{class_name}Helper
  def get_com_hash
@com_hash ={
    "tiantian"=>"天天",
    "shentong"=>"申通",
    "yuantong"=>"圆通",
    "shunfeng"=>"顺丰",
    "debangwuliu"=>"德邦",
    "yunda"=>"韵达"
    }
  end
end
  FILE
end

#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", <<-FILE
=form_tag #{file_name}_show_path,role:"form" do |f|
  .form-group
%label 快递单号:
=text_field_tag :nu
  .form-group
.col-md-3
  =select_tag 'com', options_for_select(get_com_hash.collect{ |ch| [ ch[1], ch[0] ] }), class:"form-control"
  .form-group
=submit_tag "自助查询", class: "btn btn-success"
  FILE

  create_file "app/views/#{file_name}/show.html.haml", <<-FILE
%hr
:javascript
document.write("#{'#{@content}'}");
  FILE
end