Class: TrainPlugins::Habitat::HTTPGateway
- Inherits:
-
Object
- Object
- TrainPlugins::Habitat::HTTPGateway
- Defined in:
- lib/train-habitat/httpgateway.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
readonly
Private accessor.
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
Instance Method Summary collapse
- #get_path(path) ⇒ Object
-
#initialize(opts) ⇒ HTTPGateway
constructor
A new instance of HTTPGateway.
Constructor Details
#initialize(opts) ⇒ HTTPGateway
Returns a new instance of HTTPGateway.
11 12 13 14 15 16 17 18 19 |
# File 'lib/train-habitat/httpgateway.rb', line 11 def initialize(opts) @base_uri = URI(opts[:url]) # check for provided port and default if not provided if base_uri.port == 80 && opts[:url] !~ %r{\w+:\d+(\/|$)} base_uri.port = 9631 end @auth_token = opts[:auth_token] end |
Instance Attribute Details
#auth_token ⇒ Object (readonly)
Private accessor
43 44 45 |
# File 'lib/train-habitat/httpgateway.rb', line 43 def auth_token @auth_token end |
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
9 10 11 |
# File 'lib/train-habitat/httpgateway.rb', line 9 def base_uri @base_uri end |
Instance Method Details
#get_path(path) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/train-habitat/httpgateway.rb', line 21 def get_path(path) uri = base_uri.dup uri.path = path headers = {} unless auth_token.nil? headers["Authorization"] = "Bearer " + auth_token # Set bearer token, see https://www.habitat.sh/docs/using-habitat/#authentication end conn = Net::HTTP.start(uri.host, uri.port) conn.read_timeout = 5 resp = Response.new resp.raw_response = conn.get(uri, headers) resp.code = resp.raw_response.code.to_i if resp.code == 200 resp.body = JSON.parse(resp.raw_response.body, symbolize_names: true) end resp end |