Class: Stream::APIURLGenerator

Inherits:
URLGenerator show all
Defined in:
lib/stream/url.rb

Instance Attribute Summary

Attributes inherited from URLGenerator

#base_path, #options, #url

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ APIURLGenerator

Returns a new instance of APIURLGenerator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stream/url.rb', line 9

def initialize(options)
  super()
  @options = options
  location = make_location(options[:location])
  location ||= 'api'
  api_version = options[:api_version] || 'v1.0'
  if ENV.fetch('STREAM_URL', nil)
    uri = URI.parse(ENV.fetch('STREAM_URL'))
    scheme = uri.scheme
    host = uri.host
    port = uri.port
  else
    scheme = 'https'
    host = options[:api_hostname]
    port = 443
  end
  unless ENV.fetch('STREAM_URL', nil) =~ /localhost/
    host_parts = host.split('.')
    host = host_parts.slice(1..-1).join('.') if host_parts.length == 3
    host = "#{location}.#{host}" if location
  end
  @base_path = "/api/#{api_version}"
  @url = "#{scheme}://#{host}:#{port}#{@base_path}"
end