Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weixin_authorize
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
weixin_authorize
Commits
b23ec14b
Commit
b23ec14b
authored
Apr 16, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
upload media from remote
parent
c174eb04
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
3 deletions
+28
-3
lib/weixin_authorize.rb
+2
-0
lib/weixin_authorize/api/media.rb
+13
-3
spec/api/media_spec.rb
+11
-0
spec/spec_helper.rb
+2
-0
No files found.
lib/weixin_authorize.rb
View file @
b23ec14b
require
"rest-client"
require
"rest-client"
require
"multi_json"
require
"multi_json"
require
"carrierwave"
require
"weixin_authorize/carrierwave/weixin_uploader"
require
"weixin_authorize/config"
require
"weixin_authorize/config"
require
"weixin_authorize/handler"
require
"weixin_authorize/handler"
require
"weixin_authorize/api"
require
"weixin_authorize/api"
...
...
lib/weixin_authorize/api/media.rb
View file @
b23ec14b
# encoding: utf-8
# encoding: utf-8
module
WeixinAuthorize
module
WeixinAuthorize
module
Api
module
Api
module
Media
module
Media
# 上传多媒体文件
# 上传多媒体文件
# http请求方式: POST/FORM
# http请求方式: POST/FORM
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
# 支持传路径或者文件类型
# 支持传路径或者文件类型
def
upload_media
(
media
,
media_type
)
def
upload_media
(
media
,
media_type
,
remote
=
false
)
file
=
media
.
is_a?
(
File
)
?
media
:
File
.
new
(
media
)
file
=
process_file
(
media
,
remote
)
upload_media_url
=
"
#{
media_base_url
}
/upload"
upload_media_url
=
"
#{
media_base_url
}
/upload"
http_post
(
upload_media_url
,
{
media:
file
},
{
type:
media_type
},
"file"
)
http_post
(
upload_media_url
,
{
media:
file
},
{
type:
media_type
},
"file"
)
end
end
...
@@ -24,6 +24,16 @@ module WeixinAuthorize
...
@@ -24,6 +24,16 @@ module WeixinAuthorize
private
private
def
process_file
(
media
,
remote
)
if
remote
base
=
WeixinUploader
.
new
base
.
download!
(
media
.
to_s
)
base
.
file
.
to_file
else
media
.
is_a?
(
File
)
?
media
:
File
.
new
(
media
)
end
end
def
media_base_url
def
media_base_url
"/media"
"/media"
end
end
...
...
spec/api/media_spec.rb
View file @
b23ec14b
...
@@ -10,6 +10,11 @@ describe WeixinAuthorize::Api::Media do
...
@@ -10,6 +10,11 @@ describe WeixinAuthorize::Api::Media do
File
.
new
(
image_path
)
File
.
new
(
image_path
)
end
end
let
(
:remote_image_path
)
do
# "http://l.ruby-china.org/user/large_avatar/273.png"
"http://g.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ce55457e4334970a537e187df4a3baad/03087bf40ad162d99455ef4d13dfa9ec8b13632762d0ed14.jpg"
end
it
"can upload a image"
do
it
"can upload a image"
do
response
=
$client
.
upload_media
(
image_file
,
"image"
)
response
=
$client
.
upload_media
(
image_file
,
"image"
)
expect
(
response
.
code
).
to
eq
(
WeixinAuthorize
::
OK_CODE
)
expect
(
response
.
code
).
to
eq
(
WeixinAuthorize
::
OK_CODE
)
...
@@ -23,4 +28,10 @@ describe WeixinAuthorize::Api::Media do
...
@@ -23,4 +28,10 @@ describe WeixinAuthorize::Api::Media do
expect
(
response
.
class
).
to
eq
(
String
)
expect
(
response
.
class
).
to
eq
(
String
)
end
end
it
"can upload a remote image"
do
response
=
$client
.
upload_media
(
remote_image_path
,
"image"
,
true
)
expect
(
response
.
code
).
to
eq
(
WeixinAuthorize
::
OK_CODE
)
expect
(
response
.
result
.
keys
).
to
eq
([
"type"
,
"media_id"
,
"created_at"
])
end
end
end
spec/spec_helper.rb
View file @
b23ec14b
...
@@ -23,6 +23,8 @@ require 'coveralls'
...
@@ -23,6 +23,8 @@ require 'coveralls'
require
'simplecov'
require
'simplecov'
require
"codeclimate-test-reporter"
require
"codeclimate-test-reporter"
require
"pry-rails"
Coveralls
.
wear!
Coveralls
.
wear!
SimpleCov
.
formatter
=
SimpleCov
::
Formatter
::
MultiFormatter
[
SimpleCov
.
formatter
=
SimpleCov
::
Formatter
::
MultiFormatter
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment