Skip to content

Commit ea4860c

Browse files
author
Cameron Cox
committed
first commit
0 parents  commit ea4860c

File tree

11 files changed

+379
-0
lines changed

11 files changed

+379
-0
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2010 Cameron Cox
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.

lib/tumbling.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'httparty'
2+
3+
module Tumbling
4+
5+
autoload :Resource, 'tumbling/resource'
6+
autoload :Post, 'tumbling/post'
7+
# quote, photo, link, chat, video, or audio
8+
9+
autoload :Audio, 'tumbling/audio'
10+
autoload :Conversation, 'tumbling/conversation'
11+
autoload :Link, 'tumbling/link'
12+
autoload :Photo, 'tumbling/photo'
13+
autoload :Quote, 'tumbling/quote'
14+
autoload :Video, 'tumbling/video'
15+
16+
class << self
17+
attr_accessor :email, :password
18+
19+
def blog=(blog)
20+
Resource.base_uri "http://#{blog}.tumblr.com"
21+
end
22+
end
23+
end
24+
25+
Tumbling.blog = "proposable"

lib/tumbling/audio.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Tumbling
2+
class Audio < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags
13+
14+
private
15+
16+
def self.from_api_attributes(attributes)
17+
{
18+
:id => attributes['id'],
19+
:url => attributes['url_with_slug'],
20+
:date => attributes['date_gmt'],
21+
:format => attributes['format'],
22+
:slug => attributes['slug'],
23+
:body => attributes['audio_player'],
24+
:title => attributes['audio_caption'],
25+
:tags => attributes['tag']
26+
}
27+
end
28+
29+
#
30+
# @return [String] get all posts with a type of regular
31+
def self.type
32+
'audio'
33+
end
34+
end
35+
end

lib/tumbling/conversation.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Tumbling
2+
class Conversation < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags
13+
14+
private
15+
16+
def self.from_api_attributes(attributes)
17+
{
18+
:id => attributes['id'],
19+
:url => attributes['url_with_slug'],
20+
:date => attributes['date_gmt'],
21+
:format => attributes['format'],
22+
:slug => attributes['slug'],
23+
:body => attributes['conversation_text'],
24+
:title => attributes['conversation_title'],
25+
:tags => attributes['tag']
26+
}
27+
end
28+
29+
#
30+
# @return [String] get all posts with a type of regular
31+
def self.type
32+
'conversation'
33+
end
34+
end
35+
end

lib/tumbling/link.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Tumbling
2+
class Link < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags
13+
14+
private
15+
16+
def self.from_api_attributes(attributes)
17+
{
18+
:id => attributes['id'],
19+
:url => attributes['url_with_slug'],
20+
:date => attributes['date_gmt'],
21+
:format => attributes['format'],
22+
:slug => attributes['slug'],
23+
:body => attributes['link_url'],
24+
:title => attributes['link_text'],
25+
:tags => attributes['tag']
26+
}
27+
end
28+
29+
#
30+
# @return [String] get all posts with a type of regular
31+
def self.type
32+
'link'
33+
end
34+
end
35+
end

lib/tumbling/photo.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Tumbling
2+
class Photo < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:images
13+
14+
private
15+
16+
def self.from_api_attributes(attributes)
17+
{
18+
:id => attributes['id'],
19+
:url => attributes['url_with_slug'],
20+
:date => attributes['date_gmt'],
21+
:format => attributes['format'],
22+
:slug => attributes['slug'],
23+
:body => build_body(attributes['photo_url']),
24+
:title => attributes['photo_caption'],
25+
:tags => attributes['tag']
26+
}
27+
end
28+
29+
def self.build_body(urls)
30+
31+
urls.collect { |x| Image.new(x).to_html }
32+
end
33+
34+
#
35+
# @return [String] get all posts with a type of regular
36+
def self.type
37+
'photo'
38+
end
39+
40+
class Image
41+
attr_accessor :url, :alt
42+
def initialize(url, alt = nil)
43+
@url = url
44+
@alt = alt
45+
end
46+
def to_html
47+
"<img src=\"#{url}\" alt=\"#{alt}\" />"
48+
end
49+
end
50+
end
51+
end

lib/tumbling/post.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Tumbling
2+
class Post < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags
13+
14+
private
15+
16+
def self.from_api_attributes(attributes)
17+
{
18+
:id => attributes['id'],
19+
:url => attributes['url_with_slug'],
20+
:date => attributes['date_gmt'],
21+
:format => attributes['format'],
22+
:slug => attributes['slug'],
23+
:body => attributes['regular_body'],
24+
:title => attributes['regular_title'],
25+
:tags => attributes['tag']
26+
}
27+
end
28+
29+
#
30+
# @return [String] get all posts with a type of regular
31+
def self.type
32+
'regular'
33+
end
34+
end
35+
end

lib/tumbling/quote.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Tumbling
2+
class Quote < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags,
13+
:tags
14+
15+
private
16+
17+
def self.from_api_attributes(attributes)
18+
{
19+
:id => attributes['id'],
20+
:url => attributes['url_with_slug'],
21+
:date => attributes['date_gmt'],
22+
:format => attributes['format'],
23+
:slug => attributes['slug'],
24+
:body => attributes['quote_text'],
25+
:title => attributes['quote_source'],
26+
:tags => attributes['tag']
27+
}
28+
end
29+
30+
#
31+
# @return [String] get all posts with a type of regular
32+
def self.type
33+
'quote'
34+
end
35+
end
36+
end

lib/tumbling/resource.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module Tumbling
2+
class Resource
3+
include HTTParty
4+
format :xml
5+
6+
def self.all
7+
get_resources(type)
8+
end
9+
10+
def type
11+
self.class.type
12+
end
13+
14+
def self.build(attrs)
15+
new from_api_attributes(attrs)
16+
end
17+
18+
private
19+
20+
def initialize(hash = {})
21+
hash.each do |key, value|
22+
send("#{key}=", value)
23+
end
24+
end
25+
26+
def self.export_type_to_native_objects(results)
27+
if results.kind_of?(Array)
28+
@_results = []
29+
results.each do |result|
30+
@_results.push(export_type_to_native_object(result))
31+
end
32+
33+
return @_results
34+
else
35+
return [export_type_to_native_object(results)]
36+
end
37+
end
38+
39+
def self.export_type_to_native_object(result)
40+
type = (result['type'] == 'regular') ? 'post' : result['type']
41+
object = Tumbling.const_get constantize_type(type)
42+
object.build(result)
43+
end
44+
45+
def self.get_resources(type)
46+
query = {}
47+
query.merge!({ :type => type }) unless type.nil?
48+
49+
results = get '/api/read', :query => query
50+
# return results
51+
if results['tumblr']
52+
return export_type_to_native_objects(results['tumblr']['posts']['post'])
53+
else
54+
[]
55+
end
56+
end
57+
58+
# The type of posts to get from the API
59+
#
60+
# @return [NilClass] nil for all post types
61+
def self.type
62+
nil
63+
end
64+
65+
def self.constantize_type(type)
66+
type[0...1].upcase + type[1...type.length]
67+
end
68+
end
69+
end

lib/tumbling/video.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Tumbling
2+
class Video < Resource
3+
4+
attr_accessor \
5+
:id,
6+
:url,
7+
:date,
8+
:format,
9+
:slug,
10+
:body,
11+
:title,
12+
:tags,
13+
:tags
14+
15+
private
16+
17+
def self.from_api_attributes(attributes)
18+
{
19+
:id => attributes['id'],
20+
:url => attributes['url_with_slug'],
21+
:date => attributes['date_gmt'],
22+
:format => attributes['format'],
23+
:slug => attributes['slug'],
24+
:body => attributes['video_player'],
25+
:title => attributes['video_caption'],
26+
:tags => attributes['tag']
27+
}
28+
end
29+
30+
#
31+
# @return [String] get all posts with a type of regular
32+
def self.type
33+
'video'
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)