Sha256: 2aecc6d4bfe43471b553658f4eab27afe288bca013a6d21c5a356a4b02b8c9cb

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

class Tumblr
  class Request
    
    # a GET request to http://[YOURUSERNAME].tumblr.com/api/read
    def self.read(options = {})
        response = HTTParty.get("http://#{Tumblr::blog}/api/read", options)
      return response unless raise_errors(response)
    end
    
    # a POST request to http://www.tumblr.com/api/write
    def self.write(options = {})
      response = HTTParty.post('http://www.tumblr.com/api/write', :body => options)
      return(response) unless raise_errors(response)
    end
    
    # a POST request to http://www.tumblr.com/api/delete
    def self.delete(options = {})
      response = HTTParty.post('http://www.tumblr.com/api/delete', :body => options)
      return(response) unless raise_errors(response)
    end
    
    # a POST request to http://www.tumblr.com/api/authenticate
    def self.authenticate(email, password)
      HTTParty.post('http://www.tumblr.com/api/authenticate', :body => {:email => email, :password => password})
    end
    
    # raise tumblr response errors.
    def self.raise_errors(response)
      if(response.is_a?(Hash))
        message = "#{response[:code]}: #{response[:body]}"
        code = response[:code].to_i
      else
        message = "#{response.code}: #{response.body}"
        code = response.code.to_i
      end
      
      case code
        when 403
          raise(Forbidden.new, message)
        when 400
          raise(BadRequest.new, message)
        when 404
          raise(NotFound.new, message)
        when 201
          return false
      end        
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matenia-tumblr-api-0.1.6 lib/tumblr/request.rb