Module: Dropbox::API::Connection::Requests

Included in:
Dropbox::API::Connection
Defined in:
lib/dropbox-api/connection/requests.rb

Instance Method Summary collapse

Instance Method Details

#get(endpoint, path, data = {}, headers = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/dropbox-api/connection/requests.rb', line 43

def get(endpoint, path, data = {}, headers = {})
  query = Dropbox::API::Util.query(data)
  request do
    token(endpoint).get "#{Dropbox::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
  end
end

#get_raw(endpoint, path, data = {}, headers = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/dropbox-api/connection/requests.rb', line 36

def get_raw(endpoint, path, data = {}, headers = {})
  query = Dropbox::API::Util.query(data)
  request(:raw => true) do
    token(endpoint).get "#{Dropbox::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
  end
end

#post(endpoint, path, data = {}, headers = {}) ⇒ Object



50
51
52
53
54
# File 'lib/dropbox-api/connection/requests.rb', line 50

def post(endpoint, path, data = {}, headers = {})
  request do
    token(endpoint).post "#{Dropbox::API::Config.prefix}#{path}", data, headers
  end
end

#put(endpoint, path, data = {}, headers = {}) ⇒ Object



56
57
58
59
60
# File 'lib/dropbox-api/connection/requests.rb', line 56

def put(endpoint, path, data = {}, headers = {})
  request do
    token(endpoint).put "#{Dropbox::API::Config.prefix}#{path}", data, headers
  end
end

#request(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dropbox-api/connection/requests.rb', line 8

def request(options = {})
  response = yield
  raise Dropbox::API::Error::ConnectionFailed if !response
  status = response.code.to_i
  case status
    when 401
      raise Dropbox::API::Error::Unauthorized
    when 403
      parsed = MultiJson.decode(response.body)
      raise Dropbox::API::Error::Forbidden.new(parsed["error"])
    when 404
      raise Dropbox::API::Error::NotFound
    when 400, 406
      parsed = MultiJson.decode(response.body)
      raise Dropbox::API::Error.new(parsed["error"])
    when 300..399
      raise Dropbox::API::Error::Redirect
    when 500..599
      raise Dropbox::API::Error
    else
      options[:raw] ? response.body : MultiJson.decode(response.body)
  end
rescue Dropbox::API::Error::Redirect
  return Dropbox::API::Response::NotModified.new             
end