Module: Wechat::Responder::ClassMethods

Defined in:
lib/wechat/responder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'lib/wechat/responder.rb', line 13

def token
  @token
end

#wechatObject

Returns the value of attribute wechat.



13
14
15
# File 'lib/wechat/responder.rb', line 13

def wechat
  @wechat
end

Instance Method Details

#on(message_type, with: nil, respond: nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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, :fallback]
  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, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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
    yield(* match_responders(responders, message[:Event]))

  else
    yield(responders.first)
  end
end

#responders(type) ⇒ Object



30
31
32
33
# File 'lib/wechat/responder.rb', line 30

def responders type
  @responders ||= Hash.new
  @responders[type] ||= Array.new
end