Class: Manabu::Connection::Transactor
- Inherits:
-
Object
- Object
- Manabu::Connection::Transactor
- Defined in:
- lib/manabu/connection/transactor.rb
Overview
Handles transactions between server, abstracting the transport protocol and returning objects
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#authorization ⇒ Object
Returns the value of attribute authorization.
-
#force_secure_connection ⇒ Object
Returns the value of attribute force_secure_connection.
-
#server_port ⇒ Object
Returns the value of attribute server_port.
-
#server_url ⇒ Object
Returns the value of attribute server_url.
-
#status ⇒ Object
Returns the value of attribute status.
-
#transport_type ⇒ Object
Returns the value of attribute transport_type.
Instance Method Summary collapse
- #_check_server_status ⇒ Object
- #_datafy_json(body) ⇒ Object
- #_datafy_msgpack(body) ⇒ Object
- #_datafy_response(body) ⇒ Object
- #_define_action(action, endpoint, args) ⇒ Object
- #_header_hash ⇒ Object
-
#_kludge_windows ⇒ Object
Windows doesn’t supply us with the correct cacert.pem, so we force it.
- #_status_raiser(response) ⇒ Object
- #connect ⇒ Object
- #delete(endpoint, **args) ⇒ Object
- #full_host ⇒ Object
-
#get(endpoint, **args) ⇒ Object
Gets data from the server.
-
#initialize(server_url, server_port = 80, force_secure_connection = true, transport_type = :msgpack, **options) ⇒ Transactor
constructor
A new instance of Transactor.
- #patch(endpoint, **args) ⇒ Object
-
#post(endpoint, **args) ⇒ Object
Sets data from the server.
- #simple_get(endpoint) ⇒ Object
Constructor Details
#initialize(server_url, server_port = 80, force_secure_connection = true, transport_type = :msgpack, **options) ⇒ Transactor
Returns a new instance of Transactor.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/manabu/connection/transactor.rb', line 16 def initialize(server_url, server_port = 80, force_secure_connection = true, transport_type = :msgpack, **) @server_url = server_url @server_port = server_port @transport_type = transport_type @status = :unknown @force_secure_connection = force_secure_connection @options = @api_version = .fetch(:api_version, 1) connect _check_server_status end |
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def api_version @api_version end |
#authorization ⇒ Object
Returns the value of attribute authorization.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def @authorization end |
#force_secure_connection ⇒ Object
Returns the value of attribute force_secure_connection.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def force_secure_connection @force_secure_connection end |
#server_port ⇒ Object
Returns the value of attribute server_port.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def server_port @server_port end |
#server_url ⇒ Object
Returns the value of attribute server_url.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def server_url @server_url end |
#status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def status @status end |
#transport_type ⇒ Object
Returns the value of attribute transport_type.
13 14 15 |
# File 'lib/manabu/connection/transactor.rb', line 13 def transport_type @transport_type end |
Instance Method Details
#_check_server_status ⇒ Object
137 138 139 140 141 142 143 144 145 |
# 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) ⇒ Object
131 132 133 134 135 |
# 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) ⇒ Object
124 125 126 127 128 129 |
# 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) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# 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 ⇒ Object
92 93 94 95 96 |
# File 'lib/manabu/connection/transactor.rb', line 92 def _header_hash Hash.new.tap do |h| h['Authorization'] = if end end |
#_kludge_windows ⇒ Object
Windows doesn’t supply us with the correct cacert.pem, so we force it
148 149 150 151 152 153 154 155 |
# 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) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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 ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
72 73 74 |
# File 'lib/manabu/connection/transactor.rb', line 72 def delete(endpoint, **args) _define_action(:delete, endpoint, args) end |
#full_host ⇒ Object
76 77 78 |
# File 'lib/manabu/connection/transactor.rb', line 76 def full_host "#{@protocol}://#{@server_url}:#{@server_port}" end |
#get(endpoint, **args) ⇒ Object
Gets data from the server
59 60 61 |
# File 'lib/manabu/connection/transactor.rb', line 59 def get(endpoint, **args) _define_action(:get, endpoint, args) end |
#patch(endpoint, **args) ⇒ Object
68 69 70 |
# File 'lib/manabu/connection/transactor.rb', line 68 def patch(endpoint, **args) _define_action(:patch, endpoint, args) end |
#post(endpoint, **args) ⇒ Object
Sets data from the server
64 65 66 |
# File 'lib/manabu/connection/transactor.rb', line 64 def post(endpoint, **args) _define_action(:post, endpoint, args) end |
#simple_get(endpoint) ⇒ Object
54 55 56 |
# File 'lib/manabu/connection/transactor.rb', line 54 def simple_get(endpoint) Faraday.get("#{full_host}/api/v#{@api_version}/#{endpoint}") end |