class TeamdriveApi::Base
API-Baseclass for all XML RPC APIs
Attributes
uri[R]
Public Class Methods
new(uri, api_checksum_salt, api_version)
click to toggle source
Create a new TD API Client.
@param [String] uri URI to the API. See README. @param [String] api_checksum_salt the APIChecksumSalt
system setting
("Edit Settings -> RegServer").
@param [String] api_version optionally overwrite the api_version
# File lib/teamdrive_api/base.rb, line 18 def initialize(uri, api_checksum_salt, api_version) @api_checksum_salt = api_checksum_salt @api_version = api_version @uri = uri.start_with?('http') ? uri : 'https://' + uri end
Public Instance Methods
payload_for(command, query = {})
click to toggle source
Generates the XML payload for the RPC
# File lib/teamdrive_api/base.rb, line 25 def payload_for(command, query = {}) out = header_for(command) query.each do |k, v| next if v.nil? v = v.to_s v = '$' + v if %w(true false).include?(v) out << "<#{k}>#{v}</#{k}>" end out << '</teamdrive>' end
Private Instance Methods
check_for(method, hash, params, message)
click to toggle source
# File lib/teamdrive_api/base.rb, line 47 def check_for(method, hash, params, message) keys = [params].flatten return if keys.send(method) { |k| hash.key?(k) } msg = keys.map { |k| %("#{k}") }.join(', ') fail ArgumentError, "Provide #{message} of #{msg}" end
header_for(command)
click to toggle source
Generates the XML header for the RPC
# File lib/teamdrive_api/base.rb, line 39 def header_for(command) out = "<?xml version='1.0' encoding='UTF-8' ?>" out << '<teamdrive>' out << "<apiversion>#{@api_version}</apiversion>" out << "<command>#{command}</command>" out << "<requesttime>#{Time.now.to_i}</requesttime>" end
require_all(of: [], in_hash: {})
click to toggle source
make sure all of the keys in of
exists in in_hash
. Raise an ArgumentError
if not so.
# File lib/teamdrive_api/base.rb, line 62 def require_all(of: [], in_hash: {}) check_for(:all?, in_hash, of, 'all') end
require_one(of: [], in_hash: {})
click to toggle source
make sure at least one of the keys in of
exists in in_hash
. Raise an ArgumentError
if not so.
# File lib/teamdrive_api/base.rb, line 56 def require_one(of: [], in_hash: {}) check_for(:any?, in_hash, of, 'at least one') end
return_or_fail(response)
click to toggle source
raise TeamdriveApi::Error
if response
contains an exception, return response
otherwise
# File lib/teamdrive_api/base.rb, line 81 def return_or_fail(response) return response if response[:exception].nil? fail TeamdriveApi::Error.new response[:exception][:primarycode], response[:exception][:secondarycode], response[:exception][:message] end
send_request(command, data = {})
click to toggle source
actually send a HTTP request
# File lib/teamdrive_api/base.rb, line 67 def send_request(command, data = {}) body = payload_for(command, data) res = self.class.post( @uri, headers: { 'User-Agent' => "TeamdriveApi v#{TeamdriveApi::VERSION}" }, body: body, query: { checksum: Digest::MD5.hexdigest(body + @api_checksum_salt) } ) return_or_fail(res['teamdrive'].symbolize_keys) end