tumblrb 1.0.3 → 1.0.4

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/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "nokogiri", "~> 1.4.4"
4
+ gem "curb", "~> 0.7.15"
5
+
6
+ gemspec
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Jesse Reiss
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ Tumblr API Wrapper
2
+ ====================
3
+
4
+ A simple, easy to use ruby wrapper for the Tumblr API.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Run
10
+
11
+ gem install tumblrb
12
+
13
+ Or add
14
+
15
+ gem 'tumblrb', :require => "tumblr"
16
+
17
+ to your Gemfile
18
+
19
+ Setup
20
+ ------------
21
+
22
+ Add
23
+
24
+ Tumblr.user = Tumblr::User.new 'email@email.com', 'password'
25
+ Tumblr.blog = 'myblogname'
26
+
27
+ into your initialization
28
+
29
+ Fetching
30
+ ------------
31
+
32
+ Just like `will_paginate` :
33
+
34
+ @posts = Tumblr::Item.paginate(:page => 1, :per_page => 20)
35
+ @posts.next_page => 1
36
+ @posts.previous_page => nil
37
+ @posts.each do |post|
38
+ =render :partial => "post", :object => post
39
+ end
40
+
41
+ In addition to any paging parameters, `paginate` also accepts all the documented params from the [Tumblr API](http://www.tumblr.com/docs/en/api).
42
+
43
+ You can also fetch the last post with
44
+
45
+ Tumblr::Item.first
46
+
47
+ Item Classes
48
+ ------------
49
+
50
+ Every Tumblr entry type is a separate class so you can do stuff like :
51
+
52
+ case post
53
+ when Tumblr::Regular
54
+ # Display a regular post
55
+ when Tumblr::Quote
56
+ # Display a quote
57
+ when Tumblr::Photo
58
+ # Display a photo
59
+ ...
60
+ end
61
+
62
+ Anything else?
63
+ ------------
64
+
65
+ Questions, requests, concerns? Find me at [the gorgon lab](http://www.thegorgonlab.com)
@@ -0,0 +1,26 @@
1
+ require 'nokogiri'
2
+ require 'curl'
3
+ require 'tumblr/user'
4
+ require 'tumblr/page'
5
+ require 'tumblr/item'
6
+ require 'tumblr/answer'
7
+ require 'tumblr/audio'
8
+ require 'tumblr/conversation'
9
+ require 'tumblr/item'
10
+ require 'tumblr/link'
11
+ require 'tumblr/photo'
12
+ require 'tumblr/quote'
13
+ require 'tumblr/regular'
14
+ require 'tumblr/video'
15
+
16
+ module Tumblr
17
+ mattr_accessor :user
18
+
19
+ def self.blog=(blog)
20
+ @blog = (blog =~ /\./) ? blog : "#{blog}.tumblr.com"
21
+ end
22
+
23
+ def self.blog
24
+ @blog
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ class Answer < Item
3
+ attr_accessor :question, :answer
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :question => question,
8
+ :answer => answer
9
+ })
10
+ end
11
+
12
+ private
13
+
14
+ def parse_xml_node(node)
15
+ super(node)
16
+ self.caption = node.css('question').first.try(:content)
17
+ self.source = node.css('answer').first.try(:content)
18
+ self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ class Audio < Item
3
+ attr_accessor :caption, :player
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :caption => caption,
8
+ :player => player
9
+ })
10
+ end
11
+
12
+ private
13
+
14
+ def parse_xml_node(node)
15
+ super(node)
16
+ self.caption = node.css('audio-caption').first.try(:content)
17
+ self.source = node.css('audio-player').first.try(:content)
18
+ self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Tumblr
2
+ class Conversation < Item
3
+ attr_accessor :title, :text, :lines
4
+
5
+ class Line < Struct.new(:name, :label, :content)
6
+ def as_json
7
+ {
8
+ :name => name,
9
+ :label => label,
10
+ :content => content
11
+ }
12
+ end
13
+ end
14
+
15
+ def as_json
16
+ super.merge!({
17
+ :title => title,
18
+ :text => text,
19
+ :lines => lines.map { |l| l.as_json }
20
+ })
21
+ end
22
+
23
+ private
24
+
25
+ def self.parse_xml_node(node)
26
+ super(node)
27
+ self.title = node.css('conversation-title').first.try(:content)
28
+ self.text = node.css('conversation-text').first.try(:content)
29
+ self.lines = node.css("convesation line").collect do |line|
30
+ Line.new(line["name"], line["label"], label.content)
31
+ end
32
+ self
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,103 @@
1
+ module Tumblr
2
+ class Item
3
+ PAGE_SIZE = 20
4
+ attr_accessor :id, :slug, :type, :date, :format, :xml
5
+ attr_accessor :tags
6
+
7
+ def self.paginate(params={})
8
+ params[:num] = params[:per_page].to_i > 0 ? params[:per_page].to_i : PAGE_SIZE
9
+ params[:start] = ([params[:page].to_i, 1].max - 1) * params[:num]
10
+ params.delete(:limit)
11
+ params.delete(:page)
12
+ if Tumblr.user
13
+ params[:email] = Tumblr.user.email
14
+ params[:password] = Tumblr.user.password
15
+ end
16
+ page_xml = xml(params)
17
+ items = parse(page_xml)
18
+ page_meta = page_xml.at("posts")
19
+ if page_meta
20
+ Tumblr::Page.new(items, page_meta["start"], page_meta["total"], params[:num])
21
+ else
22
+ Tumblr::Page.new(items, 0, items.length, params[:num])
23
+ end
24
+ end
25
+
26
+ def self.find(id)
27
+ paginate(:page => 1, :per_page => 1).first
28
+ end
29
+
30
+ def self.all(params={})
31
+ parse(xml(params))
32
+ end
33
+
34
+ def self.xml(params={})
35
+ Nokogiri::XML(raw(params))
36
+ end
37
+
38
+ def self.raw(params={})
39
+ Rails.logger.info "tumblr : fetching url #{url(params)}"
40
+ curl = Curl::Easy.perform(url(params))
41
+ curl.body_str
42
+ end
43
+
44
+ def self.find(id)
45
+ parse(xml(:id => id.to_i)).first
46
+ end
47
+
48
+ def to_param
49
+ "#{id}-#{slug}"
50
+ end
51
+
52
+ def date_string
53
+ date.strftime("%B %d, %Y")
54
+ end
55
+
56
+ def dom_class
57
+ self.class.to_s.split("::").last.underscore
58
+ end
59
+
60
+ def as_json
61
+ {
62
+ :id => id,
63
+ :slug => slug,
64
+ :date => date,
65
+ :type => type,
66
+ :format => format,
67
+ :tags => tags
68
+ }
69
+ end
70
+
71
+ private
72
+
73
+ def self.url(params)
74
+ url = "http://#{Tumblr.blog}/api/read"
75
+ url += "?#{params.to_query}" if params.keys.length > 0
76
+ url
77
+ end
78
+
79
+ def self.parse(xml)
80
+ xml.css("posts post").collect do |post|
81
+ object_class = "Tumblr::#{post['type'].classify}".constantize rescue nil
82
+ if object_class
83
+ object = object_class.new
84
+ object.send(:parse_xml_node, post)
85
+ object
86
+ end
87
+ end
88
+ end
89
+
90
+ def parse_xml_node(node)
91
+ self.xml = node.to_s
92
+ self.id = node['id']
93
+ self.type = node['type']
94
+ self.slug = node['slug']
95
+ self.date = Time.at(node['unix-timestamp'].to_i)
96
+ self.format = node['format']
97
+ self.tags = node.css("tag").collect do |tag|
98
+ tag.content
99
+ end
100
+ self
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ class Link < Item
3
+ attr_accessor :text, :url
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :text => text,
8
+ :url => url
9
+ })
10
+ end
11
+
12
+ private
13
+
14
+ def parse_xml_node(node)
15
+ super(node)
16
+ self.text = node.css('link-text').first.try(:content)
17
+ self.source = node.css('link-url').first.try(:content)
18
+ self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Tumblr
2
+ class Page
3
+ include Enumerable
4
+ attr_accessor :items, :start, :total, :page_size
5
+
6
+ def initialize(items, start, total, page_size)
7
+ self.items = items
8
+ self.start = start.to_i
9
+ self.total = total.to_i
10
+ self.page_size = page_size.to_i
11
+ end
12
+
13
+ def page
14
+ (start / page_size).floor + 1
15
+ end
16
+
17
+ def next_page
18
+ (total - start) > page_size ? page + 1 : nil
19
+ end
20
+
21
+ def previous_page
22
+ page > 1 ? page - 1 : nil
23
+ end
24
+
25
+ [:size, :count, :length].each do |m|
26
+ define_method m do
27
+ @items.send(m)
28
+ end
29
+ end
30
+
31
+ def each(&block)
32
+ items.each(&block)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ class Quote < Item
3
+ attr_accessor :text, :source
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :text => text,
8
+ :source => source
9
+ })
10
+ end
11
+
12
+ private
13
+
14
+ def parse_xml_node(node)
15
+ super(node)
16
+ self.text = node.css('quote-text').first.try(:content)
17
+ self.source = node.css('quote-source').first.try(:content)
18
+ self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Tumblr
2
+ class Regular < Item
3
+ attr_accessor :title, :body
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :title => title,
8
+ :body => body
9
+ })
10
+ end
11
+
12
+ private
13
+
14
+ def parse_xml_node(node)
15
+ super(node)
16
+ self.title = node.css('regular-title').first.try(:content)
17
+ self.body = node.css('regular-body').first.try(:content)
18
+ self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
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)
7
+ self.email = email
8
+ self.password = password
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Tumblr
2
+ # The version of the gem
3
+ VERSION = '1.0.4'.freeze unless defined?(::Twitter::VERSION)
4
+ end
@@ -0,0 +1,23 @@
1
+ module Tumblr
2
+ class Video < Item
3
+ attr_accessor :caption, :source, :player
4
+
5
+ def as_json
6
+ super.merge!({
7
+ :caption => caption,
8
+ :source => source,
9
+ :player => player
10
+ })
11
+ end
12
+
13
+ private
14
+
15
+ def parse_xml_node(node)
16
+ super(node)
17
+ self.caption = node.css('video-title').first.try(:content)
18
+ self.source = node.css('video-text').first.try(:content)
19
+ self.player = node.css('video-player').first.try(:content)
20
+ self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tumblr/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.add_dependency('nokogiri', '~> 1.4.4')
6
+ s.add_dependency('curb', '~> 0.7.15')
7
+ s.authors = ["Jesse Reiss"]
8
+ s.description = %q{A Ruby wrapper for the Tumblr XML API}
9
+ s.email = ['jessereiss@gmail.com']
10
+ s.homepage = 'https://github.com/thegorgon/tumblrb'
11
+ s.name = 'tumblrb'
12
+ s.platform = Gem::Platform::RUBY
13
+ s.require_paths << 'lib'
14
+ s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version=
15
+ s.rubyforge_project = s.name
16
+ s.summary = %q{Ruby wrapper for the Tumblr API}
17
+ s.files = [
18
+ "README.mkd",
19
+ "LICENSE.mkd",
20
+ "Gemfile",
21
+ "tumblrb.gemspec",
22
+ "lib/tumblr.rb",
23
+ "lib/tumblr/answer.rb",
24
+ "lib/tumblr/audio.rb",
25
+ "lib/tumblr/conversation.rb",
26
+ "lib/tumblr/item.rb",
27
+ "lib/tumblr/link.rb",
28
+ "lib/tumblr/page.rb",
29
+ "lib/tumblr/quote.rb",
30
+ "lib/tumblr/regular.rb",
31
+ "lib/tumblr/user.rb",
32
+ "lib/tumblr/version.rb",
33
+ "lib/tumblr/video.rb"
34
+ ]
35
+ s.version = Tumblr::VERSION.dup
36
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tumblrb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
4
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 3
10
- version: 1.0.3
5
+ version: 1.0.4
11
6
  platform: ruby
12
7
  authors:
13
8
  - Jesse Reiss
@@ -15,7 +10,8 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-05-16 00:00:00 Z
13
+ date: 2011-05-16 00:00:00 -07:00
14
+ default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
17
  name: nokogiri
@@ -25,11 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ~>
27
23
  - !ruby/object:Gem::Version
28
- hash: 15
29
- segments:
30
- - 1
31
- - 4
32
- - 4
33
24
  version: 1.4.4
34
25
  type: :runtime
35
26
  version_requirements: *id001
@@ -41,11 +32,6 @@ dependencies:
41
32
  requirements:
42
33
  - - ~>
43
34
  - !ruby/object:Gem::Version
44
- hash: 29
45
- segments:
46
- - 0
47
- - 7
48
- - 15
49
35
  version: 0.7.15
50
36
  type: :runtime
51
37
  version_requirements: *id002
@@ -58,8 +44,24 @@ extensions: []
58
44
 
59
45
  extra_rdoc_files: []
60
46
 
61
- files: []
62
-
47
+ files:
48
+ - README.mkd
49
+ - LICENSE.mkd
50
+ - Gemfile
51
+ - tumblrb.gemspec
52
+ - lib/tumblr.rb
53
+ - lib/tumblr/answer.rb
54
+ - lib/tumblr/audio.rb
55
+ - lib/tumblr/conversation.rb
56
+ - lib/tumblr/item.rb
57
+ - lib/tumblr/link.rb
58
+ - lib/tumblr/page.rb
59
+ - lib/tumblr/quote.rb
60
+ - lib/tumblr/regular.rb
61
+ - lib/tumblr/user.rb
62
+ - lib/tumblr/version.rb
63
+ - lib/tumblr/video.rb
64
+ has_rdoc: true
63
65
  homepage: https://github.com/thegorgon/tumblrb
64
66
  licenses: []
65
67
 
@@ -74,25 +76,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
76
  requirements:
75
77
  - - ">="
76
78
  - !ruby/object:Gem::Version
77
- hash: 3
78
- segments:
79
- - 0
80
79
  version: "0"
81
80
  required_rubygems_version: !ruby/object:Gem::Requirement
82
81
  none: false
83
82
  requirements:
84
83
  - - ">="
85
84
  - !ruby/object:Gem::Version
86
- hash: 23
87
- segments:
88
- - 1
89
- - 3
90
- - 6
91
85
  version: 1.3.6
92
86
  requirements: []
93
87
 
94
88
  rubyforge_project: tumblrb
95
- rubygems_version: 1.8.2
89
+ rubygems_version: 1.6.2
96
90
  signing_key:
97
91
  specification_version: 3
98
92
  summary: Ruby wrapper for the Tumblr API