Class: Tumblr::Request

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

Class Method Summary collapse

Class Method Details

.authenticate(email, password) ⇒ Object



25
26
27
# File 'lib/tumblr/request.rb', line 25

def self.authenticate(email, password)
  HTTParty.post('http://www.tumblr.com/api/authenticate', :query => {:email => email, :password => password})
end

.delete(options = {}) ⇒ Object

a POST request to www.tumblr.com/api/delete



19
20
21
22
# File 'lib/tumblr/request.rb', line 19

def self.delete(options = {})
  response = HTTParty.post('http://www.tumblr.com/api/delete', :query => options)
  return(response) unless raise_errors(response)
end

.raise_errors(response) ⇒ Object

raise tumblr response errors.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tumblr/request.rb', line 30

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

.read(options = {}) ⇒ Object

a GET request to [YOURUSERNAME].tumblr.com/api/read override this behavior by setting Tumblr.blog_url, GET request will then direct to [YOURURL]/api/read



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

def self.read(options = {})
  response = HTTParty.get("http://#{Tumblr::blog_url ||= (Tumblr::blog ||= 'staff')+'.tumblr.com'}/api/read", options)
  return response unless raise_errors(response)
end

.write(options = {}) ⇒ Object

a POST request to www.tumblr.com/api/write



13
14
15
16
# File 'lib/tumblr/request.rb', line 13

def self.write(options = {})
  response = HTTParty.post('http://www.tumblr.com/api/write', :query => options)
  return(response) unless raise_errors(response)
end