module Pcloud::Client

Constants

EU_API_BASE
TIMEOUT_SECONDS
US_API_BASE
VALID_DATA_REGIONS

Public Class Methods

configure(access_token: nil, data_region: nil) click to toggle source
# File lib/pcloud/client.rb, line 15
def configure(access_token: nil, data_region: nil)
  @@access_token = access_token
  @@data_region = data_region
  true # Don't accidentally return any secrets from the configure method
end
execute(method, query: {}, body: {}) click to toggle source
# File lib/pcloud/client.rb, line 21
def execute(method, query: {}, body: {})
  verb = ["uploadfile"].include?(method) ? :post : :get
  options = {
    headers: { "Authorization" => "Bearer #{access_token}" },
    timeout: TIMEOUT_SECONDS # this sets both the open and read timeouts to the same value
  }
  options[:query] = query unless query.empty?
  options[:body] = body unless body.empty?
  response = HTTParty.public_send(verb, "https://#{closest_server}/#{method}", options)
  json_response = JSON.parse(response.body)
  raise ErrorResponse.new(json_response["error"]) if json_response["error"]
  json_response
end

Private Class Methods

access_token() click to toggle source
# File lib/pcloud/client.rb, line 44
def access_token
  @@access_token ||= ENV["PCLOUD_API_ACCESS_TOKEN"]
  return @@access_token unless @@access_token.nil?
  raise ConfigurationError.new("Missing pCloud API access token")
end
data_region() click to toggle source
# File lib/pcloud/client.rb, line 37
def data_region
  @@data_region ||= ENV["PCLOUD_API_DATA_REGION"]
  return @@data_region if VALID_DATA_REGIONS.include?(@@data_region)
  raise ConfigurationError.new("Missing pCloud data region") if @@data_region.nil?
  raise ConfigurationError.new("Invalid pCloud data region, must be one of #{VALID_DATA_REGIONS}")
end