Sha256: 3e2880b26c188ca0563e9d9244c2b5bd69086089896f089a6f20f7874399dcb2

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module Tumblr
  class Post
      
    def self.find(*args)
      options = args.extract_options!
      
      if((user = args.second).is_a?(Tumblr::User))        
        options = options.merge(
          :email =>     user.email,
          :password =>  user.password
        )        
      end
          
      case args.first
        when :first then find_initial(options)
        when :last  then find_last(options)
        when :all   then find_every(options)
        else             find_from_id(args.first, options)
      end
    end
  
    def self.find_initial(options)
      Tumblr::Request.read(options.merge(:start => 0, :num => 1))
    end
  
    def self.find_last(options)
      total = all['tumblr']['posts']['total'].to_i
      Tumblr::Request.read(options.merge(:start => total - 1, :num => 1))
    end
    
    def self.find_every(options)
      Tumblr::Request.read(options.merge(:num => 50))
    end
  
    def self.find_from_id(id, options)
      Tumblr::Request.read(options.merge(:id => id))
    end
  
    def self.all(*args)
      self.find(:all, *args)
    end
    
    def self.first(*args)
      self.find(:first, *args)
    end    
    
    def self.last(*args)
      self.find(:last, *args)
    end
    
    def self.create(*args)
      options = process_options(*args)
      Tumblr::Request.write(options)
    end
    
    def self.update(*args)
      options = process_options(*args)
      Tumblr::Request.write(options)
    end
    
    def self.destroy(*args)
      options = process_options(*args)
      Tumblr::Request.delete(options)
    end
    
    def self.process_options(*args)
      options = args.extract_options!

      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jeffkreeftmeijer-tumblr-0.0.2 lib/tumblr/post.rb