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 =
"0.1.0"
GENERATOR =
"The Tumblr Gem v#{VERSION}"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*credentials) ⇒ Tumblr

Returns a new instance of Tumblr.



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

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

Class Method Details

.infer_post_type(doc) ⇒ Object

Guess the Type of Post for a given documents



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tumblr.rb', line 50

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tumblr.rb', line 64

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tumblr.rb', line 37

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

#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
33
34
# File 'lib/tumblr.rb', line 23

def post(doc)
  raise 'Requires an e-mail address and password' unless @credentials
  tumblr_post = if doc.is_a?(Tumblr::Post)
    doc.to_h
  elsif doc.respond_to?(:keys)
    doc
  else
    Tumblr.parse(doc).to_h
  end
  writer = Writer.new(@credentials[:email],@credentials[:password])
  tumblr_post.has_key?(:'post-id') ? writer.edit(tumblr_post) : writer.write(tumblr_post)
end

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

Convenience method for Reader#read



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

def read(username, parameters={})
  reader = !@credentials.blank? ? Reader.new(@credentials[:email],@credentials[:password]) : Reader.new
  reader.read(username, parameters)
end