Sha256: 15c665e2029a64095711a0ac5fd6699a43bced46c625ac6fe0f0b6b61fb7ab71

Contents?: true

Size: 887 Bytes

Versions: 5

Compression:

Stored size: 887 Bytes

Contents

require 'uri'

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

module Tickethub
  class Endpoint
    attr_reader :options, :url

    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 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

5 entries across 5 versions & 1 rubygems

Version Path
tickethub-0.3.35 lib/tickethub/endpoint.rb
tickethub-0.3.34 lib/tickethub/endpoint.rb
tickethub-0.3.33 lib/tickethub/endpoint.rb
tickethub-0.3.32 lib/tickethub/endpoint.rb
tickethub-0.3.31 lib/tickethub/endpoint.rb