Module: Tumblr::Request

Included in:
Client
Defined in:
lib/tumblr/request.rb

Instance Method Summary collapse

Instance Method Details

#get(path, params = {}) ⇒ Object

Performs a get request



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

def get(path, params={})
  respond get_response(path, params)
end

#get_redirect_url(path, params = {}) ⇒ Object

get a redirect url



15
16
17
18
19
20
21
22
# File 'lib/tumblr/request.rb', line 15

def get_redirect_url(path, params = {})
  response = get_response path, params
  if response.status == 301
    response.headers['Location']
  else
    response.body['meta']
  end
end

#get_response(path, params = {}) ⇒ Object

Perform a get request and return the raw response



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

def get_response(path, params = {})
  connection.get do |req|
    req.url path
    req.params = params
  end
end

#post(path, params = {}) ⇒ Object

Performs post request



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

def post(path, params={})
  if Array === params[:tags]
    params[:tags] = params[:tags].join(',')
  end
  response = connection.post do |req|
    req.url path
    req.body = params unless params.empty?
  end
  #Check for errors and encapsulate
  respond(response)
end

#respond(response) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/tumblr/request.rb', line 42

def respond(response)
  if [201, 200].include?(response.status)
    response.body['response']
  else
    # surface the meta alongside response
    res = response.body['meta'] || {}
    res.merge! response.body['response'] if response.body['response'].is_a?(Hash)
    res
  end
end