Class: Tumblr::Reader

Inherits:
Weary::Base
  • Object
show all
Defined in:
lib/tumblr/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*credentials) ⇒ Reader

Returns a new instance of Reader.



7
8
9
# File 'lib/tumblr/reader.rb', line 7

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

Class Method Details

.build_post(post) ⇒ Object

Build a Post object from Reader’s Post XML



54
55
56
57
58
59
60
61
62
# File 'lib/tumblr/reader.rb', line 54

def self.build_post(post)
  setup_post(post) do |tumblr_post|
    tumblr_post.date = post['date_gmt']
    tumblr_post.format = post['format'].to_sym if post['format']
    tumblr_post.slug = post['slug']
    tumblr_post.tags post['tag'] if post['tag']
    tumblr_post.reblog_key = post['reblog_key'] if post['reblog_key']
  end
end

.get_posts(response, type = nil) ⇒ Object

Get the Posts as Post objects from a Read response. Pass an additional type parameter to only get Posts of a certain type.



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

def self.get_posts(response, type = nil)
  tumblr_post = response['tumblr']['posts']['post']
  posts = tumblr_post.respond_to?(:each_pair) ? [tumblr_post] : tumblr_post
  posts.collect! { |post| build_post(post) }
  return posts.select {|post| post.is_a?(Tumblr.map(type)) } if type
  posts
end

.read(username, via = :get, params = {}) ⇒ Object

Helper method to facilitate standard GET Read and Authenticated Read



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

def self.read(username, via = :get, params = {})
  Weary.request("http://#{username}.tumblr.com/api/read/", via) do |req|
    req.with = params unless params.blank?
  end
end

Instance Method Details

#authenticated_read(username, params = {}) ⇒ Object



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

def authenticated_read(username, params={})
  raise 'You must provide an email address and password' unless (params.include?(:email) && params.include?(:password)) || defaults
  self.class.read username, :post, parameters(params)
end

#get_all_posts(username, start = 0, total = nil) ⇒ Object

Transform ALL of the posts for user/group to Post objects. This could take a while…



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tumblr/reader.rb', line 31

def get_all_posts(username, start = 0, total = nil)
  first_read = authenticated_read(username, {:num => 50,:start => start}).perform
  raise %Q(Tumblr response was not successful, "#{first_read.code}: #{first_read.message}") if !first_read.success?
  posts = self.class.get_posts(first_read)
  offset = start + posts.count
  post_total = total || first_read['tumblr']['posts']['total'].to_i
  if post_total > offset
    posts |= get_all_posts(username, offset, post_total)
  end
  posts
end

#parameters(params) ⇒ Object

Setup parameters for Reads



23
24
25
26
27
# File 'lib/tumblr/reader.rb', line 23

def parameters(params)
  allowed = [:start,:num,:type,:id,:filter,:tagged,:search,:state,:email,:password]
  params.merge! defaults if defaults
  params.reject {|key,value| !allowed.include? key }
end

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



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

def read(username, params={})
  self.class.read username, :get, parameters(params)
end