Class: Tumblr

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr/post.rb,
lib/tumblr.rb,
lib/tumblr/reader.rb,
lib/tumblr/writer.rb,
lib/tumblr/post/link.rb,
lib/tumblr/post/audio.rb,
lib/tumblr/post/photo.rb,
lib/tumblr/post/quote.rb,
lib/tumblr/post/video.rb,
lib/tumblr/post/regular.rb,
lib/tumblr/authenticator.rb,
lib/tumblr/post/conversation.rb

Overview

TODO: Support file uploading

Defined Under Namespace

Classes: Authenticator, Post, Reader, Writer

Constant Summary collapse

VERSION =
"1.1.1"
GENERATOR =
"The Tumblr Gem v#{VERSION}"
USER_AGENT =
"TumblrGem/#{VERSION} (+http://github.com/mwunsch/tumblr)"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*credentials) ⇒ Tumblr

Returns a new instance of Tumblr.



13
14
15
# File 'lib/tumblr.rb', line 13

def initialize(*credentials)
  @credentials = {:email => credentials[0], :password => credentials[1]} unless credentials.blank?
end

Class Method Details

.execute(credentials, input) ⇒ Object



57
58
59
60
# File 'lib/tumblr.rb', line 57

def self.execute(credentials, input)
  request = new(credentials[:email],credentials[:password]).post(input)
  request.perform
end

.infer_post_type(doc) ⇒ Object

Guess the Type of Post for a given documents



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tumblr.rb', line 76

def self.infer_post_type(doc)
  begin
    url = URI.parse(doc)
    if url.is_a?(URI::HTTP)
      (url.host.include?('youtube.com') || url.host.include?('vimeo.com')) ? :video : :link
    else
      :regular
    end
  rescue URI::InvalidURIError
    :regular
  end
end

.map(key) ⇒ Object

Map a post type key to its class



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tumblr.rb', line 90

def self.map(key)
  case key
    when :regular
      Post::Regular
    when :photo
      Post::Photo
    when :quote
      Post::Quote
    when :link
      Post::Link
    when :conversation
      Post::Conversation
    when :video
      Post::Video
    when :audio
      Post::Audio
    else
      raise "#{key} is not an understood Tumblr post type"
  end
end

.parse(doc) ⇒ Object

Parse a post out of a string



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tumblr.rb', line 63

def self.parse(doc)
  document = {}
  if doc =~ /^(\s*---(.*)---\s*)/m
    document[:data] = YAML.load(Regexp.last_match[2].strip)
    document[:body] = doc.sub(Regexp.last_match[1],'').strip
  else
    document[:data] = {'type' => infer_post_type(doc)}
    document[:body] = doc
  end
  create_post document
end

Instance Method Details

#authenticateObject



39
40
41
42
# File 'lib/tumblr.rb', line 39

def authenticate
  raise 'Requires an e-mail address and password' unless @credentials
  Authenticator.new(@credentials[:email],@credentials[:password]).authenticate
end

#dashboard(parameters = {}) ⇒ Object



34
35
36
37
# File 'lib/tumblr.rb', line 34

def dashboard(parameters={})
  raise 'Requires an e-mail address and password' unless @credentials
  reader.dashboard(parameters)
end

#post(doc) ⇒ Object

Post a document to Tumblr. If the document has a post-id, it will be edited.



23
24
25
26
27
28
29
30
31
32
# File 'lib/tumblr.rb', line 23

def post(doc)
  tumblr_post = if doc.is_a?(Tumblr::Post)
    doc.to_h
  elsif doc.respond_to?(:keys)
    doc
  else
    Tumblr.parse(doc).to_h
  end
  tumblr_post.has_key?(:'post-id') ? writer.edit(tumblr_post) : writer.write(tumblr_post)
end

#read(username, parameters = {}) ⇒ Object

Convenience method for Reader#read



18
19
20
# File 'lib/tumblr.rb', line 18

def read(username, parameters={})
  reader.read(username, parameters)
end

#readerObject



44
45
46
47
48
49
50
# File 'lib/tumblr.rb', line 44

def reader
  if @credentials.blank?
    Reader.new
  else
    Reader.new(@credentials[:email],@credentials[:password])
  end
end

#writerObject



52
53
54
55
# File 'lib/tumblr.rb', line 52

def writer
  raise 'Requires an e-mail address and password' unless @credentials
  Writer.new(@credentials[:email],@credentials[:password])
end