tumblr_client 0.7.5 → 0.8.1

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: 48cdbef82268950444d9073fadec9f7306383b32
4
- data.tar.gz: 563633e9486c24191d1ceef952412858d544c1b4
3
+ metadata.gz: 85e2a013fa7e390e522e3832dbaae337e3a4ff88
4
+ data.tar.gz: a250c8be007a9165f57ab0b69fb1f790da93f78c
5
5
  SHA512:
6
- metadata.gz: 29d29fd7d20ecc63a083414270defa0b408855136747cdbe808ecb8f1f4327fd60382f76884a31a66efd318bc9d4d2bde912a4f4ac45b86c2863cde0fb01e91a
7
- data.tar.gz: 3fee7e452d11a5a873ced187174f25a6b7171ca6be908795d475e9319a4416dace8a5ca33f824467ec3461e334f678cf09513e6f36a6fc88760bfe9da786a532
6
+ metadata.gz: b33406cfec5727d2f3c7960843b00d0bcfa79e3042d90739b08e98fcb33c0abeebe469283b89918b9a3960140e96ca3380065c9b8a9e546cdccebbeb12128499
7
+ data.tar.gz: 588baf76498eb98cc75ba6a83ab8d1c2df1adaf939d99b7a5736f9a5fa1ee3b57a3bbce4bd0a7d19e3e752add705b32b4754236246be41b1b08ef42ba5af32ef
data/README.md CHANGED
@@ -33,6 +33,11 @@ Once you have your configuration squared away it's time to make some requests!
33
33
 
34
34
  That's it! You now have a client that can make any request to the Tumblr API.
35
35
 
36
+ Also since the client is created with the amazing library [Faraday](https://github.com/lostisland/faraday), you can
37
+ configure it to use any HTTP Client it supports.
38
+
39
+ >> client = Tumblr::Client.new(:client => :httpclient)
40
+
36
41
  ### Some quick examples
37
42
 
38
43
  Getting user information:
@@ -53,10 +58,6 @@ Posting some photos to Tumblr:
53
58
  # Uploads a great photoset
54
59
  >> client.photo("codingjester.tumblr.com", {:data => ['/path/to/pic.jpg', '/path/to/pic.jpg']})
55
60
 
56
- # You can also post with the raw data
57
- >> raw = File.open('/path/to/pic.jpg', 'rb').read
58
- >> client.photo('codingjester.tumblr.com', :data_raw => [raw]
59
-
60
61
  ### The irb Console
61
62
 
62
63
  Finally, there is an irb console packaged with the gem that should help you test any calls you want to make.
@@ -5,7 +5,8 @@ module Tumblr
5
5
  :consumer_key,
6
6
  :consumer_secret,
7
7
  :oauth_token,
8
- :oauth_token_secret
8
+ :oauth_token_secret,
9
+ :client
9
10
  ]
10
11
 
11
12
  attr_accessor *VALID_OPTIONS_KEYS
@@ -1,27 +1,30 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
- require 'tumblr/request/oauth'
4
3
 
5
4
  module Tumblr
6
5
  module Connection
7
6
 
8
7
  def connection(options={})
9
- host = api_host
8
+
10
9
  default_options = {
11
10
  :headers => {
12
11
  :accept => 'application/json',
13
12
  :user_agent => "tumblr_client (ruby) - #{Tumblr::VERSION}"
14
13
  },
15
- :url => "http://#{host}/"
14
+ :url => "http://#{api_host}/"
16
15
  }
17
- Faraday.new("http://#{host}/", default_options.merge(options)) do |builder|
18
- data = { :api_host => host }.merge(credentials)
16
+
17
+ client = options[:client] ||= Faraday.default_adapter
18
+
19
+ Faraday.new("http://#{api_host}/", default_options.merge(options)) do |conn|
20
+ data = { :api_host => api_host }.merge(credentials)
19
21
  unless credentials.empty?
20
- builder.use Tumblr::Request::TumblrOAuth, data
22
+ conn.request :oauth, data
21
23
  end
22
- builder.use Faraday::Request::UrlEncoded
23
- builder.use FaradayMiddleware::ParseJson, :content_type => 'application/json'
24
- builder.use Faraday::Adapter::NetHttp
24
+ conn.request :multipart
25
+ conn.request :url_encoded
26
+ conn.response :json, :content_type => /\bjson$/
27
+ conn.adapter client
25
28
  end
26
29
  end
27
30
 
@@ -1,3 +1,5 @@
1
+ require 'mime/types'
2
+
1
3
  module Tumblr
2
4
  module Post
3
5
 
@@ -17,7 +19,7 @@ module Tumblr
17
19
  end
18
20
 
19
21
  def photo(blog_name, options = {})
20
- valid_opts = STANDARD_POST_OPTIONS + [:caption, :link, :data, :data_raw, :source, :photoset_layout]
22
+ valid_opts = STANDARD_POST_OPTIONS + [:caption, :link, :data, :source, :photoset_layout]
21
23
  validate_options(valid_opts, options)
22
24
  validate_no_collision options, [:data, :source]
23
25
  convert_source_array :source, options
@@ -60,7 +62,7 @@ module Tumblr
60
62
  end
61
63
 
62
64
  def audio(blog_name, options = {})
63
- valid_opts = STANDARD_POST_OPTIONS + [:data, :data_raw, :caption, :external_url]
65
+ valid_opts = STANDARD_POST_OPTIONS + [:data, :caption, :external_url]
64
66
  validate_options(valid_opts, options)
65
67
  validate_no_collision options, [:data, :external_url]
66
68
 
@@ -70,7 +72,7 @@ module Tumblr
70
72
  end
71
73
 
72
74
  def video(blog_name, options = {})
73
- valid_opts = STANDARD_POST_OPTIONS + [:data, :data_raw, :embed, :caption]
75
+ valid_opts = STANDARD_POST_OPTIONS + [:data, :embed, :caption]
74
76
  validate_options(valid_opts, options)
75
77
  validate_no_collision options, [:data, :embed]
76
78
 
@@ -98,18 +100,17 @@ module Tumblr
98
100
  # Look for the various ways that data can be passed, and normalize
99
101
  # the result in this hash
100
102
  def extract_data!(options)
101
- validate_no_collision options, [:data, :data_raw]
102
103
  if options.has_key?(:data)
103
104
  data = options.delete :data
104
105
  data = [data] unless Array === data
105
106
  data.each.with_index do |filepath, idx|
106
- options["data[#{idx}]"] = File.open(filepath, 'rb').read
107
- end
108
- elsif options.has_key?(:data_raw)
109
- data_raw = options.delete :data_raw
110
- data_raw = [data_raw] unless Array === data_raw
111
- data_raw.each.with_index do |dr, idx|
112
- options["data[#{idx}]"] = dr
107
+ mime = MIME::Types.type_for(filepath)
108
+ if (!mime.empty?)
109
+ mime_type = MIME::Types.type_for(filepath)[0].content_type
110
+ else
111
+ mime_type = "application/octet-stream"
112
+ end
113
+ options["data[#{idx}]"] = Faraday::UploadIO.new(filepath, mime_type)
113
114
  end
114
115
  end
115
116
  end
@@ -1,5 +1,5 @@
1
1
  module Tumblr
2
2
 
3
- VERSION = '0.7.5'
3
+ VERSION = '0.8.1'
4
4
 
5
5
  end
@@ -108,30 +108,20 @@ describe Tumblr::Post do
108
108
 
109
109
  before do
110
110
  fakefile = OpenStruct.new :read => file_data
111
- File.stub(:open).with(file_path, 'rb').and_return(fakefile)
111
+ File.stub(:open).with(file_path + '.jpg').and_return(fakefile)
112
112
  client.should_receive(:post).once.with("v2/blog/#{blog_name}/post", {
113
- 'data[0]' => file_data,
113
+ 'data[0]' => kind_of(Faraday::UploadIO),
114
114
  :type => type.to_s
115
115
  }).and_return('post')
116
116
  end
117
117
 
118
118
  it 'should be able to pass data as an array of filepaths' do
119
- r = client.send type, blog_name, :data => [file_path]
119
+ r = client.send type, blog_name, :data => [file_path + ".jpg"]
120
120
  r.should == 'post'
121
121
  end
122
122
 
123
123
  it 'should be able to pass data as a single filepath' do
124
- r = client.send type, blog_name, :data => file_path
125
- r.should == 'post'
126
- end
127
-
128
- it 'should be able to pass an array of raw data' do
129
- r = client.send type, blog_name, :data_raw => [file_data]
130
- r.should == 'post'
131
- end
132
-
133
- it 'should be able to pass raw data' do
134
- r = client.send type, blog_name, :data_raw => file_data
124
+ r = client.send type, blog_name, :data => file_path + ".jpg"
135
125
  r.should == 'post'
136
126
  end
137
127
 
@@ -180,12 +170,6 @@ describe Tumblr::Post do
180
170
  }.should raise_error ArgumentError
181
171
  end
182
172
 
183
- it 'should get an error when passing data & raw_data' do
184
- lambda {
185
- client.send type, blog_name, :raw_data => 'hi', :data => 'bye'
186
- }.should raise_error ArgumentError
187
- end
188
-
189
173
  end
190
174
 
191
175
  end
@@ -5,7 +5,9 @@ Gem::Specification.new do |gem|
5
5
  gem.add_dependency 'faraday', '>= 0.8'
6
6
  gem.add_dependency 'faraday_middleware', '>= 0.8'
7
7
  gem.add_dependency 'json'
8
+ gem.add_dependency 'simple_oauth'
8
9
  gem.add_dependency 'oauth'
10
+ gem.add_dependency 'mime-types'
9
11
  gem.add_development_dependency 'rake'
10
12
  gem.add_development_dependency 'rspec'
11
13
  gem.add_development_dependency 'webmock'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tumblr_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bunting
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-16 00:00:00.000000000 Z
12
+ date: 2013-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simple_oauth
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: oauth
58
72
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +81,20 @@ dependencies:
67
81
  - - '>='
68
82
  - !ruby/object:Gem::Version
69
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: mime-types
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
70
98
  - !ruby/object:Gem::Dependency
71
99
  name: rake
72
100
  requirement: !ruby/object:Gem::Requirement
@@ -146,7 +174,6 @@ files:
146
174
  - lib/tumblr/helpers.rb
147
175
  - lib/tumblr/post.rb
148
176
  - lib/tumblr/request.rb
149
- - lib/tumblr/request/oauth.rb
150
177
  - lib/tumblr/tagged.rb
151
178
  - lib/tumblr/user.rb
152
179
  - lib/tumblr/version.rb
@@ -1,72 +0,0 @@
1
- require 'faraday'
2
- require 'time'
3
- require 'uri'
4
- require 'openssl'
5
- require 'base64'
6
-
7
- module Tumblr
8
- module Request
9
- class TumblrOAuth < Faraday::Middleware
10
-
11
- def call(env)
12
- if env[:method].to_s == 'get'
13
- params = Faraday::Utils.parse_query(env[:url].query) || {}
14
- url = "#{env[:url].scheme}://#{env[:url].host}#{env[:url].path}"
15
- else
16
- params = env[:body] || {}
17
- url = env[:url]
18
- end
19
- signature_params = params
20
- params.each do |key, value|
21
- signature_params = {} if value.respond_to?(:content_type)
22
- end
23
- env[:request_headers]['Authorization'] = self.oauth_gen(env[:method], url, signature_params)
24
- env[:request_headers]['Content-type'] = 'application/x-www-form-urlencoded'
25
- env[:request_headers]['Host'] = @options[:api_host]
26
-
27
- @app.call(env)
28
- end
29
-
30
- def initialize(app, options={})
31
- @app, @options = app, options
32
- end
33
-
34
- def oauth_gen(method, url, params)
35
- params[:oauth_consumer_key] = @options[:consumer_key]
36
- params[:oauth_nonce] = Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')
37
- params[:oauth_signature_method] = 'HMAC-SHA1'
38
- params[:oauth_timestamp] = Time.now.to_i
39
- params[:oauth_token] = @options[:token]
40
- params[:oauth_version] = '1.0'
41
- params[:oauth_signature] = self.oauth_sig(method, url, params)
42
-
43
- header = []
44
- params.each do |key, value|
45
- if key.to_s.include?('oauth')
46
- header << "#{key.to_s}=#{value}"
47
- end
48
- end
49
-
50
- "OAuth #{header.join(", ")}"
51
- end
52
-
53
- def oauth_sig(method, url, params)
54
- parts = [method.upcase, URI.encode(url.to_s, /[^a-z0-9\-\.\_\~]/i)]
55
-
56
- #sort the parameters
57
- params = Hash[params.sort_by{ |key, value| key.to_s}]
58
-
59
- encoded = []
60
- params.each do |key, value|
61
- encoded << "#{key.to_s}=#{URI.encode(value.to_s, /[^a-z0-9\-\.\_\~]/i)}"
62
- end
63
-
64
- parts << URI.encode(encoded.join('&'), /[^a-z0-9\-\.\_\~]/i)
65
- signature_base = parts.join('&')
66
- secret = "#{@options[:consumer_secret]}&#{@options[:token_secret]}"
67
- Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, secret, signature_base)).chomp.gsub(/\n/, '')
68
- end
69
-
70
- end
71
- end
72
- end