tumblr_wrapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2-p290@tumblr_wrapper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tumblr_wrapper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 rheaton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # TumblrWrapper
2
+
3
+ This is a basic wrapper for the tumblr api written in ruby. It requires Oauth and RestClient.
4
+ I haven't wrapped the response objects yet.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'tumblr_wrapper'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install tumblr_wrapper
19
+
20
+ ## Usage
21
+
22
+ Set up:
23
+
24
+ TumblrWrapper.consumer_key = "YOUR APPLICATION KEY"
25
+ TumberWrapper.consumer_secret = "YOUR APPLICATION SECRET"
26
+
27
+ If you need the token and secret for a user:
28
+
29
+ client = TumblrWrapper::Client.new
30
+
31
+ then authorize in browser here:
32
+
33
+ client.authorize_url
34
+
35
+ tumblr oauth is strict, you need to save the oauth verifier from the response:
36
+
37
+ access_token = client.access_token("OAUTH VERIFIER")
38
+
39
+ If you have the token and secret for your user:
40
+
41
+ access_token = client.access_token_from_hash(oauth_token: "TOKEN", oauth_token_secret: "SECRET")
42
+
43
+ The access token is memoized in either case, so you can call it again once it was set:
44
+
45
+ access_token = client.access_token
46
+
47
+ To get the first 40 posts of your blog:
48
+
49
+ blog_resource = TumblrWrapper::BlogResource.new('yourblog.tumblr.com', access_token)
50
+
51
+ blog_resource.blog.posts(limit: 40)
52
+
53
+ or with pagination:
54
+
55
+ blog_resource.blog.posts(limit: 20, offset: 0)
56
+ blog_resource.blog.posts(limit: 20, offset: 20)
57
+
58
+ To post:
59
+
60
+ blog_resource.post.create({ type: 'text', body: "Hello world." })
61
+
62
+ To delete a post:
63
+
64
+ blog_resource.post.delete({id: "ID FROM TUMBLR"})
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ class TumblrWrapper::Blog < TumblrWrapper::BlogResource
2
+ ##
3
+ # Required parameters: N/A
4
+ # http://www.tumblr.com/docs/en/api/v2#blog-info
5
+ def info
6
+ request('info')
7
+ end
8
+
9
+ ##
10
+ # Required parameters: N/A
11
+ # http://www.tumblr.com/docs/en/api/v2#blog-followers
12
+ def followers(parameters={})
13
+ oauth_request('followers', parameters)
14
+ end
15
+
16
+ ##
17
+ # Required parameters: N/A
18
+ # http://www.tumblr.com/docs/en/api/v2#posts
19
+ def posts(parameters={})
20
+ request('posts', parameters)
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ class TumblrWrapper::BlogResource < TumblrWrapper::Resource
2
+ attr_reader :hostname, :access_token
3
+
4
+ def initialize(base_hostname, access_token=nil)
5
+ @hostname = base_hostname
6
+ super(access_token)
7
+ end
8
+
9
+ def blog
10
+ TumblrWrapper::Blog.new(@hostname, access_token)
11
+ end
12
+
13
+ def post
14
+ TumblrWrapper::Post.new(@hostname, access_token)
15
+ end
16
+
17
+ protected
18
+ def oauth_request(path, method_type = :get, body = {})
19
+ validate_oauth
20
+
21
+ url = "#{endpoint}/blog/#{hostname}/#{path}"
22
+ if parameters.present?
23
+ access_token.request(method_type, url, body)
24
+ else
25
+ access_token.request(method_type, url)
26
+ end
27
+ end
28
+
29
+ def request(path, method_type = :get, parameters = {})
30
+ parameters.merge!({ api_key: TumblrWrapper.consumer_key })
31
+ params = (method_type == :get) ? { params: parameters } : parameters
32
+
33
+ TumblrWrapper::RestClient.send(method_type, "#{endpoint}/blog/#{hostname}/#{path}", params)
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ class TumblrWrapper::Client
2
+ attr_reader :consumer, :request_token
3
+ def initialize
4
+ @consumer = OAuth::Consumer.new(
5
+ TumblrWrapper.consumer_key,
6
+ TumblrWrapper.consumer_secret,
7
+ site: 'http://www.tumblr.com'
8
+ )
9
+ end
10
+
11
+ def authorize_url
12
+ @request_token = @consumer.get_request_token
13
+ @request_token.authorize_url
14
+ end
15
+
16
+ def access_token(oauth_verifier = nil)
17
+ if oauth_verifier
18
+ @access_token = request_token.get_access_token({:oauth_verifier => oauth_verifier})
19
+ else
20
+ @access_token
21
+ end
22
+ end
23
+
24
+ def access_token_from_hash(token_hash)
25
+ @access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
26
+ end
27
+
28
+ def token
29
+ @access_token.try(:token)
30
+ end
31
+
32
+ def secret
33
+ @access_token.try(:secret)
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ class TumblrWrapper::Post < TumblrWrapper::BlogResource
2
+ ##
3
+ # Required parameters: :type
4
+ # http://www.tumblr.com/docs/en/api/v2#posting
5
+ def create(parameters = {})
6
+ validate_present(:type, parameters)
7
+ validate_not_present(:id, parameters)
8
+
9
+ oauth_request('post', :post, parameters)
10
+ end
11
+
12
+ ##
13
+ # Required parameters: :id
14
+ # http://www.tumblr.com/docs/en/api/v2#editing
15
+ def update(parameters = {})
16
+ validate_present(:id, parameters)
17
+ oauth_request('post/edit', :post, parameters)
18
+ end
19
+
20
+ ##
21
+ # Required parameters: :id
22
+ # http://www.tumblr.com/docs/en/api/v2#editing
23
+ def delete(parameters = {})
24
+ validate_present(:id, parameters)
25
+ oauth_request('post/delete', :post, parameters)
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ class TumblrWrapper::Resource
2
+ attr_reader :access_token
3
+
4
+ def initialize(access_token)
5
+ @access_token = access_token
6
+ end
7
+
8
+ def validate_oauth
9
+ raise TumblrWrapper::OauthNeeded unless access_token
10
+ end
11
+
12
+ def validate_present(key, parameters)
13
+ raise TumblrWrapper::MissingRequiredParameter, key unless parameters[key]
14
+ end
15
+
16
+ def validate_not_present(key, parameters)
17
+ raise TumblrWrapper::InvalidParameter, key if parameters[key]
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ class TumblrWrapper::RestClient < ::RestClient
2
+ end
@@ -0,0 +1,3 @@
1
+ module TumblrWrapper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'oauth'
2
+ require 'rest-client'
3
+ require "tumblr_wrapper/version"
4
+ require "tumblr_wrapper/client"
5
+ require "tumblr_wrapper/resource"
6
+ require "tumblr_wrapper/blog_resource"
7
+ require "tumblr_wrapper/blog"
8
+ require "tumblr_wrapper/post"
9
+
10
+ module TumblrWrapper
11
+ ENDPOINT = 'http://api.tumblr.com/v2'
12
+ OAUTH_ENPOINT = 'http://www.tumblr.com'
13
+
14
+ def self.consumer_key=(key)
15
+ @@consumer_key = key
16
+ end
17
+
18
+ def self.consumer_key
19
+ @@consumer_key
20
+ end
21
+
22
+ def self.consumer_secret=(secret)
23
+ @@consumer_secret = secret
24
+ end
25
+
26
+ def self.consumer_secret
27
+ @@consumer_secret
28
+ end
29
+
30
+ def self.oauth_endpoint
31
+ OAUTH_ENDPOINT
32
+ end
33
+
34
+ def self.endpoint
35
+ ENDPOINT
36
+ end
37
+
38
+ class NoAccessToken < StandardError
39
+ def initialize(message = "Oauth request. Set access_token")
40
+ super(message)
41
+ end
42
+ end
43
+
44
+ class MissingRequiredParameter < StandardError
45
+ end
46
+
47
+ class InvalidParameter < StandardError
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe TumblrWrapper::BlogResource do
4
+ let(:token) { mock(:token) }
5
+ subject(:blog_resource) { TumblrWrapper::BlogResource.new('myblog.tumblr.com') }
6
+
7
+ describe "blog" do
8
+ it "returns a new blog with the hostname and access token" do
9
+ blog = blog_resource.blog
10
+ blog.class.should == TumblrWrapper::Blog
11
+
12
+ blog.access_token.should == blog_resource.access_token
13
+ blog.hostname.should == blog_resource.hostname
14
+ end
15
+ end
16
+
17
+ describe "post" do
18
+ it "returns a new post with the hostname and access token" do
19
+ post = blog_resource.post
20
+ post.class.should == TumblrWrapper::Post
21
+
22
+ post.access_token.should == blog_resource.access_token
23
+ post.hostname.should == blog_resource.hostname
24
+ end
25
+ end
26
+ end
data/spec/blog_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe TumblrWrapper::Blog do
4
+ let(:token) { mock(:token) }
5
+ subject(:blog) { TumblrWrapper::Blog.new('myblog.tumblr.com') }
6
+
7
+ it "is a blog resource" do
8
+ TumblrWrapper::Blog.superclass.should == TumblrWrapper::BlogResource
9
+ end
10
+
11
+ describe '.info' do
12
+ it "makes an api request" do
13
+ blog.should_receive(:request).with('info')
14
+
15
+ blog.info
16
+ end
17
+ end
18
+
19
+ describe "followers" do
20
+ it "makes an oauth request" do
21
+ blog.should_receive(:oauth_request).with('followers', {})
22
+
23
+ blog.followers
24
+ end
25
+
26
+ it "accepts parameters" do
27
+ blog.should_receive(:oauth_request).with('followers', { limit: 20, offset: 5 })
28
+
29
+ blog.followers({ limit: 20, offset: 5 })
30
+ end
31
+ end
32
+
33
+ describe "posts" do
34
+ it "makes an api request" do
35
+ blog.should_receive(:request).with('posts', {})
36
+
37
+ blog.posts
38
+ end
39
+
40
+ it "accepts type parameter" do
41
+ blog.should_receive(:request).with('posts', type: 'text')
42
+
43
+ blog.posts(type: 'text')
44
+ end
45
+ end
46
+ end
data/spec/post_spec.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe TumblrWrapper::Post do
4
+ let(:token) { mock(:token) }
5
+ subject(:post) { TumblrWrapper::Post.new('myblog.tumblr.com') }
6
+
7
+ it "is a blog resource" do
8
+ TumblrWrapper::Post.superclass.should == TumblrWrapper::BlogResource
9
+ end
10
+
11
+ describe "create" do
12
+ it "makes an oauth request with parameters" do
13
+ post.should_receive(:oauth_request).with('post', :post, {:type => 'text'})
14
+
15
+ post.create({:type => 'text'})
16
+ end
17
+
18
+ it "raises unless type is specified" do
19
+ lambda do
20
+ post.create
21
+ end.should raise_error(TumblrWrapper::MissingRequiredParameter)
22
+ end
23
+
24
+ it "raises if id is sent" do
25
+ lambda do
26
+ post.create({type: 'text', id: '1234'})
27
+ end.should raise_error(TumblrWrapper::InvalidParameter)
28
+ end
29
+ end
30
+
31
+ describe "update" do
32
+ it "makes an oauth request with parameters" do
33
+ post.should_receive(:oauth_request).with('post/edit', :post, {:id => '1234'})
34
+
35
+ post.update({:id => '1234'})
36
+ end
37
+
38
+ it "raises unless id is specified" do
39
+ lambda do
40
+ post.update
41
+ end.should raise_error(TumblrWrapper::MissingRequiredParameter)
42
+ end
43
+ end
44
+
45
+ describe "delete" do
46
+ it "makes an oauth request with parameters" do
47
+ post.should_receive(:oauth_request).with('post/delete', :post, {:id => '1234'})
48
+
49
+ post.delete({:id => '1234'})
50
+ end
51
+
52
+ it "raises unless id is specified" do
53
+ lambda do
54
+ post.delete
55
+ end.should raise_error(TumblrWrapper::MissingRequiredParameter)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+
4
+ Spork.prefork do
5
+ require 'rspec'
6
+ require 'tumblr_wrapper'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rspec
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tumblr_wrapper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["rheaton"]
6
+ gem.email = ["rachelmheaton@gmail.com"]
7
+ gem.description = %q{ruby wrapper for tumblr api}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(spec)/})
14
+ gem.name = "tumblr_wrapper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TumblrWrapper::VERSION
17
+ gem.add_development_dependency 'spork'
18
+ gem.add_development_dependency 'rspec'
19
+ gem.add_dependency 'oauth'
20
+ gem.add_dependency 'rest-client'
21
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumblr_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - rheaton
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-08-01 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: spork
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: oauth
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: rest-client
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :runtime
71
+ version_requirements: *id004
72
+ description: ruby wrapper for tumblr api
73
+ email:
74
+ - rachelmheaton@gmail.com
75
+ executables: []
76
+
77
+ extensions: []
78
+
79
+ extra_rdoc_files: []
80
+
81
+ files:
82
+ - .gitignore
83
+ - .rvmrc
84
+ - Gemfile
85
+ - LICENSE
86
+ - README.md
87
+ - Rakefile
88
+ - lib/tumblr_wrapper.rb
89
+ - lib/tumblr_wrapper/blog.rb
90
+ - lib/tumblr_wrapper/blog_resource.rb
91
+ - lib/tumblr_wrapper/client.rb
92
+ - lib/tumblr_wrapper/post.rb
93
+ - lib/tumblr_wrapper/resource.rb
94
+ - lib/tumblr_wrapper/rest_client.rb
95
+ - lib/tumblr_wrapper/version.rb
96
+ - spec/blog_resource_spec.rb
97
+ - spec/blog_spec.rb
98
+ - spec/post_spec.rb
99
+ - spec/spec_helper.rb
100
+ - tumblr_wrapper.gemspec
101
+ has_rdoc: true
102
+ homepage: ""
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.3.7
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: ""
133
+ test_files:
134
+ - spec/blog_resource_spec.rb
135
+ - spec/blog_spec.rb
136
+ - spec/post_spec.rb
137
+ - spec/spec_helper.rb