Class: HorizonClient::Connection
- Inherits:
-
Object
- Object
- HorizonClient::Connection
- Defined in:
- lib/horizon_client/connection.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #get(path = '', params = {}) ⇒ Object
-
#initialize(url = nil) ⇒ Connection
constructor
A new instance of Connection.
- #log(message, params = {}) ⇒ Object
- #post(path = '', body) ⇒ Object
- #url_prefix ⇒ Object
Constructor Details
#initialize(url = nil) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/horizon_client/connection.rb', line 5 def initialize(url = nil) url ||= ENV['HORIZON_REST_URL'] @connection = Faraday.new url do |conn| conn.response :raise_error conn.use HorizonClient::Response::ParseXml conn.use HorizonClient::Request::EncodeXml conn..timeout = 20 conn..open_timeout = 2 conn.adapter Faraday.default_adapter end end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/horizon_client/connection.rb', line 3 def logger @logger end |
Instance Method Details
#get(path = '', params = {}) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/horizon_client/connection.rb', line 23 def get(path = '', params = {}) log "GET #{path}", params do response = @connection.get path, params response.body end end |
#log(message, params = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/horizon_client/connection.rb', line 42 def log(, params = {}) t1 = Time.now response = yield ensure t2 = Time.now duration = (t2 - t1) / 1000 if logger log_item = "Horizon (#{duration}ms) #{}" \ "\nBody:" \ "\n#{params[:body].respond_to?(:xml) ? params[:body].xml : params[:body].inspect}" \ "\nResponse:" \ "\n#{response.respond_to?(:xml) ? response.xml : response.inspect}" logger.info log_item end end |
#post(path = '', body) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/horizon_client/connection.rb', line 30 def post(path = '', body) log "GET #{path}", {body: body} do response = @connection.post do |req| req.url path req.headers['Content-Type'] = 'application/xml;charset=UTF-8' req.body = body end response.body end end |
#url_prefix ⇒ Object
19 20 21 |
# File 'lib/horizon_client/connection.rb', line 19 def url_prefix @connection.url_prefix end |