Class: DropboxApiV2::Request

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

Constant Summary collapse

API_URL =
"https://api.dropboxapi.com/2"
CONTENT_URL =
"https://content.dropboxapi.com/2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params = {}) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
17
18
# File 'lib/dropbox_api_v2/request.rb', line 12

def initialize(path, params={})
  @path          = path
  @token         = params.delete(:token)
  @body          = params.delete(:body)
  @file_transfer = params.delete(:file_transfer)
  @params        = params
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#verbObject (readonly)

Returns the value of attribute verb.



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

def verb
  @verb
end

Instance Method Details

#base_urlObject



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

def base_url
  file_transfer? ? CONTENT_URL : API_URL
end

#bodyObject



64
65
66
# File 'lib/dropbox_api_v2/request.rb', line 64

def body
  file_transfer? ? @body : json_params
end

#content_typeObject



44
45
46
47
48
49
50
51
52
# File 'lib/dropbox_api_v2/request.rb', line 44

def content_type
  if upload?
    "application/octet-stream"
  elsif download?
    ""
  else
    "application/json"
  end
end

#download?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dropbox_api_v2/request.rb', line 36

def download?
  !!(file_transfer? && !@body)
end

#file_transfer?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dropbox_api_v2/request.rb', line 40

def file_transfer?
  @file_transfer
end

#headersObject



54
55
56
57
58
# File 'lib/dropbox_api_v2/request.rb', line 54

def headers
  headers = {"Authorization": "Bearer #{@token}", "Content-Type": content_type}
  headers["Dropbox-API-Arg"] = json_params if file_transfer?
  headers
end

#json_paramsObject



28
29
30
# File 'lib/dropbox_api_v2/request.rb', line 28

def json_params
  @params.to_json
end

#performObject



68
69
70
71
72
# File 'lib/dropbox_api_v2/request.rb', line 68

def perform
  Curl.post(url, body) do |http|
    http.headers = headers
  end
end

#responseObject



60
61
62
# File 'lib/dropbox_api_v2/request.rb', line 60

def response
  DropboxApiV2::Response.new perform
end

#upload?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dropbox_api_v2/request.rb', line 32

def upload?
  !!(file_transfer? && @body)
end

#urlObject



24
25
26
# File 'lib/dropbox_api_v2/request.rb', line 24

def url
  base_url + path
end