Class: XiWechatCorp::Callback::Rack
- Inherits:
-
Object
- Object
- XiWechatCorp::Callback::Rack
- Includes:
- Rack::Utils
- Defined in:
- lib/xi_wechat_corp/callback/rack.rb
Constant Summary collapse
- CONTENT_TYPE =
'text/html; charset=utf-8'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, &block) ⇒ Rack
constructor
A new instance of Rack.
- #read_body(body) ⇒ Object
Constructor Details
#initialize(app, &block) ⇒ Rack
Returns a new instance of Rack.
14 15 16 17 |
# File 'lib/xi_wechat_corp/callback/rack.rb', line 14 def initialize(app, &block) @app = app @config = Config.new.configure(&block) end |
Instance Method Details
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/xi_wechat_corp/callback/rack.rb', line 19 def call(env) rack_request = ::Rack::Request.new(env) if (rack_request.get? || rack_request.post?) && (request = @config.build_request(rack_request)) request.verify! if rack_request.get? ::Rack::Response.new([request.challenge]).finish else request.decrypt env['xi_wechat_corp.params'] = request.params status, headers, body = @app.call(env) rack_response = ::Rack::Response.new([], status, headers) if rack_response.successful? response = request.build_response(read_body(body)) xml = response.to_s if !xml.empty? rack_response['Content-Type'] = CONTENT_TYPE rack_response.write xml end rack_response.finish else [status, headers, body] end end else @app.call(env) end rescue XiWechatCorp::Error => e if XiWechatCorp.logger XiWechatCorp.logger.error(e.) end [403, {}, []] end |
#read_body(body) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/xi_wechat_corp/callback/rack.rb', line 53 def read_body(body) ret = '' body.each do |str| ret << str end body.close if body.respond_to?(:close) ret end |