Sha256: a43a803872dfed10aa27102953390c285b45cc4b483745be19c62afb82902953

Contents?: true

Size: 988 Bytes

Versions: 62

Compression:

Stored size: 988 Bytes

Contents

require 'uri'

require_relative 'request'
require_relative 'response'
require_relative 'helpers'

module Tickethub
  class Endpoint
    attr_reader :options

    def initialize(url, options)
      @url     = _normalize_path url
      @options = options
    end

    def [](*parts)
      parts = parts.compact.map { |part| _normalize_path part.to_s }
      self.class.new URI.join(url, *parts).to_s, @options
    end

    [:get, :post, :delete, :patch].each do |method|
      define_method method do |params = {}|
        request params, options.merge(method: method)
      end
    end

    def uri
      URI.parse @url
    end

    def url(params = {})
      return params.empty?? @url : "#{@url}?#{Helpers.to_param(params)}"
    end

    def request(params, options)
      raise 'this endpoint is readonly' if frozen?
      Tickethub::Request.new(url, options.merge(params: params)).execute
    end

    def _normalize_path(str)
      str.match(/\/$/) ? str : "#{str}/"
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
tickethub-0.3.100 lib/tickethub/endpoint.rb
tickethub-0.3.99 lib/tickethub/endpoint.rb
tickethub-0.3.98 lib/tickethub/endpoint.rb
tickethub-0.3.97 lib/tickethub/endpoint.rb
tickethub-0.3.96 lib/tickethub/endpoint.rb
tickethub-0.3.95 lib/tickethub/endpoint.rb
tickethub-0.3.94 lib/tickethub/endpoint.rb
tickethub-0.3.93 lib/tickethub/endpoint.rb
tickethub-0.3.92 lib/tickethub/endpoint.rb
tickethub-0.3.91 lib/tickethub/endpoint.rb
tickethub-0.3.90 lib/tickethub/endpoint.rb
tickethub-0.3.89 lib/tickethub/endpoint.rb
tickethub-0.3.88 lib/tickethub/endpoint.rb
tickethub-0.3.87 lib/tickethub/endpoint.rb
tickethub-0.3.86 lib/tickethub/endpoint.rb
tickethub-0.3.85 lib/tickethub/endpoint.rb
tickethub-0.3.84 lib/tickethub/endpoint.rb
tickethub-0.3.82 lib/tickethub/endpoint.rb
tickethub-0.3.81 lib/tickethub/endpoint.rb
tickethub-0.3.80 lib/tickethub/endpoint.rb