Class: Tumblr::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr/post.rb

Class Method Summary collapse

Class Method Details

.all(options = {}) ⇒ Object

alias of find(:all)



74
75
76
# File 'lib/tumblr/post.rb', line 74

def self.all(options = {})
  self.find(:all, options)
end

.count(options = {}) ⇒ Object

count the posts



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tumblr/post.rb', line 17

def self.count(options = {})
  
  #puts balh = {:num => 1}.merge(options).to_yaml      
  response = Tumblr::Request.read({:num => 1}.merge(options))
  if(options.empty?)
    #puts response['tumblr']['posts'].to_yaml
    #puts "*****"
  end
  response['tumblr']['posts']['total'].to_i
  
end

.create(*args) ⇒ Object

create a new post



89
90
91
92
# File 'lib/tumblr/post.rb', line 89

def self.create(*args)
  options = process_options(*args)
  Tumblr::Request.write(options)
end

.destroy(*args) ⇒ Object

destroy a post



101
102
103
104
# File 'lib/tumblr/post.rb', line 101

def self.destroy(*args)
  options = process_options(*args)
  Tumblr::Request.delete(options)
end

.find(*args) ⇒ Object

works just like ActiveRecord’s find. (:all, :first, :last or id)



5
6
7
8
9
10
11
12
13
14
# File 'lib/tumblr/post.rb', line 5

def self.find(*args)
  extra_options = args.last.is_a?(Hash) ?  args.pop : {} 
    
  case args.first
    when :all then return self.find_every(extra_options)
    when :first then return self.find_initial(extra_options)
    when :last then return self.find_last(extra_options)
    else return self.find_from_id(args.first)
  end      
end

.find_every(options) ⇒ Object

find all posts



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tumblr/post.rb', line 46

def self.find_every(options)
  amount = (Tumblr::Post.count(options).to_f / 50).ceil
  options = {:num => 50}.merge(options)

  responses = []
  amount.times do |count|
    responses << Tumblr::Request.read(options.merge({:start => (count.to_i * 50)}))
    #puts options.merge({:start => (count.to_i * 50)}).to_yaml
  end
      
  response = {'tumblr' => {'posts' => {'post' => []}}}
  responses.each do |r|
    r['tumblr']['posts']['post'].each { | p | response['tumblr']['posts']['post'] << p }
  end
  
  #puts response['tumblr']['posts']['post'].length.to_yaml

  return [response['tumblr']['posts']['post']] unless(response['tumblr']['posts']['post'].is_a?(Array))  
  response['tumblr']['posts']['post']
end

.find_from_id(id) ⇒ Object

find a post by id



68
69
70
71
# File 'lib/tumblr/post.rb', line 68

def self.find_from_id(id)
  response = Tumblr::Request.read(:id => id)
  response['tumblr']['posts']['post']
end

.find_initial(options) ⇒ Object

find the first post



30
31
32
33
34
35
36
37
# File 'lib/tumblr/post.rb', line 30

def self.find_initial(options)
  total = self.count
  options = {:start => (total - 1),  :num => 1} if(options.empty?)      
  response = Tumblr::Request.read(options)

  return response['tumblr']['posts']['post'].first unless(options == {:start => (total - 1),  :num => 1})
  response['tumblr']['posts']['post']
end

.find_last(options) ⇒ Object

find the last post



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

def self.find_last(options)
  response = Tumblr::Request.read({:num => 1}.merge(options))
  response['tumblr']['posts']['post']
end

.first(options = {}) ⇒ Object

alias of find(:first)



79
80
81
# File 'lib/tumblr/post.rb', line 79

def self.first(options = {})
  self.find(:first, options)
end

.last(options = {}) ⇒ Object

alias of find(:last)



84
85
86
# File 'lib/tumblr/post.rb', line 84

def self.last(options = {})
  self.find(:last, options)
end

.process_options(*args) ⇒ Object

extracts options from the arguments, converts a user object to :email and :password params and fixes the :post_id/‘post-id’ issue.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tumblr/post.rb', line 107

def self.process_options(*args)
  options = args.last.is_a?(Hash) ?  args.pop : {}

  if((user = args.first).is_a?(Tumblr::User))        
    options = options.merge(
      :email =>     user.email,
      :password =>  user.password
    )
  end
        
  if(options[:post_id])
    options['post-id'] = options[:post_id]
    options[:post_id] = nil
  end
  
  return options
end

.update(*args) ⇒ Object

update a post



95
96
97
98
# File 'lib/tumblr/post.rb', line 95

def self.update(*args)
  options = process_options(*args)
  Tumblr::Request.write(options)
end