Skip to content

Commit

Permalink
Merge pull request #11 from frodsan/fix-server-thread
Browse files Browse the repository at this point in the history
Use instance variables for request/response data.
  • Loading branch information
jgorset committed Apr 29, 2016
2 parents be7b875 + ae90956 commit 0630204
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions lib/facebook/messenger/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,46 @@ module Messenger
# This module holds the server that processes incoming messages from the
# Facebook Messenger Platform.
class Server
class << self
def call(env)
@request = Rack::Request.new(env)
@response = Rack::Response.new

case @request.request_method
when 'GET' then verify
when 'POST' then receive
else method_not_allowed
end
end
def self.call(env)
new.call(env)
end

def verify
verify_token = Facebook::Messenger.config.verify_token
def call(env)
@request = Rack::Request.new(env)
@response = Rack::Response.new

if @request.params['hub.verify_token'] == verify_token
@response.write @request.params['hub.challenge']
else
@response.write 'Error; wrong verify token'
end

@response.finish
case
when @request.get? then verify
when @request.post? then receive
else @response.status = 405
end

def receive
hash = JSON.parse(@request.body.read)
# Facebook may batch several items in the 'entry' array during
# periods of high load.
hash['entry'].each do |entry|
# Facebook may batch several items in the 'messaging' array during
# periods of high load.
entry['messaging'].each do |messaging|
Facebook::Messenger::Bot.receive(messaging)
end
end
@response.finish
end

@response.finish
def verify
if @request['hub.verify_token'] == verify_token
@response.write @request['hub.challenge']
else
@response.write 'Error; wrong verify token'
end
end

def verify_token
Facebook::Messenger.config.verify_token
end

def method_not_allowed
@response.status = 405
@response.finish
def receive
hash = JSON.parse(@request.body.read)

# Facebook may batch several items in the 'entry' array during
# periods of high load.
hash['entry'].each do |entry|
# Facebook may batch several items in the 'messaging' array during
# periods of high load.
entry['messaging'].each do |messaging|
Facebook::Messenger::Bot.receive(messaging)
end
end
end
end
Expand Down

0 comments on commit 0630204

Please sign in to comment.