class Ccs::Uploader

Encrypts and uploads the document to Occson.

Public Class Methods

new(uri, content, access_token, passphrase, force: false) click to toggle source

Constructs an Uploader instance from a given URI, content, access token and passphrase.

@example

uri = 'ccs://path/to/file.yml'
content = 'my very secret message'
access_token = 'f30b5450421362c9ca0b'
passphrase = 'my document passphrase'

Ccs::Uploader.new(uri, access_token, passphrase)

@param uri [String] Document URI. Accepts `ccs://` as shorthand for Occson location. @param content [String] Plaintext for encryption and upload. @param access_token [String] Occson access token. @param passphrase [String] Document passphrase, used in encryption and decryption. @param force [Boolean] Whether to overwrite target document in Occson, if any. Default `false`.

# File lib/ccs/uploader.rb, line 21
def initialize(uri, content, access_token, passphrase, force: false)
  @uri = uri
  @content = content
  @access_token = access_token
  @passphrase = passphrase
  @force = force.to_s
end

Public Instance Methods

call() click to toggle source

Performs the actual upload to server.

@return [Boolean] `true` for a successful upload, `false` otherwise

# File lib/ccs/uploader.rb, line 32
def call
  request.body = { encrypted_content: encrypted_content, force: @force }.to_json
  %w[200 201].include?(http.request(request).code)
end

Private Instance Methods

encrypted_content() click to toggle source
# File lib/ccs/uploader.rb, line 58
def encrypted_content
  @encrypted_content ||= Encrypter.new(@passphrase, @content, salt).call
end
headers() click to toggle source
# File lib/ccs/uploader.rb, line 51
def headers
  {
    'Authorization' => format('Token token=%<access_token>s', access_token: @access_token),
    'Content-Type' => 'application/json'
  }
end
http() click to toggle source
# File lib/ccs/uploader.rb, line 39
def http
  @http ||= Net::HTTP.new(@uri.host, @uri.port).tap do |http|
    http.use_ssl = @uri.scheme.eql?('https')
  end
end
request() click to toggle source
# File lib/ccs/uploader.rb, line 45
def request
  @request ||= Net::HTTP::Post.new(@uri.path, headers).tap do |request|
    request["User-Agent"] = format('ccs/%s', Ccs::VERSION)
  end
end
salt() click to toggle source
# File lib/ccs/uploader.rb, line 62
def salt
  @access_token[0...8]
end