Sha256: 0c7d5ecfe69492c2855952003a8d6fc291200b2cf92c759d30fe111fe35b55f5

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

module Stream
  class URLGenerator
    attr_reader :options
    attr_reader :base_path
    attr_reader :url
  end

  class APIURLGenerator < URLGenerator
    def initialize(options)
      @options = options
      location = make_location(options[:location])
      location ||= "api"
      api_version = options[:api_version] ? options[:api_version] : 'v1.0'
      if ENV['STREAM_URL']
        uri = URI.parse(ENV['STREAM_URL'])
        scheme = uri.scheme
        host = uri.host
        port = uri.port
      else
        scheme = 'https'
        host = options[:api_hostname]
        port = 443
      end
      unless ENV['STREAM_URL'] =~ /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

    private

    def make_location(loc)
      case loc
      when 'us-east'
        'us-east-api'
      when 'eu-west'
        'eu-west-api'
      when 'singapore'
        'singapore-api'
      else
        loc
      end
    end
  end

  class PersonalizationURLGenerator < URLGenerator
    def initialize(options)
      @options = options
      host = 'personalization.stream-io-api.com'
      @base_path = '/personalization/v1.0'
      @url = "https://#{host}#{@base_path}"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
stream-ruby-3.1.0 lib/stream/url.rb
stream-ruby-3.0.1 lib/stream/url.rb
stream-ruby-3.0.0 lib/stream/url.rb
stream-ruby-2.11.0 lib/stream/url.rb
stream-ruby-2.10.0 lib/stream/url.rb
stream-ruby-2.9.3 lib/stream/url.rb
stream-ruby-2.9.2 lib/stream/url.rb
stream-ruby-2.9.1 lib/stream/url.rb
stream-ruby-2.9.0 lib/stream/url.rb
stream-ruby-2.8.0 lib/stream/url.rb
stream-ruby-2.7.1 lib/stream/url.rb
stream-ruby-2.7.0 lib/stream/url.rb
stream-ruby-2.6.1 lib/stream/url.rb
stream-ruby-2.6.0 lib/stream/url.rb