weixin_authorize 1.5.8 → 1.5.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af6979103c92161f0e5e190e4e2e3050abf5aa4a
4
- data.tar.gz: ad4f77bbc0a0745f87762c500a14f23e2fc10778
3
+ metadata.gz: fa3f0b2aefa861605741941f844ab85f1ac94643
4
+ data.tar.gz: 5c03d38761626e496e6c4bddea72ad852030d393
5
5
  SHA512:
6
- metadata.gz: b98d1b3412cadc32b2c62790f0c97a265ebf93120baf611fa40e261f2f477f531c328dc575fe44cda8cecbfeb1197c15d53e608df76c12bf2e8912f7b870749f
7
- data.tar.gz: 79ef616a6877b6790aca143d08bf571919e47be2d41581f7aa2535690f2e588d15d07bdd32d2493d3e0dc9d68888be756781c05dc34a4df0aeea503215eb9a31
6
+ metadata.gz: ac931c8e944a128b00598578d642fd6a7d65ec396fa95cfa6f5b2b79fdd9be79f8604c623a6268d8d85eea01a05489e1dfd0a816f9df7693063ba2aa10f7ee49
7
+ data.tar.gz: 604d3424ebac5db36b727828a1155645d1354c4857e678891fe2c64f0f84bfe917f10ce07d23df271d5611bc302d4064a4b01278c144e131ea11ee08763c1c92
data/Gemfile CHANGED
@@ -1,13 +1,12 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "rspec", "~> 3.0.0.beta1"
4
- gem "redis-namespace", "~> 1.4.1"
5
- gem "rake", "~> 0.9.6"
6
- gem 'simplecov', '~> 0.7.1', :require => false
7
- gem "codeclimate-test-reporter", require: nil
8
- gem 'coveralls', require: false
9
-
10
- group :development do
3
+ group :test, :development do
4
+ gem "rspec", "~> 3.0.0.beta1"
5
+ gem "redis-namespace", "~> 1.4.1"
6
+ gem "rake", "~> 0.9.6"
7
+ gem 'simplecov', '~> 0.7.1', :require => false
8
+ gem "codeclimate-test-reporter", require: nil
9
+ gem 'coveralls', require: false
11
10
  # For debugger
12
11
  gem "pry-rails", "~> 0.3.2"
13
12
 
@@ -1,5 +1,10 @@
1
1
  require "rest-client"
2
- require "multi_json"
2
+
3
+ require "carrierwave"
4
+ require "weixin_authorize/carrierwave/weixin_uploader"
5
+
6
+ require 'yajl/json_gem'
7
+
3
8
  require "weixin_authorize/config"
4
9
  require "weixin_authorize/handler"
5
10
  require "weixin_authorize/api"
@@ -25,7 +30,7 @@ module WeixinAuthorize
25
30
 
26
31
  def http_post_without_token(url, payload={}, headers={}, endpoint="plain")
27
32
  post_api_url = endpoint_url(endpoint, url)
28
- payload = MultiJson.dump(payload) if endpoint == "plain" # to json if invoke "plain"
33
+ payload = JSON.dump(payload) if endpoint == "plain" # to json if invoke "plain"
29
34
  load_json(RestClient.post(post_api_url, payload, :params => headers))
30
35
  end
31
36
 
@@ -1,14 +1,14 @@
1
1
  # encoding: utf-8
2
+
2
3
  module WeixinAuthorize
3
4
  module Api
4
5
  module Media
5
-
6
6
  # 上传多媒体文件
7
7
  # http请求方式: POST/FORM
8
8
  # http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
9
9
  # 支持传路径或者文件类型
10
10
  def upload_media(media, media_type)
11
- file = media.is_a?(File) ? media : File.new(media)
11
+ file = process_file(media)
12
12
  upload_media_url = "#{media_base_url}/upload"
13
13
  http_post(upload_media_url, {media: file}, {type: media_type}, "file")
14
14
  end
@@ -24,9 +24,67 @@ module WeixinAuthorize
24
24
 
25
25
  private
26
26
 
27
+ def process_file(media)
28
+ return media if media.is_a?(File) && jpep?(media)
29
+
30
+ media_url = media
31
+ uploader = WeixinUploader.new
32
+
33
+ if http?(media_url) # remote
34
+ uploader.download!(media_url.to_s)
35
+ else # local
36
+ media_file = media.is_a?(File) ? media : File.new(media_url)
37
+ uploader.cache!(media_file)
38
+ end
39
+ file = process_media(uploader)
40
+ CarrierWave.clean_cached_files! # clear last one day cache
41
+ file
42
+ end
43
+
27
44
  def media_base_url
28
45
  "/media"
29
46
  end
47
+
48
+ def process_media(uploader)
49
+ uploader = covert(uploader)
50
+ uploader.file.to_file
51
+ end
52
+
53
+ # JUST ONLY FOR JPG IMAGE
54
+ def covert(uploader)
55
+ # image process
56
+ unless (uploader.file.content_type =~ /image/).nil?
57
+ if !jpep?(uploader.file)
58
+ require "mini_magick"
59
+ # covert to jpeg
60
+ image = MiniMagick::Image.open(uploader.path)
61
+ image.format("jpg")
62
+ uploader.cache!(File.open(image.path))
63
+ image.destroy! # remove /tmp from MinMagick generate
64
+ end
65
+ end
66
+ uploader
67
+ end
68
+
69
+ def http?(uri)
70
+ return false if !uri.is_a?(String)
71
+ uri = URI.parse(uri)
72
+ uri.scheme =~ /^https?$/
73
+ end
74
+
75
+ def jpep?(file)
76
+ content_type = if file.respond_to?(:content_type)
77
+ file.content_type
78
+ else
79
+ content_type(file.path)
80
+ end
81
+ !(content_type =~ /jpeg/).nil?
82
+ end
83
+
84
+ def content_type(media_path)
85
+ MIME::Types.type_for(media_path).first.content_type
86
+ end
87
+
30
88
  end
31
89
  end
32
90
  end
@@ -20,7 +20,7 @@ module WeixinAuthorize
20
20
  # 自定义菜单创建接口
21
21
  # https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
22
22
  def create_menu(menu)
23
- menu = MultiJson.load(menu) if menu.is_a?(String)
23
+ menu = JSON.load(menu) if menu.is_a?(String)
24
24
  create_menu_url = "#{menu_base_url}/create"
25
25
  http_post(create_menu_url, menu)
26
26
  end
@@ -0,0 +1,4 @@
1
+ module WeixinAuthorize
2
+ class WeixinUploader < CarrierWave::Uploader::Base
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module WeixinAuthorize
2
- VERSION = "1.5.8"
2
+ VERSION = "1.5.9"
3
3
  end
@@ -2,25 +2,71 @@ require "spec_helper"
2
2
 
3
3
  describe WeixinAuthorize::Api::Media do
4
4
 
5
- let(:image_path) do
5
+ let(:image_jpg_path) do
6
6
  "#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
7
7
  end
8
8
 
9
- let(:image_file) do
10
- File.new(image_path)
9
+ let(:image_ico_path) do
10
+ "#{File.dirname(__FILE__)}/medias/favicon.ico"
11
11
  end
12
12
 
13
- it "can upload a image" do
14
- response = $client.upload_media(image_file, "image")
13
+ let(:image_jpg_file) do
14
+ File.new(image_jpg_path)
15
+ end
16
+
17
+ let(:image_ico_file) do
18
+ File.new(image_ico_path)
19
+ end
20
+
21
+ let(:remote_png_path) do
22
+ "http://l.ruby-china.org/user/large_avatar/273.png"
23
+ end
24
+
25
+ let(:remote_jpg_path) do
26
+ "http://g.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ce55457e4334970a537e187df4a3baad/03087bf40ad162d99455ef4d13dfa9ec8b13632762d0ed14.jpg"
27
+ end
28
+
29
+ it "can upload a jpg File image" do
30
+ response = $client.upload_media(image_jpg_file, "image")
31
+ expect(response.code).to eq(WeixinAuthorize::OK_CODE)
32
+ expect(response.result.keys).to eq(["type", "media_id", "created_at"])
33
+ end
34
+
35
+ it "can upload a ico File image" do
36
+ response = $client.upload_media(image_ico_file, "image")
37
+ expect(response.code).to eq(WeixinAuthorize::OK_CODE)
38
+ expect(response.result.keys).to eq(["type", "media_id", "created_at"])
39
+ end
40
+
41
+ it "can upload a local image" do
42
+ response = $client.upload_media(image_jpg_path, "image")
43
+ expect(response.code).to eq(WeixinAuthorize::OK_CODE)
44
+ expect(response.result.keys).to eq(["type", "media_id", "created_at"])
45
+ end
46
+
47
+ it "can upload a local ico image" do
48
+ response = $client.upload_media(image_ico_path, "image")
49
+ expect(response.code).to eq(WeixinAuthorize::OK_CODE)
50
+ expect(response.result.keys).to eq(["type", "media_id", "created_at"])
51
+ end
52
+
53
+ it "can upload a remote png image" do
54
+ response = $client.upload_media(remote_png_path, "image")
55
+ expect(response.code).to eq(WeixinAuthorize::OK_CODE)
56
+ expect(response.result.keys).to eq(["type", "media_id", "created_at"])
57
+ end
58
+
59
+ it "can upload a remote jpg image" do
60
+ response = $client.upload_media(remote_jpg_path, "image")
15
61
  expect(response.code).to eq(WeixinAuthorize::OK_CODE)
16
62
  expect(response.result.keys).to eq(["type", "media_id", "created_at"])
17
63
  end
18
64
 
19
65
  it "#download_media_url return a String url" do
20
- image = $client.upload_media(image_file, "image")
66
+ image = $client.upload_media(image_ico_path, "image")
21
67
  media_id = image.result["media_id"]
22
- response = $client.download_media_url(media_id)
23
- expect(response.class).to eq(String)
68
+ image_url = $client.download_media_url(media_id)
69
+ expect(image_url.class).to eq(String)
24
70
  end
25
71
 
26
72
  end
Binary file
@@ -5,7 +5,7 @@ describe WeixinAuthorize::Api::Menu do
5
5
 
6
6
  it "can create a menu" do
7
7
  menu = '{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}'
8
- response = $client.create_menu(MultiJson.load(menu)) # or Json string
8
+ response = $client.create_menu(JSON.load(menu)) # or Json string
9
9
  expect(response.code).to eq(WeixinAuthorize::OK_CODE)
10
10
  end
11
11
 
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,7 @@
15
15
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
16
  require "rspec"
17
17
  require "weixin_authorize"
18
- require "multi_json"
18
+ require 'yajl/json_gem'
19
19
  require "redis"
20
20
  require "redis-namespace"
21
21
 
@@ -23,6 +23,8 @@ require 'coveralls'
23
23
  require 'simplecov'
24
24
  require "codeclimate-test-reporter"
25
25
 
26
+ require "pry-rails"
27
+
26
28
  Coveralls.wear!
27
29
 
28
30
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
@@ -19,9 +19,15 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "rest-client", ">= 1.6.7"
22
- spec.add_dependency "multi_json", "~> 1.9.0"
23
22
  spec.add_dependency "redis", "~> 3.0.7"
24
23
 
24
+ spec.add_dependency "carrierwave", "~> 0.10.0"
25
+ spec.add_dependency 'mini_magick', '~> 3.7.0'
26
+
27
+ # A streaming JSON parsing and encoding library for Ruby (C bindings to yajl)
28
+ # https://github.com/brianmario/yajl-ruby
29
+ spec.add_dependency "yajl-ruby", "~> 1.2.0"
30
+
25
31
  spec.add_development_dependency "bundler", "~> 1.3"
26
32
  spec.add_development_dependency "rake"
27
33
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weixin_authorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.8
4
+ version: 1.5.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - lanrion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -25,33 +25,61 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.6.7
27
27
  - !ruby/object:Gem::Dependency
28
- name: multi_json
28
+ name: redis
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 1.9.0
33
+ version: 3.0.7
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 1.9.0
40
+ version: 3.0.7
41
41
  - !ruby/object:Gem::Dependency
42
- name: redis
42
+ name: carrierwave
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.7
47
+ version: 0.10.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.7
54
+ version: 0.10.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mini_magick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.7.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.7.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: yajl-ruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.0
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +134,7 @@ files:
106
134
  - lib/weixin_authorize/api/menu.rb
107
135
  - lib/weixin_authorize/api/qrcode.rb
108
136
  - lib/weixin_authorize/api/user.rb
137
+ - lib/weixin_authorize/carrierwave/weixin_uploader.rb
109
138
  - lib/weixin_authorize/client.rb
110
139
  - lib/weixin_authorize/config.rb
111
140
  - lib/weixin_authorize/handler.rb
@@ -117,6 +146,7 @@ files:
117
146
  - spec/api/custom_spec.rb
118
147
  - spec/api/groups_spec.rb
119
148
  - spec/api/media_spec.rb
149
+ - spec/api/medias/favicon.ico
120
150
  - spec/api/medias/ruby-logo.jpg
121
151
  - spec/api/menu_spec.rb
122
152
  - spec/api/qrcode_spec.rb
@@ -152,6 +182,7 @@ test_files:
152
182
  - spec/api/custom_spec.rb
153
183
  - spec/api/groups_spec.rb
154
184
  - spec/api/media_spec.rb
185
+ - spec/api/medias/favicon.ico
155
186
  - spec/api/medias/ruby-logo.jpg
156
187
  - spec/api/menu_spec.rb
157
188
  - spec/api/qrcode_spec.rb