Module: Rubytus::Helpers

Includes:
Common, Constants
Included in:
API
Defined in:
lib/rubytus/helpers.rb

Constant Summary

Constants included from Constants

Constants::BASE_PATH_REGEX, Constants::COMMON_HEADERS, Constants::DEFAULT_BASE_PATH, Constants::DEFAULT_DATA_DIR, Constants::DEFAULT_MAX_SIZE, Constants::DEFAULT_STORAGE, Constants::ENV_BASE_PATH, Constants::ENV_DATA_DIR, Constants::ENV_MAX_SIZE, Constants::ENV_STORAGE, Constants::RESOURCE_UID_REGEX, Constants::RESUMABLE_CONTENT_TYPE, Constants::STATUS_BAD_REQUEST, Constants::STATUS_CREATED, Constants::STATUS_FORBIDDEN, Constants::STATUS_INTERNAL_ERROR, Constants::STATUS_NOT_ALLOWED, Constants::STATUS_NOT_FOUND, Constants::STATUS_OK

Instance Method Summary collapse

Methods included from Common

#error!, #generate_uid

Instance Method Details

#prepare_headers(env, headers) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubytus/helpers.rb', line 10

def prepare_headers(env, headers)
  request = Rubytus::Request.new(env)

  # CREATE
  if request.collection? && request.post?
    uid = generate_uid

    env['api.action']       = :create
    env['api.uid']          = uid
    env['api.final_length'] = request.final_length
    env['api.resource_url'] = request.resource_url(uid)
  end

  if request.resource?
    # UID for this resource
    env['api.uid'] = request.resource_uid

    # HEAD
    if request.head?
      env['api.action'] = :head
    end

    # PATCH
    if request.patch?
      unless request.resumable_content_type?
        error!(STATUS_BAD_REQUEST, "Content-Type must be '#{RESUMABLE_CONTENT_TYPE}'")
      end

      env['api.action']  = :patch
      env['api.buffers'] = ''
      env['api.offset']  = request.offset
    end

    # GET
    if request.get?
      env['api.action'] = :get
    end
  end
end

#validates_base_path(base_path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rubytus/helpers.rb', line 62

def validates_base_path(base_path)
  unless base_path =~ BASE_PATH_REGEX
    raise ConfigurationError, "Invalid `base_path` configuration, it should be using format /uploads/, /user-data/, etc"
  end

  base_path
end

#validates_length(req_length, remaining) ⇒ Object



56
57
58
59
60
# File 'lib/rubytus/helpers.rb', line 56

def validates_length(req_length, remaining)
  if req_length > remaining
    error!(STATUS_FORBIDDEN, "Content-Length: #{req_length} exceeded remaining length: #{remaining}")
  end
end

#validates_max_size(max_size) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubytus/helpers.rb', line 70

def validates_max_size(max_size)
  if max_size.is_a? String
    max_size = max_size.to_i
  end

  if max_size <= 0
    raise ConfigurationError, "Invalid `max_size`, it should be > 0 bytes"
  end

  max_size
end

#validates_offset(req_offset, info_offset) ⇒ Object



50
51
52
53
54
# File 'lib/rubytus/helpers.rb', line 50

def validates_offset(req_offset, info_offset)
  if req_offset > info_offset
    error!(STATUS_FORBIDDEN, "Offset: #{req_offset} exceeds current offset: #{info_offset}")
  end
end