Permalink
Browse files

Re-wrote the Post class (the read methods)

  • Loading branch information...
1 parent 9d68710 commit 1fd434e49b280f4477facede6c6f12375e688a27 @jeffkreeftmeijer committed Jul 15, 2009
View
@@ -1,3 +1,4 @@
+0.1.0 - Re-wrote the Post class (the read methods)
0.0.10 - Fixed dependencies
0.0.9 - Removed the version number from the HTTParty dependency
0.0.8 - The example app slipped in. Removed it.
View
@@ -18,28 +18,20 @@ 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
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
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
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
end
-task :default => :test
+task :default => :spec
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
@@ -54,5 +46,4 @@ Rake::RDocTask.new do |rdoc|
rdoc.title = "tumblr #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
+end
View
@@ -1,4 +1,4 @@
---
-:minor: 0
-:patch: 10
+:minor: 1
+:patch: 0
:major: 0
View
@@ -1,12 +1,12 @@
+require 'rubygems'
require 'httparty'
-require 'active_support'
require 'tumblr/user'
require 'tumblr/request'
require 'tumblr/post'
-module Tumblr
- mattr_accessor :blog
+class Tumblr
+ class << self; attr_accessor :blog end
# tumblr errors
class TumblrError < StandardError; end
@@ -15,6 +15,6 @@ class Forbidden < TumblrError; end
# tumblr 400 errors
class BadRequest < TumblrError; end
# tumblr 404 errors
- class NotFound < TumblrError; end
+ class NotFound < TumblrError; end
-end
+end
View
@@ -1,59 +1,81 @@
-module Tumblr
+class 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
-
+ extra_options = args.last.is_a?(Hash) ? args.pop : {}
+
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
+ when :all then return self.find_every(extra_options)
+ when :first then return self.find_initial(extra_options)
+ when :last then return self.find_last(extra_options)
+ else return self.find_from_id(args.first)
+ end
end
-
+
+ # count the posts
+ def self.count(options = {})
+ response = Tumblr::Request.read({:num => 1}.merge(options))
+ response['tumblr']['posts']['total'].to_i
+ end
+
# find the first post
def self.find_initial(options)
- Tumblr::Request.read(options.merge(:start => 0, :num => 1))
+ total = self.count
+ options = {:start => (total - 1), :num => 1} if(options.empty?)
+
+ puts options.to_yaml
+
+ response = Tumblr::Request.read(options)
+
+ return response['tumblr']['posts']['post'].first unless(options == {:start => (total - 1), :num => 1})
+ response['tumblr']['posts']['post']
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))
+ response = Tumblr::Request.read({:num => 1}.merge(options))
+ response['tumblr']['posts']['post']
end
-
- # find all posts (the maximum amount of posts is 50, don't blame the messenger)
+
+ # find all posts
def self.find_every(options)
- Tumblr::Request.read(options.merge(:num => 50))
+ amount = (Tumblr::Post.count(options).to_f / 50).ceil
+ options = {:num => 50}.merge(options)
+
+ responses = []
+ amount.times do |count|
+ responses << Tumblr::Request.read(options.merge({:start => (count.to_i * 50)}))
+ end
+
+ response = {'tumblr' => {'posts' => {'post' => []}}}
+ responses.each do |r|
+ r['tumblr']['posts']['post'].each { | p | response['tumblr']['posts']['post'] << p }
+ end
+
+ return [response['tumblr']['posts']['post']] unless(response['tumblr']['posts']['post'].is_a?(Array))
+ response['tumblr']['posts']['post']
end
# find a post by id
- def self.find_from_id(id, options)
- Tumblr::Request.read(options.merge(:id => id))
+ def self.find_from_id(id)
+ response = Tumblr::Request.read(:id => id)
+ response['tumblr']['posts']['post']
end
-
+
# alias of find(:all)
- def self.all(*args)
- self.find(:all, *args)
+ def self.all(options = {})
+ self.find(:all, options)
end
# alias of find(:first)
- def self.first(*args)
- self.find(:first, *args)
- end
+ def self.first(options = {})
+ self.find(:first, options)
+ end
# alias of find(:last)
- def self.last(*args)
- self.find(:last, *args)
+ def self.last(options = {})
+ self.find(:last, options)
end
# create a new post
@@ -76,7 +98,7 @@ def self.destroy(*args)
# 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!
+ options = args.last.is_a?(Hash) ? args.pop : {}
if((user = args.first).is_a?(Tumblr::User))
options = options.merge(
@@ -93,5 +115,4 @@ def self.process_options(*args)
return options
end
end
-end
-
+end
View
@@ -1,10 +1,10 @@
-module Tumblr
+class 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)
+ response = HTTParty.get("http://#{Tumblr::blog ||= 'staff'}.tumblr.com/api/read", options)
+ return response unless raise_errors(response)
end
# a POST request to http://www.tumblr.com/api/write
@@ -45,6 +45,6 @@ def self.raise_errors(response)
return false
end
end
+
end
-end
-
+end
@@ -0,0 +1,39 @@
+<tumblr version='1.0'>
+<tumblelog name='jeffkreeftmeijer' timezone='US/Eastern' title='Jeff Kreeftmeijer'>
+<feeds>
+<feed id='508097' url='http://twitter.com/statuses/user_timeline/8284992.rss' import-type='regular-no-title' next-update-in-seconds='1487' title='Twitter / jkreeftmeijer'/>
+</feeds>
+</tumblelog>
+<posts start='0' total='98'>
+<post id='140920842' url='http://jeffkreeftmeijer.tumblr.com/post/140920842' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140920842/making-final-tweaks-to-awesomescaffold-yes' type='regular' date-gmt='2009-07-13 18:26:57 GMT' date='Mon, 13 Jul 2009 14:26:57' unix-timestamp='1247509617' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2617420039' from-feed-id='508097'>
+<regular-body>Making final tweaks to AwesomeScaffold (yes, that&amp;#8217;s the name of the #rspec/#cucumber/#haml scaffold genegrator) for the initial commit. :)</regular-body>
+</post>
+<post id='140920832' url='http://jeffkreeftmeijer.tumblr.com/post/140920832' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140920832/yay-got-an-alpha-invite-to-kohive-checking-it' type='regular' date-gmt='2009-07-13 18:26:56 GMT' date='Mon, 13 Jul 2009 14:26:56' unix-timestamp='1247509616' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2617889835' from-feed-id='508097'>
+<regular-body>Yay! Got an alpha invite to @&lt;a href="http://twitter.com/kohive">kohive&lt;/a>. Checking it out now :) Thanks a lot @&lt;a href="http://twitter.com/EdwardVII">EdwardVII&lt;/a></regular-body>
+</post>
+<post id='140837000' url='http://jeffkreeftmeijer.tumblr.com/post/140837000' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140837000/just-updated-the-tumblr-ror-gem' type='regular' date-gmt='2009-07-13 15:38:07 GMT' date='Mon, 13 Jul 2009 11:38:07' unix-timestamp='1247499487' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2614945546' from-feed-id='508097'>
+<regular-body>just updated the #tumblr #ror #gem (&lt;a href="http://bit.ly/19G5uu">http://bit.ly/19G5uu&lt;/a>) Thanks a lot @&lt;a href="http://twitter.com/zapnap">zapnap&lt;/a>! :)</regular-body>
+</post>
+<post id='140742811' url='http://jeffkreeftmeijer.tumblr.com/post/140742811' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140742811/writing-cucumber-scenarios-for-the-scaffold' type='regular' date-gmt='2009-07-13 12:16:21 GMT' date='Mon, 13 Jul 2009 08:16:21' unix-timestamp='1247487381' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2612563426' from-feed-id='508097'>
+<regular-body>Writing cucumber scenarios for the scaffold generator. Pretty hard when you don&amp;#8217;t know what you&amp;#8217;re working with&amp;#8230;</regular-body>
+</post>
+<post id='140639182' url='http://jeffkreeftmeijer.tumblr.com/post/140639182' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140639182/would-love-to-be-able-to-auto-download-new-podcast' type='regular' date-gmt='2009-07-13 07:34:17 GMT' date='Mon, 13 Jul 2009 03:34:17' unix-timestamp='1247470457' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2610252266' from-feed-id='508097'>
+<regular-body>Would love to be able to auto-download new podcast episodes directly to my iPhone. (like iTunes does)</regular-body>
+</post>
+<post id='140639176' url='http://jeffkreeftmeijer.tumblr.com/post/140639176' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140639176/spent-some-time-writing-a-custom-rails-scaffold' type='regular' date-gmt='2009-07-13 07:34:16 GMT' date='Mon, 13 Jul 2009 03:34:16' unix-timestamp='1247470456' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2611048999' from-feed-id='508097'>
+<regular-body>Spent some time writing a custom rails scaffold generator the other day. It generates rspec and cucumber tests and uses haml for templating.</regular-body>
+</post>
+<post id='140639181' url='http://jeffkreeftmeijer.tumblr.com/post/140639181' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/140639181/girlfriend-bought-an-usb-flash-drive-it-came-with' type='regular' date-gmt='2009-07-13 07:34:16 GMT' date='Mon, 13 Jul 2009 03:34:16' unix-timestamp='1247470456' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2611009558' from-feed-id='508097'>
+<regular-body>Girlfriend bought an USB flash drive. It came with &amp;#8220;essential software&amp;#8221;: firefox, vlc and more free stuff. Awful sales strategy&amp;#8230;</regular-body>
+</post>
+<post id='139298154' url='http://jeffkreeftmeijer.tumblr.com/post/139298154' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/139298154/lyt-inman-great-serve' type='regular' date-gmt='2009-07-10 23:11:16 GMT' date='Fri, 10 Jul 2009 19:11:16' unix-timestamp='1247267476' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2575183864' from-feed-id='508097'>
+<regular-body>#lyt #inman, great serve!</regular-body>
+</post>
+<post id='139241528' url='http://jeffkreeftmeijer.tumblr.com/post/139241528' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/139241528/experience-with-using-cucumber-and-selenium' type='regular' date-gmt='2009-07-10 21:02:56 GMT' date='Fri, 10 Jul 2009 17:02:56' unix-timestamp='1247259776' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2573630826' from-feed-id='508097'>
+<regular-body>experience with using #cucumber and #selenium together? Anyone? #ror</regular-body>
+</post>
+<post id='139241541' url='http://jeffkreeftmeijer.tumblr.com/post/139241541' url-with-slug='http://jeffkreeftmeijer.tumblr.com/post/139241541/awesome-volley-by-shauninman-ghubaceks-last' type='regular' date-gmt='2009-07-10 21:02:56 GMT' date='Fri, 10 Jul 2009 17:02:56' unix-timestamp='1247259776' format='html' feed-item='http://twitter.com/jkreeftmeijer/statuses/2573104144' from-feed-id='508097'>
+<regular-body>awesome volley by @&lt;a href="http://twitter.com/shauninman">shauninman&lt;/a>. @&lt;a href="http://twitter.com/ghubacek">ghubacek&lt;/a>&amp;#8217;s last one was great too by the way&amp;#8230; #lyt</regular-body>
+</post>
+</posts>
+</tumblr>
Oops, something went wrong.

0 comments on commit 1fd434e

Please sign in to comment.