Module: Tweetlr::Processors::Tumblr

Includes:
LogAware
Defined in:
lib/tweetlr/processors/tumblr.rb

Overview

utilities for handling tumblr

Constant Summary collapse

GENERATOR =
%{tweetlr - http://tweetlr.5v3n.com}
API_ENDPOINT_TUMBLR =
'http://www.tumblr.com'

Class Method Summary collapse

Methods included from LogAware

log=

Class Method Details

.logObject



10
11
12
# File 'lib/tweetlr/processors/tumblr.rb', line 10

def self.log
  Tweetlr::LogAware.log #TODO why doesn't the include make the log method accessible?
end

.post(options = {}) ⇒ Object

post a tumblr photo entry.

required arguments are :tumblr_blog_hostname, :tumblr_blog_hostname, :tumblr_oauth_api_secret, :tumblr_oauth_access_token_secret, :source, :caption, :state

optional arguments: :tags, :type (default: ‘photo’)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tweetlr/processors/tumblr.rb', line 19

def self.post(options={})
  log.info "posting to #{options['tumblr_blog_hostname'] || options['group']}..."
  base_hostname       = options['tumblr_blog_hostname'] || options['group']
  tumblr_oauth_api_key= options['tumblr_oauth_api_key'] 
  tumblr_oauth_api_secret= options['tumblr_oauth_api_secret'] 
  access_token_key    = options['tumblr_oauth_access_token_key']
  access_token_secret = options['tumblr_oauth_access_token_secret']
  type                = options['type'] || 'photo'
  tags                = options['tags'] || ''
  post_response = nil

  if base_hostname && access_token_key && access_token_secret
    begin
      consumer = OAuth::Consumer.new(tumblr_oauth_api_key, tumblr_oauth_api_secret,
                                     { :site => 'http://www.tumblr.com',
                                       :request_token_path => '/oauth/request_token',
                                       :authorize_path => '/oauth/authorize',
                                       :access_token_path => '/oauth/access_token',
                                       :http_method => :post } )

      access_token = OAuth::AccessToken.new(consumer, access_token_key, access_token_secret)

      post_response = access_token.post(
        "http://api.tumblr.com/v2/blog/#{base_hostname}/post", { 
          :type => type, 
          :source => options[:source], 
          :caption => options[:caption],
          :date => options[:date],
          :tags => tags,
          :state => options[:state],
          :generator => GENERATOR
           }
          )
    rescue Exception => e
      log.error "Error posting to tumblr: #{e}"
      raise e
    end
    post_response
  end
end