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.2.0"
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



66
67
68
69
# File 'lib/tumblr.rb', line 66

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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tumblr.rb', line 85

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tumblr.rb', line 99

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



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tumblr.rb', line 72

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

#all_pages(username) ⇒ Object



49
50
51
# File 'lib/tumblr.rb', line 49

def all_pages(username)
  reader.all_pages(username)
end

#authenticate(theme = false) ⇒ Object



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

def authenticate(theme = false)
  raise 'Requires an e-mail address and password' unless @credentials
  params = theme ? {:'include-theme' => 1} : {}
  Authenticator.new(@credentials[:email],@credentials[:password]).authenticate(params)
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

#pages(username) ⇒ Object



45
46
47
# File 'lib/tumblr.rb', line 45

def pages(username)
  reader.pages(username)
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



53
54
55
56
57
58
59
# File 'lib/tumblr.rb', line 53

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

#writerObject



61
62
63
64
# File 'lib/tumblr.rb', line 61

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