module WechatPublic::Responder::ClassMethods
Attributes
token[RW]
wechat[RW]
Public Instance Methods
on(message_type, with: nil, respond: nil, &block)
click to toggle source
# File lib/wechat/responder.rb, line 15 def on message_type, with: nil, respond: nil, &block raise "Unknow message type" unless message_type.in? [:text, :image, :voice, :video, :location, :link, :event, :login] config=respond.nil? ? {} : {:respond=>respond} config.merge!(:proc=>block) if block_given? if (with.present? && !message_type.in?([:text, :event])) raise "Only text and event message can take :with parameters" else config.merge!(:with=>with) if with.present? end responders(message_type) << config return config end
responder_for(message) { |* match_responders(responders, message)| ... }
click to toggle source
# File lib/wechat/responder.rb, line 35 def responder_for message, &block message_type = message[:MsgType].to_sym responders = responders(message_type) case message_type when :text yield(* match_responders(responders, message[:Content])) when :event if message[:Event] == 'CLICK' yield(* match_responders(responders, message[:EventKey])) else yield(* match_responders(responders, message[:Event])) end else yield(responders.first) end end
responders(type)
click to toggle source
# File lib/wechat/responder.rb, line 30 def responders type @responders ||= Hash.new @responders[type] ||= Array.new end
Private Instance Methods
match_responders(responders, value)
click to toggle source
# File lib/wechat/responder.rb, line 55 def match_responders responders, value matched = responders.inject({scoped:nil, general:nil}) do |matched, responder| condition = responder[:with] if condition.nil? matched[:general] ||= [responder, value] next matched end if condition.is_a? Regexp matched[:scoped] ||= [responder] + $~.captures if(value =~ condition) else matched[:scoped] ||= [responder, value] if(value == condition) end matched end return matched[:scoped] || matched[:general] end