motion-wechat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTEwNDE5OGQyYzJhMmQ2NTY2OWFiMjEzYWU4NmYwMzQ3NTcwNjhmNg==
5
+ data.tar.gz: !binary |-
6
+ OTQ4OTlhN2I1ZWY3ZGVhMWFmNTVhMjNlNjM0NjFkZDgyODk0ZTY4NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWQ5OTY0YjI3NjgzMDhlMGRhMDQ0NTcxODlhMmJhZjQ1NzQ5NmIxOTc1MWVl
10
+ NjI0ZmM4OWZjMWM5NTVjNDgyY2Q3NDAwYjRlMjc2NzcwNjNjMWVhOTBmNGVi
11
+ OWVhMzZjMzJhMzU4ZTAzNTQ3OThkYTBiNmNiY2Q0YjcyMDgzMGM=
12
+ data.tar.gz: !binary |-
13
+ NmE0YzhkYTAxY2FjNjM5MWJlZWUwYjhhYWY0YjBhZDhmYTdmMjRjYWVhNWJi
14
+ Y2I3YTBlMTFkNWQzMWExYWE3N2U0OGRlNjYxODYzZmNiYjZjYWVhZDE1MmY0
15
+ ZWY1N2NhYmM2YjAxZjhiNmM3YzFlMjg2MzIwZmUwMTgzOTk1ZWM=
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ WechatSDK for RubyMotion
2
+ ====================
3
+
4
+ - RubyMotion wrapper for [WechatSDK](https://open.weixin.qq.com)
5
+ - Improving ...
6
+
7
+ ## Setup
8
+
9
+ Add WechatMotion to your Gemfile, and run `bundle install`:
10
+ ```ruby
11
+ gem 'motion-cocoapods'
12
+ gem 'wechat-motion'
13
+ ```
14
+
15
+ Edit the Rakefile of your RubyMotion project and add the following require line:
16
+ ```ruby
17
+ # After the line that require Rubymotion
18
+ require 'bundler'
19
+ Bundler.require
20
+ ```
21
+
22
+ Then add WeChatSDK to your pods list and setup configuration in your Rakefile:
23
+ ```ruby
24
+ app.pods do
25
+ pod 'WeChatSDK'
26
+ end
27
+
28
+ MotionWechat::Config.setup(app, 'app_key', 'app_secret')
29
+ ```
30
+
31
+ Initialize in app_delegate.rb
32
+ ```ruby
33
+ MotionWechat::API.instance.register
34
+ ```
35
+
36
+ Usage
37
+ ==========
38
+
39
+ Basic:
40
+
41
+ ```ruby
42
+ MotionWechat::API.instance.send_webpage "http://www.rubymotion.com", \
43
+ title: "Ruby Motion", description: "Awesome way to write ios/osx app"
44
+ ```
45
+
46
+ ## TODO
47
+ - delegete helpers, i.e. `WXApi.handleOpenURL(url, delegate:self)`
48
+
49
+ ## Contributions
50
+
51
+ Fork, please!
@@ -0,0 +1,17 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ require_relative 'motion-wechat/config'
6
+
7
+ Motion::Project::App.setup do |app|
8
+ Dir.glob(File.join(File.dirname(__FILE__), 'motion-wechat/*.rb')).each do |file|
9
+ app.files.unshift(file)
10
+ end
11
+
12
+ app.pods ||= Motion::Project::CocoaPods.new(app)
13
+ app.pods do
14
+ pod 'WeChatSDK'
15
+ end
16
+
17
+ end
@@ -0,0 +1,101 @@
1
+ module MotionWechat
2
+ module API
3
+
4
+ InvalidMediaObject = Class.new StandardError
5
+
6
+ def initialize(key, secret, options={})
7
+ @key = key
8
+ @secret = secret
9
+ end
10
+
11
+ # Register WechatAPI
12
+ def register
13
+ WXApi.registerApp(@key)
14
+ end
15
+
16
+ # Returns singleton instance of MotionWechat
17
+ #
18
+ # Example:
19
+ # MotionWechat::API.instance
20
+ #
21
+ def self.instance
22
+ @config ||= NSBundle.mainBundle.objectForInfoDictionaryKey MotionWechat::Config.info_plist_key
23
+ @instance = new @config["key"], @config["secret"]
24
+ end
25
+
26
+ # Send media objects, i.e. webpage, video, music and image to wechat
27
+ #
28
+ # Example:
29
+ # MotionWechat::API.instance.send_video video_url, title: 't', description: 'desc'
30
+ #
31
+ # Arguments:
32
+ # media object: (String / NSData)
33
+ # options: (Hash)
34
+ %w(webpage video music image).each do |meth|
35
+ define_method("send_#{meth}") do |arg, params|
36
+ property = case meth
37
+ when "webpage", "video", "music"
38
+ "#{meth}Url"
39
+ when "image"
40
+ "imageData"
41
+ else
42
+ raise InvalidMediaObject
43
+ end
44
+
45
+ send_message_with(params) do |options|
46
+ klass = Object.const_get "WX#{meth.capitalize}Object"
47
+ obj = klass.object
48
+ obj.send "#{property}=", arg
49
+ obj
50
+ end
51
+
52
+ end
53
+ end
54
+
55
+ # Send text to wechat
56
+ #
57
+ # Example:
58
+ # MotionWechat::API.instance.send_text "hello, motion"
59
+ #
60
+ # Arguments:
61
+ # text: (String)
62
+ def send_text(text)
63
+ req = SendMessageToWXReq.alloc.init
64
+ req.bText = false
65
+ req.text = text
66
+ WXApi.sendReq(req)
67
+ end
68
+
69
+ private
70
+
71
+ ###
72
+ # scene: WXSceneTimeline | WXSceneSession | WXSceneFavorite
73
+ # b_text: is this a text message?
74
+ # title, :description, thumbnail
75
+ ###
76
+ def send_message_with(params, &block)
77
+ options = {
78
+ scene: WXSceneSession,
79
+ thumbnail: nil,
80
+ b_text: false
81
+ }.merge(params)
82
+
83
+ message = WXMediaMessage.message
84
+ message.setThumbImage options[:thumbnail]
85
+ message.title = options[:title]
86
+ message.description = options[:description]
87
+
88
+ obj = block.call(options)
89
+
90
+ message.mediaObject = obj
91
+
92
+ req = SendMessageToWXReq.alloc.init
93
+ req.bText = options[:b_text]
94
+ req.scene = options[:scene]
95
+ req.message = message
96
+ WXApi.sendReq(req)
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,45 @@
1
+ module MotionWechat
2
+ module Config
3
+ extend self
4
+
5
+ def info_plist_key
6
+ "WechatConfig"
7
+ end
8
+
9
+ # Setup wechat information to rubymotion app, i.e. key, secret...
10
+ #
11
+ # Example: (in Rakefile)
12
+ #
13
+ # Motion::Project::App.setup do |app|
14
+ # ...
15
+ # MotionWechat::Config.setup(app, 'app_key', 'app_secret')
16
+ # end
17
+ #
18
+ # Arguments:
19
+ # RubyMotion app: (Motion::Project::App)
20
+ # Wechat key: (String)
21
+ # Wechat secret: (String)
22
+ # other options: (Hash)
23
+ def setup(app, key, secret, opts={})
24
+ options = {
25
+ bundle_url_name: "weixin",
26
+ key: key,
27
+ secret: secret
28
+ }.merge(opts)
29
+
30
+ app.info_plist[info_plist_key] = options.select { |k| k == :key || k == :secret }
31
+
32
+ bundle_url_types = [
33
+ { 'CFBundleURLName' => "weixin",
34
+ 'CFBundleURLSchemes' => [app.info_plist[info_plist_key][:key]] }
35
+ ]
36
+
37
+ if app.info_plist['CFBundleURLTypes']
38
+ app.info_plist['CFBundleURLTypes'] += bundle_url_types
39
+ else
40
+ app.info_plist['CFBundleURLTypes'] = bundle_url_types
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module MotionWechat
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,92 @@
1
+ describe MotionWechat::API do
2
+
3
+ before { @mv = MotionWechat::API.instance }
4
+
5
+ it "should call register key" do
6
+ WXApi.stub!(:registerApp) do |key|
7
+ key.should == "app_key"
8
+ end
9
+ @mv.register
10
+ end
11
+
12
+ describe "Send messages" do
13
+
14
+ before { @mv.register }
15
+
16
+ it 'sends web page url' do
17
+ WXApi.stub!(:sendReq) do |req|
18
+ req.should.be.kind_of(SendMessageToWXReq)
19
+ req.message.should.be.kind_of(WXMediaMessage)
20
+ req.message.mediaObject.should.be.kind_of(WXWebpageObject)
21
+ req.message.mediaObject.webpageUrl.should == "http://www.motion-wechat.com"
22
+ end
23
+
24
+ MotionWechat::API.instance.send_webpage "http://www.motion-wechat.com", \
25
+ title: "title", description: "description"
26
+ end
27
+
28
+ it 'sends video url' do
29
+ WXApi.stub!(:sendReq) do |req|
30
+ req.message.mediaObject.should.be.kind_of(WXVideoObject)
31
+ req.message.mediaObject.videoUrl.should == "http://www.youtube.com/1"
32
+ end
33
+
34
+ MotionWechat::API.instance.send_video "http://www.youtube.com/1", \
35
+ title: "title", description: "description"
36
+ end
37
+
38
+ it 'sends music url' do
39
+ WXApi.stub!(:sendReq) do |req|
40
+ req.message.mediaObject.should.be.kind_of(WXMusicObject)
41
+ req.message.mediaObject.musicUrl.should == "http://www.pandora.com/1"
42
+ end
43
+
44
+ MotionWechat::API.instance.send_music "http://www.pandora.com/1", \
45
+ title: "title", description: "description"
46
+ end
47
+
48
+ it 'sends image' do
49
+ WXApi.stub!(:sendReq) do |req|
50
+ req.message.mediaObject.should.be.kind_of(WXImageObject)
51
+ end
52
+
53
+ MotionWechat::API.instance.send_image NSData.dataWithContentsOfFile("dummy"), \
54
+ title: "title", description: "description"
55
+ end
56
+
57
+ it 'sends text' do
58
+ WXApi.stub!(:sendReq) do |req|
59
+ req.should.be.kind_of(SendMessageToWXReq)
60
+ req.text.should == "hello"
61
+ end
62
+
63
+ MotionWechat::API.instance.send_text "hello"
64
+ end
65
+
66
+ describe "Scene type" do
67
+
68
+ it "default sends to session" do
69
+ WXApi.stub!(:sendReq) do |req|
70
+ req.should.be.kind_of(SendMessageToWXReq)
71
+ req.scene.should == WXSceneSession
72
+ end
73
+
74
+ MotionWechat::API.instance.send_webpage "http://www.motion-wechat.com", \
75
+ title: "title", description: "description"
76
+ end
77
+
78
+ it "sends to time line" do
79
+ WXApi.stub!(:sendReq) do |req|
80
+ req.should.be.kind_of(SendMessageToWXReq)
81
+ req.scene.should == WXSceneTimeline
82
+ end
83
+
84
+ MotionWechat::API.instance.send_webpage "http://www.motion-wechat.com", \
85
+ title: "title", description: "description", scene: WXSceneTimeline
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,7 @@
1
+ describe MotionWechat::Config do
2
+
3
+ it "has correct info plist key" do
4
+ MotionWechat::Config.info_plist_key.should == 'WechatConfig'
5
+ end
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-wechat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Qi He
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: motion-cocoapods
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ description: Rubymotion gem to easily use WeChatSDK
28
+ email: qhe@heyook.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/motion-wechat.rb
35
+ - lib/motion-wechat/api.rb
36
+ - lib/motion-wechat/config.rb
37
+ - lib/motion-wechat/version.rb
38
+ - spec/motion-wechat/api_spec.rb
39
+ - spec/motion-wechat/config_spec.rb
40
+ homepage: http://github.com/he9qi/motion-wechat
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Rubymotion wrapper for WeChatSDK
64
+ test_files:
65
+ - spec/motion-wechat/api_spec.rb
66
+ - spec/motion-wechat/config_spec.rb