class Occson::Downloader
Downloads and decrypts the document at given URI with given access token. Decryption occurs using given passphrase.
Public Class Methods
new(uri, access_token, passphrase)
click to toggle source
Constructs a Downloader
instance from a given URI, access token and passphrase.
@example
uri = 'occson://path/to/file.yml' access_token = 'f30b5450421362c9ca0b' passphrase = 'my document passphrase' Occson::Downloader.new(uri, access_token, passphrase)
@param uri [String] Document
URI. Accepts ‘occson://` as shorthand for Occson
location. @param access_token [String] Occson
access token. @param passphrase [String] Document
passphrase, used in encryption and decryption.
# File lib/occson/downloader.rb, line 19 def initialize(uri, access_token, passphrase) @uri = uri @access_token = access_token @passphrase = passphrase end
Public Instance Methods
call()
click to toggle source
Performs the download and decryption of document.
@return [String|nil] Decrypted body of the document or ‘nil` in case the
server did not respond with a `200` HTTP code.
# File lib/occson/downloader.rb, line 29 def call response = http.request(request) body = response.body return unless response.code.eql? '200' json = JSON.parse body Decrypter.new(@passphrase, json['encrypted_content']).call end
Private Instance Methods
headers()
click to toggle source
# File lib/occson/downloader.rb, line 52 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/occson/downloader.rb, line 40 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/occson/downloader.rb, line 46 def request Net::HTTP::Get.new(@uri.path, headers).tap do |request| request["User-Agent"] = format('occson/%s', Occson::VERSION) end end