class BrickFTP::RESTfulAPI::Client

Constants

DEFAULT_BASE_URL_TEMPLATE
ErrorResponse

ref. developers.files.com/#errors

OPEN_TIMEOUT
READ_TIMEOUT
USER_AGENT

Public Class Methods

new(base_url, api_key) click to toggle source

Initialize REST API client.

@param [String] base_url @param [String] api_key

# File lib/brick_ftp/restful_api/client.rb, line 41
def initialize(base_url, api_key)
  @base_uri = make_base_uri(base_url)
  @http = Net::HTTP.new(@base_uri.host, @base_uri.port)
  @http.use_ssl = (@base_uri.scheme == 'https')
  @http.open_timeout = OPEN_TIMEOUT
  @http.read_timeout = READ_TIMEOUT
  @request_headers = {
    'User-Agent' => USER_AGENT,
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
  }
  @api_key = api_key
end

Public Instance Methods

delete(path, headers = nil) click to toggle source

Send HTTP request via DELETE method.

@param [String] path the request path including query string. @param [Hash, nil] headers additional request headers @return [Hash] JSON parsed object.

# File lib/brick_ftp/restful_api/client.rb, line 123
def delete(path, headers = nil)
  req = Net::HTTP::Delete.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end
get(path, headers = nil) click to toggle source

Send HTTP request via GET method.

@param [String] path the request path including query string. @param [Hash, nil] headers additional request headers @return [Hash] JSON parsed object.

# File lib/brick_ftp/restful_api/client.rb, line 61
def get(path, headers = nil)
  req = Net::HTTP::Get.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end
patch(path, data = nil, headers = nil) click to toggle source

Send HTTP request via PATCH method.

@param [String] path the request path including query string. @param [Hash, nil] data the request body @param [Hash, nil] headers additional request headers @return [Hash] JSON parsed object.

# File lib/brick_ftp/restful_api/client.rb, line 108
def patch(path, data = nil, headers = nil)
  req = Net::HTTP::Patch.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end
post(path, data = nil, headers = nil) click to toggle source

Send HTTP request via POST method.

@param [String] path the request path including query string. @param [Hash, nil] data the request body @param [Hash, nil] headers additional request headers @return [Hash] JSON parsed object.

# File lib/brick_ftp/restful_api/client.rb, line 76
def post(path, data = nil, headers = nil)
  req = Net::HTTP::Post.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end
put(path, data = nil, headers = nil) click to toggle source

Send HTTP request via PUT method.

@param [String] path the request path including query string. @param [Hash, nil] data the request body @param [Hash, nil] headers additional request headers @return [Hash] JSON parsed object.

# File lib/brick_ftp/restful_api/client.rb, line 92
def put(path, data = nil, headers = nil)
  req = Net::HTTP::Put.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end
upload_file(http_method, upload_url, io) click to toggle source

Upload file.

@param [String] http_method Value is `PUT` or `POST`, and is the HTTP method used when uploading the file. @param [String] upload_url The URL where the file is uploaded to. @param [IO] io uploading data @return [Integer] content length

# File lib/brick_ftp/restful_api/client.rb, line 138
def upload_file(http_method, upload_url, io)
  raise ArgumentError, "Unsupported HTTP method `#{http_method}`" unless %w[POST PUT].include?(http_method)

  uri = URI.parse(upload_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  req = Net::HTTP.const_get(http_method.capitalize).new(uri.request_uri)
  req.body_stream = io
  req['Content-Length'] = io.size
  res = http.start { |session| session.request(req) }

  return io.size if res.is_a?(Net::HTTPSuccess)

  raise Error, parse_error_response(res)
end

Private Instance Methods

handle_response(response) click to toggle source
# File lib/brick_ftp/restful_api/client.rb, line 166
def handle_response(response)
  case response
  when Net::HTTPNoContent
    nil
  when Net::HTTPSuccess
    parse_success_response(response)
  else
    error = parse_error_response(response)
    raise Error, error
  end
end
make_base_uri(url_or_subdomain) click to toggle source
# File lib/brick_ftp/restful_api/client.rb, line 158
def make_base_uri(url_or_subdomain)
  unless /[.:]/.match?(url_or_subdomain)
    url_or_subdomain = format(DEFAULT_BASE_URL_TEMPLATE, subdomain: url_or_subdomain)
  end

  URI.parse(url_or_subdomain)
end
parse_error_response(response) click to toggle source
# File lib/brick_ftp/restful_api/client.rb, line 182
def parse_error_response(response)
  parsed = begin
    JSON.parse(response.body)
  rescue StandardError
    {}
  end
  parsed = {} unless parsed.is_a?(Hash)

  ErrorResponse.new(parsed.symbolize_keys).tap do |e|
    e['http-code'] ||= response.code
    e['error'] ||= response.body
  end
end
parse_success_response(response) click to toggle source
# File lib/brick_ftp/restful_api/client.rb, line 178
def parse_success_response(response)
  JSON.parse(response.body)
end