Class: Kuvera::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kuvera/api/client.rb

Constant Summary collapse

HOST =
'https://kuvera.io/'
ENV_UID =
ENV['OAUTH_UID']
ENV_SECRET =
ENV['OAUTH_SECRET']
ENV_HOST =
ENV.fetch('KUVERA_API', HOST)
AT_ENDPOINT =
'at/'
ME_ENDPOINT =
'/api/v1/me'
UPLOAD_ENDPOINT =
'/api/v1/secrets'
SHARE_ENDPOINT =
'/api/v1/links'
ROOT =
'secret'
ATTACHMENT =
'SecretAttachment'
RESPONSES =
{
  200 => ->(response) { response.body },
  403 => ->(_response) { raise SecretAlreadyRead },
  404 => ->(_response) { raise SecretNotFound }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(host: ENV_HOST, uid: ENV_UID, secret: ENV_SECRET) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
# File 'lib/kuvera/api/client.rb', line 25

def initialize(host: ENV_HOST, uid: ENV_UID, secret: ENV_SECRET)
  @host = host
  @uid = uid
  @secret = secret
end

Instance Method Details

#at(address) ⇒ Object



37
38
39
40
# File 'lib/kuvera/api/client.rb', line 37

def at(address)
  response = Net::HTTP.get_response(URI.join(@host, AT_ENDPOINT, address))
  RESPONSES.fetch(response.code.to_i).call(response)
end

#meObject



42
43
44
# File 'lib/kuvera/api/client.rb', line 42

def me
  oauth.get(ME_ENDPOINT)
end

#share(address) ⇒ Object



52
53
54
# File 'lib/kuvera/api/client.rb', line 52

def share(address)
  oauth.post(SHARE_ENDPOINT, { secret_id: address }.to_json)
end

#upload(title, io, mime) ⇒ Object



46
47
48
49
50
# File 'lib/kuvera/api/client.rb', line 46

def upload(title, io, mime)
  upload = Faraday::UploadIO.new(io, mime, io.path.split('/').last)
  body = { new_secret: { type: ATTACHMENT, title: title, body: upload } }
  oauth.post(UPLOAD_ENDPOINT, body, headers: {})
end