Sha256: 4371e1c75ebf463d5c5313f8d29d20c223bc700e5401faf9f4988f95792455ff

Contents?: true

Size: 845 Bytes

Versions: 19

Compression:

Stored size: 845 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 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

19 entries across 19 versions & 1 rubygems

Version Path
tickethub-0.3.0 lib/tickethub/endpoint.rb
tickethub-0.2.18 lib/tickethub/endpoint.rb
tickethub-0.2.17 lib/tickethub/endpoint.rb
tickethub-0.2.15 lib/tickethub/endpoint.rb
tickethub-0.2.14 lib/tickethub/endpoint.rb
tickethub-0.2.13 lib/tickethub/endpoint.rb
tickethub-0.2.12 lib/tickethub/endpoint.rb
tickethub-0.2.11 lib/tickethub/endpoint.rb
tickethub-0.2.10 lib/tickethub/endpoint.rb
tickethub-0.2.9 lib/tickethub/endpoint.rb
tickethub-0.2.8 lib/tickethub/endpoint.rb
tickethub-0.2.7 lib/tickethub/endpoint.rb
tickethub-0.2.6 lib/tickethub/endpoint.rb
tickethub-0.2.5 lib/tickethub/endpoint.rb
tickethub-0.2.4 lib/tickethub/endpoint.rb
tickethub-0.2.3 lib/tickethub/endpoint.rb
tickethub-0.2.2 lib/tickethub/endpoint.rb
tickethub-0.2.1 lib/tickethub/endpoint.rb
tickethub-0.2.0 lib/tickethub/endpoint.rb