module MotionWechat::API

Constants

InvalidClientError
InvalidMediaObject

Public Class Methods

config() click to toggle source

Returns info_plist_key of MotionWechat

Example:

MotionWechat::API.config
# File lib/motion-wechat/api.rb, line 95
def self.config
  NSBundle.mainBundle.objectForInfoDictionaryKey MotionWechat::Config.info_plist_key
end
instance() click to toggle source

Returns singleton instance of MotionWechat

Example:

MotionWechat::API.instance
# File lib/motion-wechat/api.rb, line 86
def self.instance
  @instance ||= new config["key"], config["secret"]
end
new(key, secret, options={}) click to toggle source

Initialize weixin API using key and secret

Example:

MtionWechat::API.new 'key', 'secret'

Arguments:

key: (String)
secret: (String)
options: (Hash)
# File lib/motion-wechat/api.rb, line 19
def initialize(key, secret, options={})
  @key    = key
  @secret = secret
end

Public Instance Methods

authorize(opts={}) click to toggle source

Sends authorization request

Example:

MotionWechat::API.instance.authorize

Arguments:

opts: (Hash), state: "myapp"
# File lib/motion-wechat/api.rb, line 70
def authorize(opts={})
  options = {
    scope: "snsapi_userinfo",
    state: "motion-wechat-app"
  }.merge(opts)
  req = SendAuthReq.alloc.init
  req.scope = options[:scope]
  req.state = options[:state]
  WXApi.sendReq(req)
end
get_user_info(&block) click to toggle source

Get user information

Example:

MotionWechat::API.instance.get_user_info { |info| ... }
# File lib/motion-wechat/api.rb, line 50
def get_user_info(&block)
  raise InvalidClientError if @client.nil?
  if @token.nil?
    @client.get_token do |token|
      @token = token
      @token.get_user_info { |info| block.call info }
    end
  else
    @token.get_user_info { |info| block.call info }
  end
end
registerApp() click to toggle source

Register weixin app, usually put in ‘app_delegate.rb`

Example:

MotionWechat::API.instance.registerApp
# File lib/motion-wechat/api.rb, line 29
def registerApp
  WXApi.registerApp @key
end
registerClient(code) click to toggle source

Register client

Example:

MotionWechat::API.instance.registerClient "code"

Arguments:

code: (String)
# File lib/motion-wechat/api.rb, line 41
def registerClient(code)
  @client ||= MotionWechat::Client.new @key, @secret, code
end
send_text(text) click to toggle source

Send text to wechat

Example:

MotionWechat::API.instance.send_text "hello, motion"

Arguments:

text: (String)
# File lib/motion-wechat/api.rb, line 135
def send_text(text)
  req = SendMessageToWXReq.alloc.init
  req.bText = false
  req.text = text
  WXApi.sendReq(req)
end
wx() click to toggle source
# File lib/motion-wechat/api.rb, line 7
def wx; WXApi; end

Private Instance Methods

send_message_with(params, &block) click to toggle source

scene: WXSceneTimeline | WXSceneSession | WXSceneFavorite b_text: is this a text message? title, :description, thumbnail

# File lib/motion-wechat/api.rb, line 149
def send_message_with(params, &block)
  options = {
    scene: WXSceneSession,
    thumbnail: nil,
    b_text: false
  }.merge(params)

  message = WXMediaMessage.message
  message.setThumbImage options[:thumbnail]
  message.title = options[:title]
  message.description = options[:description]

  obj = block.call(options)

  message.mediaObject = obj

  req = SendMessageToWXReq.alloc.init
  req.bText = options[:b_text]
  req.scene = options[:scene]
  req.message = message
  WXApi.sendReq(req)
end