oauth_china 0.1.1 → 0.2.0

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.
@@ -1,73 +1,63 @@
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
1
+ module OauthChina
2
+
3
+ module Multipart
4
+
5
+ class Param
6
+ attr_accessor :k, :v
7
+ def initialize(k, v)
8
+ @k = k
9
+ @v = v
10
+ end
11
+
12
+ def to_multipart
13
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
14
+ # Don't escape mine...
15
+ return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
16
+ end
21
17
  end
22
18
 
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"
19
+ class FileParam
20
+ attr_accessor :k, :filename, :content
21
+ def initialize(k, filename, content)
22
+ @k = k
23
+ @filename = filename
24
+ @content = content
25
+ end
26
+
27
+ def to_multipart
28
+ #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 "
29
+ # Don't escape mine
30
+ 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"
31
+ end
27
32
  end
28
- end
33
+ class MultipartPost
34
+
35
+ BOUNDARY = 'tarsiers-rule0000'
36
+ ContentType = "multipart/form-data; boundary=" + BOUNDARY
37
+
38
+ def set_form_data(req, params)
39
+ body, content_type = prepare_query(params)
40
+ req["Content-Type"] = content_type
41
+ req.body = body
42
+ req["Content-Length"] = body.bytesize
43
+ req
44
+ end
45
+
46
+ def prepare_query(params)
47
+ fp = []
48
+ params.each {|k,v|
49
+ if v.respond_to?(:read)
50
+ fp.push(FileParam.new(k, v.path, v.read))
51
+ v.close
52
+ else
53
+ fp.push(Param.new(k,v))
54
+ end
55
+ }
56
+ body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn"
57
+ return body, ContentType
58
+ end
29
59
 
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
60
 
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
61
  end
43
62
  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
63
  end
@@ -35,35 +35,7 @@ module OauthChina
35
35
  upload("http://api.t.sina.com.cn/statuses/upload.json", options)
36
36
  end
37
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
38
+
67
39
 
68
40
  end
69
41
  end
@@ -2,7 +2,6 @@ module OauthChina
2
2
  class Sohu < OauthChina::OAuth
3
3
 
4
4
  def initialize(*args)
5
- #fuck sohu...!!
6
5
  #搜狐微博的OAUTH 只支持将OAuth的认证参数放在HTTP的头部中。而且不可以带realm参数
7
6
  self.consumer_options = {
8
7
  :site => 'http://api.t.sohu.com',
@@ -31,9 +30,9 @@ module OauthChina
31
30
  self.post("http://api.t.sohu.com/statuses/update.json", options)
32
31
  end
33
32
 
34
- #TODO
35
33
  def upload_image(content, image_path, options = {})
36
- add_status(content, options)
34
+ options = options.merge!(:status => content, :pic => File.open(image_path, "rb")).to_options
35
+ upload("http://api.t.sohu.com/statuses/upload.json", options)
37
36
  end
38
37
 
39
38
  end
@@ -0,0 +1,35 @@
1
+ module OauthChina
2
+ module Upload
3
+
4
+ #NOTICE:
5
+ #各个微博字段名可能不统一
6
+ def upload(url, options)
7
+ url = URI.parse(url)
8
+ http = Net::HTTP.new(url.host, url.port)
9
+
10
+ req = Net::HTTP::Post.new(url.request_uri)
11
+ req = sign_without_pic_field(req, self.access_token, options)
12
+ req = set_multipart_field(req, options)
13
+
14
+ http.request(req)
15
+ end
16
+
17
+ #图片不参与签名
18
+ def sign_without_pic_field(req, access_token, options)
19
+ req.set_form_data(params_without_pic_field(options))
20
+ self.consumer.sign!(req, access_token)
21
+ req
22
+ end
23
+
24
+ #mutipart编码:http://www.ietf.org/rfc/rfc1867.txt
25
+ def set_multipart_field(req, params)
26
+ multipart_post = Multipart::MultipartPost.new
27
+ multipart_post.set_form_data(req, params)
28
+ end
29
+
30
+ def params_without_pic_field(options)
31
+ options.except(:pic)
32
+ end
33
+
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module OauthChina
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/oauth_china.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  require 'rubygems'
2
2
  require 'oauth'
3
- require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/multipart"))
3
+ require 'mime/types'
4
+ require 'net/http'
5
+ require 'cgi'
6
+
7
+ require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/multipart"))
8
+ require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/upload"))
4
9
 
5
10
  module OauthChina
6
11
 
7
12
  class OAuth
8
13
 
9
- include Multipart
14
+ include Upload
10
15
 
11
16
  CONFIG = {}
12
17
 
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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
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-05-17 00:00:00 +08:00
18
+ date: 2011-05-18 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,7 @@ files:
56
56
  - lib/oauth_china/strategies/qq.rb
57
57
  - lib/oauth_china/strategies/sina.rb
58
58
  - lib/oauth_china/strategies/sohu.rb
59
+ - lib/oauth_china/upload.rb
59
60
  - lib/oauth_china/version.rb
60
61
  - oauth_china.gemspec
61
62
  has_rdoc: true