tumblr-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport"
4
+ gem "oauth", :git => "https://github.com/oauth/oauth-ruby.git"
5
+ gem "hashie"
6
+ gem "rack"
7
+
8
+ group :development do
9
+ gem "webmock"
10
+ gem "rspec"
11
+ gem "mocha"
12
+ gem "shoulda", ">= 0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.6.4"
15
+ gem "rcov", ">= 0"
16
+ end
@@ -0,0 +1,54 @@
1
+ GIT
2
+ remote: https://github.com/oauth/oauth-ruby.git
3
+ revision: 32fdc4cc9f39efe7b34f99a71cab1ba683eb72c9
4
+ specs:
5
+ oauth (0.4.5)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activesupport (3.1.1)
11
+ multi_json (~> 1.0)
12
+ addressable (2.2.6)
13
+ crack (0.3.1)
14
+ diff-lcs (1.1.3)
15
+ git (1.2.5)
16
+ hashie (1.2.0)
17
+ jeweler (1.6.4)
18
+ bundler (~> 1.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ mocha (0.9.8)
22
+ rake
23
+ multi_json (1.0.3)
24
+ rack (1.3.5)
25
+ rake (0.9.2.2)
26
+ rcov (0.9.11)
27
+ rspec (2.7.0)
28
+ rspec-core (~> 2.7.0)
29
+ rspec-expectations (~> 2.7.0)
30
+ rspec-mocks (~> 2.7.0)
31
+ rspec-core (2.7.1)
32
+ rspec-expectations (2.7.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.7.0)
35
+ shoulda (2.11.3)
36
+ webmock (1.7.7)
37
+ addressable (~> 2.2, > 2.2.5)
38
+ crack (>= 0.1.7)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ activesupport
45
+ bundler (~> 1.0.0)
46
+ hashie
47
+ jeweler (~> 1.6.4)
48
+ mocha
49
+ oauth!
50
+ rack
51
+ rcov
52
+ rspec
53
+ shoulda
54
+ webmock
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Max Golovnia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ = tumblr
2
+
3
+ A Ruby wrapper for Tumblr API. Highly inspired by Twitter gem.
4
+
5
+ == Installation
6
+ You can use latest gem:
7
+ gem install tumblr-ruby
8
+
9
+ or in Gemfile:
10
+ gem 'tumblr-ruby'
11
+
12
+ == Usage
13
+ client = Tumblr::Client.new do |client|
14
+ client.consumer_key = your_consumer_key
15
+ client.consumer_secret = your_consumer_secret
16
+ client.oauth_token = your_oauth_token
17
+ client.oauth_token_secret = oauth_token_secret
18
+ end
19
+
20
+ Return posts from a blog
21
+ client.posts("example.tumblr.com")
22
+
23
+ Create text post
24
+ client.text("example.tumblr.com", "Hello, World!")
25
+
26
+ Return user's account information
27
+ client.user_info
28
+
29
+ Use this method to retrieve the dashboard
30
+ client.dashboard
31
+
32
+ == Contributing to tumblr
33
+
34
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
35
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
36
+ * Fork the project
37
+ * Start a feature/bugfix branch
38
+ * Commit and push until you are happy with your contribution
39
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
40
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2011 Max Golovnia. See LICENSE.txt for
45
+ further details.
46
+
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tumblr-ruby"
18
+ gem.homepage = "http://github.com/mgolovnia/tumblr"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Ruby wrapper for Tumblr API}
21
+ gem.description = %Q{A Ruby wrapper for Tumblr API}
22
+ gem.email = "mgolovnia@gmail.com"
23
+ gem.authors = ["Max Golovnia"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core/rake_task'
29
+
30
+ RSpec::Core::RakeTask.new(:spec)
31
+
32
+ task :test => :spec
33
+ task :default => :spec
34
+
35
+ require 'rdoc/task'
36
+ Rake::RDocTask.new do |rdoc|
37
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
38
+
39
+ rdoc.rdoc_dir = 'rdoc'
40
+ rdoc.title = "tumblr #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ require 'tumblr/client'
2
+ module Tumblr
3
+ API_ENDPOINT = "http://api.tumblr.com/"
4
+ API_VERSION = "v2"
5
+
6
+ end
@@ -0,0 +1,23 @@
1
+ require 'tumblr/error'
2
+ require 'tumblr/connection'
3
+ require 'tumblr/request'
4
+ require 'tumblr/client/blog'
5
+ require 'tumblr/client/user'
6
+
7
+ module Tumblr
8
+ class Client
9
+
10
+ attr_accessor :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
11
+
12
+ def initialize
13
+ yield self if block_given?
14
+ end
15
+
16
+ include Tumblr::Error
17
+ include Tumblr::Connection
18
+ include Tumblr::Request
19
+
20
+ include Tumblr::Client::Blog
21
+ include Tumblr::Client::User
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ require 'oauth'
2
+
3
+ module Tumblr
4
+ class Client
5
+ module Blog
6
+
7
+ def blog_info(base_hostname, params={})
8
+ get "/#{API_VERSION}/blog/#{base_hostname}/info", params.merge!("api_key" => consumer_key)
9
+ end
10
+
11
+ def avatar(base_hostname, params={})
12
+ get "/#{API_VERSION}/blog/#{base_hostname}/avatar/#{params.delete("size")}", params
13
+ end
14
+
15
+ def followers(base_hostname, params={})
16
+ get "/#{API_VERSION}/blog/#{base_hostname}/followers", params
17
+ end
18
+
19
+ def posts(base_hostname, params={})
20
+ get "/#{API_VERSION}/blog/#{base_hostname}/posts/#{params.delete("type")}", params.merge!("api_key" => consumer_key)
21
+ end
22
+
23
+ def queue(base_hostname, params={})
24
+ get "/#{API_VERSION}/blog/#{base_hostname}/posts/queue", params
25
+ end
26
+
27
+ def drafts(base_hostname, params={})
28
+ get "/#{API_VERSION}/blog/#{base_hostname}/posts/draft", params
29
+ end
30
+
31
+ def submissions(base_hostname, params={})
32
+ get "/#{API_VERSION}/blog/#{base_hostname}/posts/submission", params
33
+ end
34
+
35
+ def text(base_hostname, body, params={})
36
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params.merge!({"body" => body, "type" => "text"})
37
+ end
38
+
39
+ def photo(base_hostname,source = nil, data=[], params={})
40
+ params["type"] = "photo"
41
+ params["source"] = source if source
42
+ unless data.empty?
43
+ params["data[]"] = data.map{|file| OAuth::Helper.escape(File.open(file).binmode.read) }
44
+ end
45
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params
46
+ end
47
+
48
+ def quote(base_hostname, quote, params={})
49
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params.merge!({"type" => "quote", "quote" => quote})
50
+ end
51
+
52
+ def link(base_hostname, url, params={})
53
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params.merge!({"url" => url, "type" => "link"})
54
+ end
55
+
56
+ def chat(base_hostname, conversation, params={})
57
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params.merge!({"converstion" => conversation, "type" => "chat"})
58
+ end
59
+
60
+ def audio(base_hostname, external_url=nil, data=[] , params={})
61
+
62
+ params["type"] = "audio"
63
+ params["external_url"] = external_url if external_url
64
+ unless data.empty?
65
+ params["data[]"] = data.map{|file| OAuth::Helper.escape(File.open(file).binmode.read) }
66
+ end
67
+
68
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params
69
+ end
70
+
71
+ def video(base_hostname, embed = nil, data = [], params={})
72
+ params["type"] = "video"
73
+ params["embed"] = embed if embed
74
+ unless data.empty?
75
+ params["data[]"] = data.map{|file| OAuth::Helper.escape(File.open(file).binmode.read) }
76
+ end
77
+ post "/#{API_VERSION}/blog/#{base_hostname}/post", params
78
+ end
79
+
80
+ def edit(base_hostname,id , params={})
81
+ post "/#{API_VERSION}/blog/#{base_hostname}/post/edit", params.merge!({"id" => id})
82
+ end
83
+
84
+ def reblog(base_hostname, id, reblog_key, params={})
85
+ post "/#{API_VERSION}/blog/#{base_hostname}/post/reblog", params.merge!({"id" => id , "reblog_key" => reblog_key})
86
+ end
87
+
88
+ def delete_post(base_hostname, id, params={})
89
+ post "/#{API_VERSION}/blog/#{base_hostname}/post/delete", params.merge!({"id" => id})
90
+ end
91
+
92
+ private
93
+
94
+ def check_required_params required_params, params
95
+ required_params.each do |p|
96
+ raise Tumblr::Error::MissingRequiredParameterError.new("Missing required parameter: #{p}") unless params.has_key? p
97
+ end
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,37 @@
1
+ module Tumblr
2
+ class Client
3
+ module User
4
+ def user_info
5
+ get "/#{API_VERSION}/user/info"
6
+ end
7
+
8
+ def dashboard(params={})
9
+ get "/#{API_VERSION}/user/dashboard", params
10
+ end
11
+
12
+ def likes(params={})
13
+ get "/#{API_VERSION}/user/likes", params
14
+ end
15
+
16
+ def following(params={})
17
+ get "/#{API_VERSION}/user/following", params
18
+ end
19
+
20
+ def follow(url)
21
+ post "/#{API_VERSION}/user/follow", {"url" => url}
22
+ end
23
+
24
+ def unfollow(url)
25
+ post "/#{API_VERSION}/user/unfollow", {"url" => url}
26
+ end
27
+
28
+ def like(id, reblog_key)
29
+ post "/#{API_VERSION}/user/like", {"id" => id, "reblog_key" => reblog_key}
30
+ end
31
+
32
+ def unlike(id, reblog_key)
33
+ post "/#{API_VERSION}/user/unlike", {"id" => id, "reblog_key" => reblog_key}
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ require 'oauth'
2
+
3
+ module Tumblr
4
+ module Connection
5
+ private
6
+
7
+ def connection
8
+ @consumer ||= OAuth::Consumer.new consumer_key, consumer_secret, :site => API_ENDPOINT
9
+ @access_token ||= OAuth::AccessToken.new(@consumer, oauth_token, oauth_token_secret)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ module Error
3
+ class TumblrError < StandardError
4
+ attr_reader :data
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ super
9
+ end
10
+ end
11
+
12
+ class MissingRequiredParameterError < TumblrError; end;
13
+ class NotFoundError < TumblrError; end;
14
+ class BadRequestError < TumblrError; end;
15
+ class UnauthorizedError < TumblrError; end;
16
+ class ForbiddenError < TumblrError; end;
17
+ class InternalServerError < TumblrError; end;
18
+ class BadGatewayError < TumblrError; end;
19
+ class ServiceUnavailableError < TumblrError; end;
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ require 'hashie/mash'
2
+ require 'json'
3
+ require 'rack'
4
+
5
+ module Tumblr
6
+ module Request
7
+
8
+ def get(path,params={})
9
+ make_request(:get, build_query(path, params))
10
+ end
11
+
12
+ def post(path, *params)
13
+ make_request(:post, path, *params)
14
+ end
15
+
16
+ def put(path)
17
+ make_request(:put, path)
18
+ end
19
+
20
+ def delete(path)
21
+ make_request(:delete, path)
22
+ end
23
+
24
+ private
25
+
26
+ def build_query(base, params={})
27
+ return base if params.empty?
28
+ "#{base}?#{Rack::Utils.build_query(params)}"
29
+ end
30
+
31
+
32
+ def make_request(method, path, *params)
33
+ response = connection.request(method, path, *params)
34
+ case response.code.to_i
35
+ when 400
36
+ raise Tumblr::Error::BadRequestError.new(response.body)
37
+ when 401
38
+ raise Tumblr::Error::UnauthorizedError.new(response.body)
39
+ when 403
40
+ raise Tumblr::Error::ForbiddenError.new(response.body)
41
+ when 404
42
+ raise Tumblr::Error::NotFoundError.new(response.body)
43
+ when 500
44
+ raise Tumblr::Error::InternalServerError.new(response.body)
45
+ when 502
46
+ raise Tumblr::Error::BadGatewayError.new(response.body)
47
+ when 503
48
+ raise Tumblr::Error::ServiceUnavailableError.new(response.body)
49
+ end
50
+
51
+ response["Content-Type"] == "application/json" ? Hashie::Mash.new(JSON::parse(response.body)) : response.body
52
+ end
53
+ end
54
+
55
+ end