Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed the version number from the HTTParty dependency

  • Loading branch information...
commit ef9d0be8ff44ccc92aaebf1c629b3c90203286d7 0 parents
Jeff Kreeftmeijer authored
5 .document
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
7 .gitignore
@@ -0,0 +1,7 @@
+*.sw?
+.DS_Store
+coverage
+rdoc
+pkg
+website
+tumblr_example
9 History
@@ -0,0 +1,9 @@
+0.0.9 - Removed the version number from the HTTParty dependency
+0.0.8 - The example app slipped in. Removed it.
+0.0.7 - Somethig went wrong, pushing again...
+0.0.6 - Fixed a bug in Post::find_from_id (the post-id wasn't being passed to the api)
+0.0.5 - Finished README.rdoc!
+0.0.4 - Updated README.rdoc, removed the docs folder (http://rdoc.info/projects/jeffkreeftmeijer/tumblr)
+0.0.3 - Forgot to update the gemspec. Here it goes! :)
+0.0.2 - Added the docs
+0.0.1 - Initial Release
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Jeff Kreeftmeijer
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81 README.rdoc
@@ -0,0 +1,81 @@
+= Tumblr
+
+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.
+
+== Authentication
+
+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:
+
+ user = Tumblr::User.new('your@email.com', 'yourpassword')
+
+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:
+
+ user = Tumblr::User.new('your@email.com', 'yourpassword', false)
+
+== Fetching Posts
+
+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:
+
+ Tumblr.blog = 'myblog'
+
+When just fetching posts, you don't have to worry about authentication, so let's go and get your posts now, shall we?
+
+ @posts = Tumblr::Post.all
+
+Congratulations! Now you've got all - with a maximum of 50 - of your posts!
+
+Want the first/last post? No problem:
+
+ @posts = Tumblr::Post.first # gets the first post it can find
+ @posts = Tumblr::Post.last # gets the last post it can find
+
+And when you want a specific post, you can always just do this:
+
+ @posts = Tumblr::Post.find(12345) # gets the post with ID = 12345
+
+== Parameters
+
+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!
+
+ @posts = Tumblr::Post.all(:type => 'photo') # gets all posts with type = 'photo'
+ @posts = Tumblr::Post.all(:filter => 'text') # gets all posts in plain text
+
+Please check out http://www.tumblr.com/docs/api for Tumblr's full documentation about their API and the parameters.
+
+== Authenticated read
+
+To be able to fetch private posts, you have to be authenticated. Just include the User object (see Authentication above) like so:
+
+ @posts = Tumblr::Post.all(user)
+
+== Creating posts
+
+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.
+
+This is an example of a "regular" text post:
+
+ post = Tumblr::Post.create(user, :type => 'regular', :title => 'Tumblr', :body => 'Tumblr is a rails gem that allows you to use the Tumblr API.')
+
+After a post is successfully created, Tumblr will return the newly created post's id.
+
+== Updating posts
+
+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.):
+
+ 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.')
+
+After a post is successfully updated, Tumblr will return the post's id.
+
+== Destroying posts
+
+Just provide the user object and the +post_id+ of the post you'd like to destroy:
+
+ Tumblr::Post.destroy(user, :post_id => 12345)
+
+== Docs
+
+http://rdoc.info/projects/jeffkreeftmeijer/tumblr
+
+== Copyright
+
+Copyright (c) 2009 Jeff Kreeftmeijer. See LICENSE for details.
57 Rakefile
@@ -0,0 +1,57 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "tumblr"
+ gem.summary = %Q{TODO}
+ gem.email = "jeff@kreeftmeijer.nl"
+ gem.homepage = "http://github.com/jeffkreeftmeijer/tumblr"
+ gem.authors = ["Jeff Kreeftmeijer"]
+ gem.add_dependency('httparty')
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ if File.exist?('VERSION.yml')
+ config = YAML.load(File.read('VERSION.yml'))
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
+ else
+ version = ""
+ end
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "tumblr #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
1  VERSION
@@ -0,0 +1 @@
+0.0.9
15 lib/tumblr.rb
@@ -0,0 +1,15 @@
+require 'httparty'
+
+module Tumblr
+ mattr_accessor :blog
+
+ # tumblr errors
+ class TumblrError < StandardError; end
+ # tumblr 403 errors
+ class Forbidden < TumblrError; end
+ # tumblr 400 errors
+ class BadRequest < TumblrError; end
+ # tumblr 404 errors
+ class Notfound < TumblrError; end
+
+end
97 lib/tumblr/post.rb
@@ -0,0 +1,97 @@
+module Tumblr
+ class Post
+
+ # works just like ActiveRecord's find. (:all, :first, :last or id)
+ def self.find(*args)
+ options = args.extract_options!
+
+ if((user = args.second).is_a?(Tumblr::User))
+ options = options.merge(
+ :email => user.email,
+ :password => user.password
+ )
+ end
+
+ case args.first
+ when :first then find_initial(options)
+ when :last then find_last(options)
+ when :all then find_every(options)
+ else find_from_id(args.first, options)
+ end
+ end
+
+ # find the first post
+ def self.find_initial(options)
+ Tumblr::Request.read(options.merge(:start => 0, :num => 1))
+ end
+
+ # find the last post
+ def self.find_last(options)
+ total = all['tumblr']['posts']['total'].to_i
+ Tumblr::Request.read(options.merge(:start => total - 1, :num => 1))
+ end
+
+ # find all posts (the maximum amount of posts is 50, don't blame the messenger)
+ def self.find_every(options)
+ Tumblr::Request.read(options.merge(:num => 50))
+ end
+
+ # find a post by id
+ def self.find_from_id(id, options)
+ Tumblr::Request.read(options.merge(:id => id))
+ end
+
+ # alias of find(:all)
+ def self.all(*args)
+ self.find(:all, *args)
+ end
+
+ # alias of find(:first)
+ def self.first(*args)
+ self.find(:first, *args)
+ end
+
+ # alias of find(:last)
+ def self.last(*args)
+ self.find(:last, *args)
+ end
+
+ # create a new post
+ def self.create(*args)
+ options = process_options(*args)
+ Tumblr::Request.write(options)
+ end
+
+ # update a post
+ def self.update(*args)
+ options = process_options(*args)
+ Tumblr::Request.write(options)
+ end
+
+ # destroy a post
+ def self.destroy(*args)
+ options = process_options(*args)
+ Tumblr::Request.delete(options)
+ end
+
+ # extracts options from the arguments, converts a user object to :email and :password params and fixes the :post_id/'post-id' issue.
+ def self.process_options(*args)
+ options = args.extract_options!
+
+ if((user = args.first).is_a?(Tumblr::User))
+ options = options.merge(
+ :email => user.email,
+ :password => user.password
+ )
+ end
+
+ if(options[:post_id])
+ options['post-id'] = options[:post_id]
+ options[:post_id] = nil
+ end
+
+ return options
+ end
+ end
+end
+
50 lib/tumblr/request.rb
@@ -0,0 +1,50 @@
+module Tumblr
+ class Request
+ # a GET request to http://[YOURUSERNAME].tumblr.com/api/read
+ def self.read(options = {})
+ Tumblr::blog ||= 'staff'
+ response = HTTParty.get("http://#{Tumblr::blog}.tumblr.com/api/read/", :query => options)
+ return(response) unless raise_errors(response)
+ end
+
+ # a POST request to http://www.tumblr.com/api/write
+ def self.write(options = {})
+ response = HTTParty.post('http://www.tumblr.com/api/write', :query => options)
+ return(response) unless raise_errors(response)
+ end
+
+ # a POST request to http://www.tumblr.com/api/delete
+ def self.delete(options = {})
+ response = HTTParty.post('http://www.tumblr.com/api/delete', :query => options)
+ return(response) unless raise_errors(response)
+ end
+
+ # a POST request to http://www.tumblr.com/api/authenticate
+ def self.authenticate(email, password)
+ HTTParty.post('http://www.tumblr.com/api/authenticate', :query => {:email => email, :password => password})
+ end
+
+ # raise tumblr response errors.
+ def self.raise_errors(response)
+ if(response.is_a?(Hash))
+ message = "#{response[:code]}: #{response[:body]}"
+ code = response[:code].to_i
+ else
+ message = "#{response.code}: #{response.body}"
+ code = response.code.to_i
+ end
+
+ case code
+ when 403
+ raise(Forbidden.new, message)
+ when 400
+ raise(BadRequest.new, message)
+ when 404
+ raise(Notfound.new, message)
+ when 201
+ return false
+ end
+ end
+ end
+end
+
14 lib/tumblr/user.rb
@@ -0,0 +1,14 @@
+module Tumblr
+ class User
+ attr_accessor :email, :password, :tumblr
+
+ # creates a User object and authenticates the user through the Tumblr API to get user data.
+ def initialize(email, password, authenticate = true)
+ self.email = email
+ self.password = password
+ if(authenticate)
+ self.tumblr = Tumblr::Request.authenticate(email,password)['tumblr']
+ end
+ end
+ end
+end
10 test/test_helper.rb
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'test/unit'
+require 'shoulda'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'tumblr'
+
+class Test::Unit::TestCase
+end
7 test/tumblr_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TumblrTest < Test::Unit::TestCase
+ should "probably rename this file and start testing for real" do
+ flunk "hey buddy, you should probably rename this file and start testing for real"
+ end
+end
54 tumblr.gemspec
@@ -0,0 +1,54 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{tumblr}
+ s.version = "0.0.9"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jeff Kreeftmeijer"]
+ s.date = %q{2009-05-15}
+ s.email = %q{jeff@kreeftmeijer.nl}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "History",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/tumblr.rb",
+ "lib/tumblr/post.rb",
+ "lib/tumblr/request.rb",
+ "lib/tumblr/user.rb",
+ "test/test_helper.rb",
+ "test/tumblr_test.rb",
+ "tumblr.gemspec"
+ ]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/jeffkreeftmeijer/tumblr}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{TODO}
+ s.test_files = [
+ "test/test_helper.rb",
+ "test/tumblr_test.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
+ else
+ s.add_dependency(%q<httparty>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<httparty>, [">= 0"])
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.