class Manabu::Connection::Transactor
Handles transactions between server, abstracting the transport protocol and returning objects
Attributes
api_version[RW]
force_secure_connection[RW]
server_port[RW]
server_url[RW]
status[RW]
transport_type[RW]
Public Class Methods
new(server_url, server_port = 80, force_secure_connection = true, transport_type = :msgpack, **options)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 16 def initialize(server_url, server_port = 80, force_secure_connection = true, transport_type = :msgpack, **options) @server_url = server_url @server_port = server_port @transport_type = transport_type @status = :unknown @force_secure_connection = force_secure_connection @options = options @api_version = options.fetch(:api_version, 1) connect _check_server_status end
Public Instance Methods
_check_server_status()
click to toggle source
# File lib/manabu/connection/transactor.rb, line 137 def _check_server_status @protocol = 'https' @status = get('status')[:status] rescue Faraday::ConnectionFailed unless @force_secure_connection @protocol = 'http' @status = get('status')[:status] end end
_datafy_json(body)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 131 def _datafy_json(body) JSON.parse(body, symbolize_names: true) rescue JSON::ParseError raise Manabu::Connection::Error::InvalidJSON, 'Malformed data from server!' end
_datafy_msgpack(body)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 124 def _datafy_msgpack(body) MessagePack::DefaultFactory.register_type(0x00, Symbol) MessagePack.unpack(body) rescue raise Manabu::Connection::Error::InvalidMsgPack, 'Malformed data from server!' end
_datafy_response(body)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 113 def _datafy_response(body) case @transport_type when :msgpack return _datafy_msgpack(body) when :json return _datafy_json(body) end body # Just return raw data if no transport type was specified... end
_define_action(action, endpoint, args)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 80 def _define_action(action, endpoint, args) response = connect.send( action, URI.encode( "#{full_host}/api/v#{@api_version}/#{endpoint}"), args, _header_hash ) _status_raiser(response) _datafy_response(response.body) end
_header_hash()
click to toggle source
# File lib/manabu/connection/transactor.rb, line 92 def _header_hash Hash.new.tap do |h| h['Authorization'] = authorization if authorization end end
_kludge_windows()
click to toggle source
Windows doesn't supply us with the correct cacert.pem, so we force it
# File lib/manabu/connection/transactor.rb, line 148 def _kludge_windows cert_loc = "#{__dir__}/cacert.pem" unless File.exist? cert_loc response = @connection.get('http://curl.haxx.se/ca/cacert.pem') File.open(cert_loc, 'wb') { |fp| fp.write(response.body) } end ENV['SSL_CERT_FILE'] = cert_loc end
_status_raiser(response)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 99 def _status_raiser(response) case response.status when 200..299 return # don't raise else case @transport_type when :msgpack raise Error::UnprocessableEntity, _datafy_msgpack(response.body) when :json raise Error::UnprocessableEntity, _datafy_json(response.body) end end end
connect()
click to toggle source
# File lib/manabu/connection/transactor.rb, line 29 def connect() return @connection if @connection @connection = Faraday.new do |conn| conn.request :multipart conn.request :url_encoded case @transport_type when :msgpack conn.headers['Accept'] = 'application/msgpack' when :json conn.headers['Accept'] = 'application/json' else # someone messed up, defaulting to msgpack @transport_type = :msgpack conn.headers['Accept'] = 'application/msgpack' end conn.use FaradayMiddleware::FollowRedirects, limit: 5 conn.adapter :typhoeus end _kludge_windows if Gem.win_platform? _check_server_status end
delete(endpoint, **args)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 72 def delete(endpoint, **args) _define_action(:delete, endpoint, args) end
full_host()
click to toggle source
# File lib/manabu/connection/transactor.rb, line 76 def full_host "#{@protocol}://#{@server_url}:#{@server_port}" end
get(endpoint, **args)
click to toggle source
Gets data from the server
# File lib/manabu/connection/transactor.rb, line 59 def get(endpoint, **args) _define_action(:get, endpoint, args) end
patch(endpoint, **args)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 68 def patch(endpoint, **args) _define_action(:patch, endpoint, args) end
post(endpoint, **args)
click to toggle source
Sets data from the server
# File lib/manabu/connection/transactor.rb, line 64 def post(endpoint, **args) _define_action(:post, endpoint, args) end
simple_get(endpoint)
click to toggle source
# File lib/manabu/connection/transactor.rb, line 54 def simple_get(endpoint) Faraday.get("#{full_host}/api/v#{@api_version}/#{endpoint}") end