Class: Magpie::Snake

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/middles/snake.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#build_xml, #dig, #get_notify_res, #get_query, #get_xml_body, #hash_to_xml, #log_errors, #log_notify, #post_query, #send_notify, #send_req_to, #start_http

Constructor Details

#initialize(app, &block) ⇒ Snake

Returns a new instance of Snake.



13
14
15
16
# File 'lib/middles/snake.rb', line 13

def initialize(app, &block)
  @app = app
  @block = block
end

Class Method Details

.reg(snake, target, state) ⇒ Object



9
10
11
# File 'lib/middles/snake.rb', line 9

def self.reg(snake, target, state)
  proc{ instance_method("#{target}_#{state}").bind(snake).call}
end

Instance Method Details

#alipay_indexObject



56
57
58
59
60
# File 'lib/middles/snake.rb', line 56

def alipay_index
  @am = AlipayModel.new(@req.params)
  @title = "支付宝-收银台"
  render_success_or_fail
end

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/middles/snake.rb', line 18

def call(env)
  state, header, body = @app.call(env)
  @block.call(self)
  @req = Rack::Request.new(env)
  @urls[@req.request_method].each { |path, lamb|
    if @req.path_info =~ Regexp.new("^#{path}$")
      body = lamb.call
      break
    end
  }
  [state, header, body]
  rescue Exception => e
  Magpie.logger.info(e.inspect + ":\n" + e.backtrace[0..8].join("\n"))
  [500, header, "500, 请查看日志,了解异常原因"]
end

#chinabank_indexObject



62
63
64
65
66
# File 'lib/middles/snake.rb', line 62

def chinabank_index
  @am = ChinabankModel.new(@req.params)
  @title = "网银在线-收银台"
  render_success_or_fail
end

#order_payObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/middles/snake.rb', line 74

def order_pay
  return "支付失败, 缺少足够的参数" if @req.params.blank?
  case @req.params["notify_kind"]
  when "alipay", "chinabank"
    notify_res = send_notify("POST", @req.params["notify_url"], query_to_hash(@req.params["notify"]))
    method = "POST"
  when "tenpay"
    notify_res = send_notify("GET", @req.params["notify_url"], @req.params["notify"])
    method = "GET"
  end
  log_notify(method, @req.params["notify_url"], query_to_hash(@req.params["notify"]), notify_res)
  notify_res
end

#reg(target, state) ⇒ Object



42
43
44
# File 'lib/middles/snake.rb', line 42

def reg(target, state)
  self.class.reg(self, target, state)
end

#route(method, target, states) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/middles/snake.rb', line 46

def route(method, target, states)
  routes = states.inject({ }){ |h, state|
    url_path = "/#{target}/#{state}"
    h[url_path] = reg(target, state)
    h["/#{target}"] = reg(target, state) if state.to_s == "index"
    h
  }
  @urls[method.to_s.upcase].merge!(routes)
end

#tenpay_indexObject



68
69
70
71
72
# File 'lib/middles/snake.rb', line 68

def tenpay_index
  @am = TenpayModel.new(@req.params)
  @title = "财付通-收银台"
  render_success_or_fail
end

#tongue(target, contents = { }) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/middles/snake.rb', line 34

def tongue(target, contents = { })
  @urls ||= { "GET" => { }, "POST" => { }}
  states = [contents[:states]].flatten.compact
  route("GET", target, states)
  actions = [contents[:actions]].flatten.compact
  route("POST", target, actions)
end