oauth_china 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ *.swp
2
3
  .bundle
3
4
  Gemfile.lock
4
5
  pkg/*
@@ -0,0 +1,73 @@
1
+ module Multipart
2
+ # From: http://deftcode.com/code/flickr_upload/multipartpost.rb
3
+ # Helper class to prepare an HTTP POST request with a file upload
4
+ # Mostly taken from
5
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774
6
+ # WAS:
7
+ # Anything that's broken and wrong probably the fault of Bill Stilwell
8
+ # (bill@marginalia.org)
9
+ # NOW:
10
+ # Everything wrong is due to keith@oreilly.com
11
+ require 'rubygems'
12
+ require 'mime/types'
13
+ require 'net/http'
14
+ require 'cgi'
15
+
16
+ class Param
17
+ attr_accessor :k, :v
18
+ def initialize(k, v)
19
+ @k = k
20
+ @v = v
21
+ end
22
+
23
+ def to_multipart
24
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
25
+ # Don't escape mine...
26
+ return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
27
+ end
28
+ end
29
+
30
+ class FileParam
31
+ attr_accessor :k, :filename, :content
32
+ def initialize(k, filename, content)
33
+ @k = k
34
+ @filename = filename
35
+ @content = content
36
+ end
37
+
38
+ def to_multipart
39
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
40
+ # Don't escape mine
41
+ return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
42
+ end
43
+ end
44
+ class MultipartPost
45
+
46
+ BOUNDARY = 'tarsiers-rule0000'
47
+ ContentType = "multipart/form-data; boundary=" + BOUNDARY
48
+
49
+ def set_form_data(req, params)
50
+ body, content_type = prepare_query(params)
51
+ req["Content-Type"] = content_type
52
+ req.body = body
53
+ req["Content-Length"] = body.bytesize
54
+ req
55
+ end
56
+
57
+ def prepare_query(params)
58
+ fp = []
59
+ params.each {|k,v|
60
+ if v.respond_to?(:read)
61
+ fp.push(FileParam.new(k, v.path, v.read))
62
+ v.close
63
+ else
64
+ fp.push(Param.new(k,v))
65
+ end
66
+ }
67
+ body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn"
68
+ return body, ContentType
69
+ end
70
+
71
+
72
+ end
73
+ end
@@ -39,6 +39,11 @@ module OauthChina
39
39
  XML
40
40
  end
41
41
 
42
+ #TODO
43
+ def upload_image(content, image_path, options = {})
44
+ add_status(content, options)
45
+ end
46
+
42
47
  protected
43
48
  def destroy_access_key
44
49
  response = delete("/access_token/#{access_token.token}")
@@ -30,5 +30,11 @@ module OauthChina
30
30
  options.merge!(:status => content)
31
31
  self.post("http://api.t.163.com/statuses/update.json", options)
32
32
  end
33
+
34
+ #TODO
35
+ def upload_image(content, image_path, options = {})
36
+ add_status(content, options)
37
+ end
38
+
33
39
  end
34
40
  end
@@ -36,5 +36,44 @@ module OauthChina
36
36
  options.merge!(:content => content)
37
37
  self.post("http://open.t.qq.com/api/t/add", options)
38
38
  end
39
+
40
+ #TODO
41
+ #还未实现
42
+ def upload_image(content, image_path, options = {})
43
+ add_status(content, options)
44
+ end
45
+
46
+ # def upload_image(content, image_path, options = {})
47
+ # options = options.merge!(:content => content, :pic => File.open(image_path, "rb")).to_options
48
+ #
49
+ # upload("http://open.t.qq.com/api/t/add_pic", options)
50
+ # end
51
+ #
52
+ # def upload(url, options)
53
+ # url = URI.parse(url)
54
+ # http = Net::HTTP.new(url.host, url.port)
55
+ # req = Net::HTTP::Post.new(url.request_uri)
56
+ # req = sign_without_pic_field(req, self.access_token, options)
57
+ # req = set_multipart_field(req, options)
58
+ #
59
+ # http.request(req)
60
+ #
61
+ # end
62
+ #
63
+ # def sign_without_pic_field(req, access_token, options)
64
+ # req.set_form_data(params_without_pic_field(options))
65
+ # self.consumer.sign!(req, access_token)
66
+ # req
67
+ # end
68
+ #
69
+ # def set_multipart_field(req, params)
70
+ # multipart_post = MultipartPost.new
71
+ # multipart_post.set_form_data(req, params)
72
+ # end
73
+ #
74
+ # def params_without_pic_field(options)
75
+ # options.except(:pic)
76
+ # end
77
+
39
78
  end
40
79
  end
@@ -28,5 +28,42 @@ module OauthChina
28
28
  options.merge!(:status => content)
29
29
  self.post("http://api.t.sina.com.cn/statuses/update.json", options)
30
30
  end
31
+
32
+
33
+ def upload_image(content, image_path, options = {})
34
+ options = options.merge!(:status => content, :pic => File.open(image_path, "rb")).to_options
35
+ upload("http://api.t.sina.com.cn/statuses/upload.json", options)
36
+ end
37
+
38
+ #NOTICE:
39
+ #各个微博字段名可能不统一
40
+ def upload(url, options)
41
+ url = URI.parse(url)
42
+ http = Net::HTTP.new(url.host, url.port)
43
+
44
+ req = Net::HTTP::Post.new(url.request_uri)
45
+ req = sign_without_pic_field(req, self.access_token, options)
46
+ req = set_multipart_field(req, options)
47
+
48
+ http.request(req)
49
+ end
50
+
51
+ #图片不参与签名
52
+ def sign_without_pic_field(req, access_token, options)
53
+ req.set_form_data(params_without_pic_field(options))
54
+ self.consumer.sign!(req, access_token)
55
+ req
56
+ end
57
+
58
+ #mutipart编码:http://www.ietf.org/rfc/rfc1867.txt
59
+ def set_multipart_field(req, params)
60
+ multipart_post = MultipartPost.new
61
+ multipart_post.set_form_data(req, params)
62
+ end
63
+
64
+ def params_without_pic_field(options)
65
+ options.except(:pic)
66
+ end
67
+
31
68
  end
32
69
  end
@@ -30,5 +30,11 @@ module OauthChina
30
30
  options.merge!(:status => content)
31
31
  self.post("http://api.t.sohu.com/statuses/update.json", options)
32
32
  end
33
+
34
+ #TODO
35
+ def upload_image(content, image_path, options = {})
36
+ add_status(content, options)
37
+ end
38
+
33
39
  end
34
40
  end
@@ -1,3 +1,3 @@
1
1
  module OauthChina
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/oauth_china.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  require 'rubygems'
2
2
  require 'oauth'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/multipart"))
3
4
 
4
5
  module OauthChina
5
6
 
6
7
  class OAuth
7
8
 
9
+ include Multipart
10
+
8
11
  CONFIG = {}
9
12
 
10
13
  attr_accessor :request_token, :access_token, :consumer_options
@@ -77,4 +80,4 @@ module OauthChina
77
80
  autoload :Qq, 'oauth_china/strategies/qq'
78
81
  autoload :Sohu, 'oauth_china/strategies/sohu'
79
82
  autoload :Netease, 'oauth_china/strategies/netease'
80
- end
83
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth_china
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
4
+ hash: 25
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hooopo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-26 00:00:00 +08:00
18
+ date: 2011-05-17 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,7 @@ files:
50
50
  - README.md
51
51
  - Rakefile
52
52
  - lib/oauth_china.rb
53
+ - lib/oauth_china/multipart.rb
53
54
  - lib/oauth_china/strategies/douban.rb
54
55
  - lib/oauth_china/strategies/netease.rb
55
56
  - lib/oauth_china/strategies/qq.rb
@@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements: []
88
89
 
89
90
  rubyforge_project: oauth_china
90
- rubygems_version: 1.6.2
91
+ rubygems_version: 1.3.7
91
92
  signing_key:
92
93
  specification_version: 3
93
94
  summary: !binary |