Module: Attache::API::V1

Extended by:
V1
Included in:
V1
Defined in:
lib/attache/api/v1.rb

Defined Under Namespace

Classes: HTTPClient

Constant Summary collapse

ATTACHE_URL =
ENV.fetch('ATTACHE_URL')             { "http://localhost:9292" }
ATTACHE_UPLOAD_URL =
ENV.fetch('ATTACHE_UPLOAD_URL')      { "#{ATTACHE_URL}/upload" }
ATTACHE_DOWNLOAD_URL =
ENV.fetch('ATTACHE_DOWNLOAD_URL')    { "#{ATTACHE_URL}/view" }
ATTACHE_DELETE_URL =
ENV.fetch('ATTACHE_DELETE_URL')      { "#{ATTACHE_URL}/delete" }
ATTACHE_UPLOAD_DURATION =

expires signed upload form

ENV.fetch('ATTACHE_UPLOAD_DURATION') { 3*3600 }.to_i
ATTACHE_SECRET_KEY =

unset to test password-less interaction

ENV['ATTACHE_SECRET_KEY']

Instance Method Summary collapse

Instance Method Details

#attache_auth_optionsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/attache/api/v1.rb', line 44

def attache_auth_options
  if ATTACHE_SECRET_KEY
    uuid = SecureRandom.uuid
    expiration = (Time.now + ATTACHE_UPLOAD_DURATION).to_i
    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ATTACHE_SECRET_KEY, "#{uuid}#{expiration}")
    { uuid: uuid, expiration: expiration, hmac: hmac }
  else
    {}
  end
end

#attache_delete(*paths) ⇒ Object



37
38
39
40
41
42
# File 'lib/attache/api/v1.rb', line 37

def attache_delete(*paths)
  HTTPClient.post_content(
    URI.parse(ATTACHE_DELETE_URL),
    attache_auth_options.merge(paths: paths.join("\n"))
  )
end

#attache_options(geometry, current_value, auth_options: true, placeholder: nil, data: {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/attache/api/v1.rb', line 55

def attache_options(geometry, current_value, auth_options: true, placeholder: nil, data: {})
  {
    data: {
      geometry: geometry,
      value: [*current_value],
      placeholder: [*placeholder],
      uploadurl: ATTACHE_UPLOAD_URL,
      downloadurl: ATTACHE_DOWNLOAD_URL,
    }.merge(data || {}).merge(auth_options == false ? {} : attache_auth_options),
  }
end

#attache_retry_doing(max_retries, retries = 0, exception_class = Exception) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/attache/api/v1.rb', line 67

def attache_retry_doing(max_retries, retries = 0, exception_class = Exception)
  yield
rescue exception_class
  if (retries += 1) <= max_retries
    max_sleep_seconds = Float(2 ** retries)
    sleep rand(0..max_sleep_seconds)
    retry
  end
  raise
end

#attache_upload(readable) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/attache/api/v1.rb', line 19

def attache_upload(readable)
  uri = URI.parse(ATTACHE_UPLOAD_URL)
  original_filename =
    readable.respond_to?(:original_filename) && readable.original_filename ||
    readable.respond_to?(:path) && File.basename(readable.path) ||
    'noname'
  uri.query = { file: original_filename, **attache_auth_options }.collect {|k,v|
    CGI.escape(k.to_s) + "=" + CGI.escape(v.to_s)
  }.join('&')
  res = attache_retry_doing(3) { HTTPClient.post(uri, readable, {'Content-Type' => 'binary/octet-stream'}) }
  res.body
end

#attache_url_for(path, geometry) ⇒ Object



32
33
34
35
# File 'lib/attache/api/v1.rb', line 32

def attache_url_for(path, geometry)
  prefix, basename = File.split(path)
  [ATTACHE_DOWNLOAD_URL, prefix, CGI.escape(geometry), CGI.escape(basename)].join('/')
end