Sha256: e4523ad0a10ef59402c23b228c581822c236c909668af5e373311e8fa45d83b5

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

class Apisync
  module Http

    # Responsible for generating URLs
    class Url
      DEFAULT_HOST = "https://api.apisync.io"

      # - resource_name: a name in plural such as 'users', 'profiles' etc.
      # - id: id of the resource that you're looking for
      # - filters: these will define what's in the query string, such as
      #   'filter[application-id]=value'
      # - options: allows you to pass options such 'host'. Accepted options are
      #
      #   - host: a custom host for the URL, defaults to DEFAULT_HOST
      def initialize(resource_name:, id: nil, filters: nil, options:)
        @resource_name = resource_name
        @id = id
        @filters = filters
        @options = {
          host: nil
        }.merge(options)
      end

      # to_s
      #
      # Takes a host, api_version, resource name and id and form the URL. Then
      # pass filters and other options into QueryString class which will return
      # whatever is after the `?` symbol.
      #
      # Returns a string such as
      #
      #    'https://api.apisync.io/inventory-items?filter[application-id]=abc'
      #
      # If there are no query strings, omits the `?`
      #
      #    'https://api.apisync.io/inventory-items'
      #
      def to_s
        url = [
          host,
          api_version,
          normalized_resource_name,
          @id
        ].compact.join("/")
        url = remove_duplicated_slashes(url)
        [url, query_string].compact.join("?")
      end

      private

      def api_version
        Apisync::HttpClient::VERSION_PREFIX
      end

      def host
        @options[:host] || DEFAULT_HOST
      end

      def query_string
        str = Apisync::Http::QueryString.new(filters: @filters).to_s
        str if str != ""
      end

      def normalized_resource_name
        @resource_name.to_s.downcase.gsub("_", "-")
      end

      def remove_duplicated_slashes(string)
        string.gsub(/([^:])\/\//, '\1/')
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
apisync-0.3.0 lib/apisync/http/url.rb
apisync-0.2.3 lib/apisync/http/url.rb
apisync-0.2.2 lib/apisync/http/url.rb
apisync-0.2.1 lib/apisync/http/url.rb
apisync-0.2.0 lib/apisync/http/url.rb
apisync-0.1.6 lib/apisync/http/url.rb
apisync-0.1.5 lib/apisync/http/url.rb
apisync-0.1.4 lib/apisync/http/url.rb
apisync-0.1.3 lib/apisync/http/url.rb