zapnap-tumblr 0.0.9

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ website
7
+ tumblr_example
data/History ADDED
@@ -0,0 +1,9 @@
1
+ 0.0.9 - Removed the version number from the HTTParty dependency
2
+ 0.0.8 - The example app slipped in. Removed it.
3
+ 0.0.7 - Somethig went wrong, pushing again...
4
+ 0.0.6 - Fixed a bug in Post::find_from_id (the post-id wasn't being passed to the api)
5
+ 0.0.5 - Finished README.rdoc!
6
+ 0.0.4 - Updated README.rdoc, removed the docs folder (http://rdoc.info/projects/jeffkreeftmeijer/tumblr)
7
+ 0.0.3 - Forgot to update the gemspec. Here it goes! :)
8
+ 0.0.2 - Added the docs
9
+ 0.0.1 - Initial Release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jeff Kreeftmeijer
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.
data/README.rdoc ADDED
@@ -0,0 +1,81 @@
1
+ = Tumblr
2
+
3
+ Tumblr is a rails gem that allows you to use the Tumblr API. The idea is that you don't have to worry about HTTP requests, you just want to fetch your posts right? But there's more; you can also create, update and destroy Tumblr posts and do so as railsy as possible.
4
+
5
+ == Authentication
6
+
7
+ To create, update or delete a post (or fetch private posts), you should be authenticated. The only thing you have to do is call initialize the User class by doing this:
8
+
9
+ user = Tumblr::User.new('your@email.com', 'yourpassword')
10
+
11
+ The +user+ object holds your +email+ and +password+. If also calls to the API to get the user's data. If you don't want to call to the API and just get a user object, call it like this:
12
+
13
+ user = Tumblr::User.new('your@email.com', 'yourpassword', false)
14
+
15
+ == Fetching Posts
16
+
17
+ Fetching posts is easy. The first thing you have to do is tell the Tumblr gem which blog we're talking about. So, If your blog is located on http://myblog.tumblr.com you do this:
18
+
19
+ Tumblr.blog = 'myblog'
20
+
21
+ When just fetching posts, you don't have to worry about authentication, so let's go and get your posts now, shall we?
22
+
23
+ @posts = Tumblr::Post.all
24
+
25
+ Congratulations! Now you've got all - with a maximum of 50 - of your posts!
26
+
27
+ Want the first/last post? No problem:
28
+
29
+ @posts = Tumblr::Post.first # gets the first post it can find
30
+ @posts = Tumblr::Post.last # gets the last post it can find
31
+
32
+ And when you want a specific post, you can always just do this:
33
+
34
+ @posts = Tumblr::Post.find(12345) # gets the post with ID = 12345
35
+
36
+ == Parameters
37
+
38
+ Tumblr allows you to pass some parameters to you requests. In the functions described above, the Tumblr gem automatically sets the +start+, +num+ and/or +id+ parameters. But you can add more!
39
+
40
+ @posts = Tumblr::Post.all(:type => 'photo') # gets all posts with type = 'photo'
41
+ @posts = Tumblr::Post.all(:filter => 'text') # gets all posts in plain text
42
+
43
+ Please check out http://www.tumblr.com/docs/api for Tumblr's full documentation about their API and the parameters.
44
+
45
+ == Authenticated read
46
+
47
+ To be able to fetch private posts, you have to be authenticated. Just include the User object (see Authentication above) like so:
48
+
49
+ @posts = Tumblr::Post.all(user)
50
+
51
+ == Creating posts
52
+
53
+ To create a post, you should include the authentication object. More information about the parameters you have to include to create posts can be found in the Tumblr API Docs -> http://www.tumblr.com/docs/api.
54
+
55
+ This is an example of a "regular" text post:
56
+
57
+ post = Tumblr::Post.create(user, :type => 'regular', :title => 'Tumblr', :body => 'Tumblr is a rails gem that allows you to use the Tumblr API.')
58
+
59
+ After a post is successfully created, Tumblr will return the newly created post's id.
60
+
61
+ == Updating posts
62
+
63
+ Updating posts is a lot like creating posts. The only thing you have to do is provide the id of the post you wish to edit (the parameters +type+, +private+ and +format+ are ignored and can be omitted.):
64
+
65
+ post = Tumblr::Post.create(user, :post_id => '12345', :title => 'Tumblr', :body => 'Tumblr is a super awesome rails gem that allows you to use the Tumblr API.')
66
+
67
+ After a post is successfully updated, Tumblr will return the post's id.
68
+
69
+ == Destroying posts
70
+
71
+ Just provide the user object and the +post_id+ of the post you'd like to destroy:
72
+
73
+ Tumblr::Post.destroy(user, :post_id => 12345)
74
+
75
+ == Docs
76
+
77
+ http://rdoc.info/projects/jeffkreeftmeijer/tumblr
78
+
79
+ == Copyright
80
+
81
+ Copyright (c) 2009 Jeff Kreeftmeijer. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tumblr"
8
+ gem.summary = %q{Tumblr API wrapper}
9
+ gem.email = "jeff@kreeftmeijer.nl"
10
+ gem.homepage = "http://github.com/jeffkreeftmeijer/tumblr"
11
+ gem.authors = ["Jeff Kreeftmeijer"]
12
+ gem.add_dependency('httparty')
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "tumblr #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.9
data/lib/tumblr.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'httparty'
2
+ require 'active_support'
3
+
4
+ require 'tumblr/user'
5
+ require 'tumblr/request'
6
+ require 'tumblr/post'
7
+
8
+ module Tumblr
9
+ mattr_accessor :blog
10
+
11
+ # tumblr errors
12
+ class TumblrError < StandardError; end
13
+ # tumblr 403 errors
14
+ class Forbidden < TumblrError; end
15
+ # tumblr 400 errors
16
+ class BadRequest < TumblrError; end
17
+ # tumblr 404 errors
18
+ class NotFound < TumblrError; end
19
+
20
+ end
@@ -0,0 +1,97 @@
1
+ module Tumblr
2
+ class Post
3
+
4
+ # works just like ActiveRecord's find. (:all, :first, :last or id)
5
+ def self.find(*args)
6
+ options = args.extract_options!
7
+
8
+ if((user = args.second).is_a?(Tumblr::User))
9
+ options = options.merge(
10
+ :email => user.email,
11
+ :password => user.password
12
+ )
13
+ end
14
+
15
+ case args.first
16
+ when :first then find_initial(options)
17
+ when :last then find_last(options)
18
+ when :all then find_every(options)
19
+ else find_from_id(args.first, options)
20
+ end
21
+ end
22
+
23
+ # find the first post
24
+ def self.find_initial(options)
25
+ Tumblr::Request.read(options.merge(:start => 0, :num => 1))
26
+ end
27
+
28
+ # find the last post
29
+ def self.find_last(options)
30
+ total = all['tumblr']['posts']['total'].to_i
31
+ Tumblr::Request.read(options.merge(:start => total - 1, :num => 1))
32
+ end
33
+
34
+ # find all posts (the maximum amount of posts is 50, don't blame the messenger)
35
+ def self.find_every(options)
36
+ Tumblr::Request.read(options.merge(:num => 50))
37
+ end
38
+
39
+ # find a post by id
40
+ def self.find_from_id(id, options)
41
+ Tumblr::Request.read(options.merge(:id => id))
42
+ end
43
+
44
+ # alias of find(:all)
45
+ def self.all(*args)
46
+ self.find(:all, *args)
47
+ end
48
+
49
+ # alias of find(:first)
50
+ def self.first(*args)
51
+ self.find(:first, *args)
52
+ end
53
+
54
+ # alias of find(:last)
55
+ def self.last(*args)
56
+ self.find(:last, *args)
57
+ end
58
+
59
+ # create a new post
60
+ def self.create(*args)
61
+ options = process_options(*args)
62
+ Tumblr::Request.write(options)
63
+ end
64
+
65
+ # update a post
66
+ def self.update(*args)
67
+ options = process_options(*args)
68
+ Tumblr::Request.write(options)
69
+ end
70
+
71
+ # destroy a post
72
+ def self.destroy(*args)
73
+ options = process_options(*args)
74
+ Tumblr::Request.delete(options)
75
+ end
76
+
77
+ # extracts options from the arguments, converts a user object to :email and :password params and fixes the :post_id/'post-id' issue.
78
+ def self.process_options(*args)
79
+ options = args.extract_options!
80
+
81
+ if((user = args.first).is_a?(Tumblr::User))
82
+ options = options.merge(
83
+ :email => user.email,
84
+ :password => user.password
85
+ )
86
+ end
87
+
88
+ if(options[:post_id])
89
+ options['post-id'] = options[:post_id]
90
+ options[:post_id] = nil
91
+ end
92
+
93
+ return options
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,50 @@
1
+ module Tumblr
2
+ class Request
3
+ # a GET request to http://[YOURUSERNAME].tumblr.com/api/read
4
+ def self.read(options = {})
5
+ Tumblr::blog ||= 'staff'
6
+ response = HTTParty.get("http://#{Tumblr::blog}.tumblr.com/api/read/", :query => options)
7
+ return(response) unless raise_errors(response)
8
+ end
9
+
10
+ # a POST request to http://www.tumblr.com/api/write
11
+ def self.write(options = {})
12
+ response = HTTParty.post('http://www.tumblr.com/api/write', :query => options)
13
+ return(response) unless raise_errors(response)
14
+ end
15
+
16
+ # a POST request to http://www.tumblr.com/api/delete
17
+ def self.delete(options = {})
18
+ response = HTTParty.post('http://www.tumblr.com/api/delete', :query => options)
19
+ return(response) unless raise_errors(response)
20
+ end
21
+
22
+ # a POST request to http://www.tumblr.com/api/authenticate
23
+ def self.authenticate(email, password)
24
+ HTTParty.post('http://www.tumblr.com/api/authenticate', :query => {:email => email, :password => password})
25
+ end
26
+
27
+ # raise tumblr response errors.
28
+ def self.raise_errors(response)
29
+ if(response.is_a?(Hash))
30
+ message = "#{response[:code]}: #{response[:body]}"
31
+ code = response[:code].to_i
32
+ else
33
+ message = "#{response.code}: #{response.body}"
34
+ code = response.code.to_i
35
+ end
36
+
37
+ case code
38
+ when 403
39
+ raise(Forbidden.new, message)
40
+ when 400
41
+ raise(BadRequest.new, message)
42
+ when 404
43
+ raise(NotFound.new, message)
44
+ when 201
45
+ return false
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,14 @@
1
+ module Tumblr
2
+ class User
3
+ attr_accessor :email, :password, :tumblr
4
+
5
+ # creates a User object and authenticates the user through the Tumblr API to get user data.
6
+ def initialize(email, password, authenticate = true)
7
+ self.email = email
8
+ self.password = password
9
+ if(authenticate)
10
+ self.tumblr = Tumblr::Request.authenticate(email,password)['tumblr']
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'tumblr'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class TumblrTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
data/tumblr.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{tumblr}
5
+ s.version = "0.0.9"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jeff Kreeftmeijer"]
9
+ s.date = %q{2009-05-15}
10
+ s.email = %q{jeff@kreeftmeijer.nl}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "History",
19
+ "LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/tumblr.rb",
24
+ "lib/tumblr/post.rb",
25
+ "lib/tumblr/request.rb",
26
+ "lib/tumblr/user.rb",
27
+ "test/test_helper.rb",
28
+ "test/tumblr_test.rb",
29
+ "tumblr.gemspec"
30
+ ]
31
+ s.has_rdoc = true
32
+ s.homepage = %q{http://github.com/jeffkreeftmeijer/tumblr}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.1}
36
+ s.summary = %q{Tumblr API wrapper}
37
+ s.test_files = [
38
+ "test/test_helper.rb",
39
+ "test/tumblr_test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 2
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<httparty>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<httparty>, [">= 0"])
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zapnap-tumblr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Kreeftmeijer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: jeff@kreeftmeijer.nl
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - History
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/tumblr.rb
43
+ - lib/tumblr/post.rb
44
+ - lib/tumblr/request.rb
45
+ - lib/tumblr/user.rb
46
+ - test/test_helper.rb
47
+ - test/tumblr_test.rb
48
+ - tumblr.gemspec
49
+ has_rdoc: true
50
+ homepage: http://github.com/jeffkreeftmeijer/tumblr
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Tumblr API wrapper
75
+ test_files:
76
+ - test/test_helper.rb
77
+ - test/tumblr_test.rb