Sha256: 211b38bc34bb97dbd0c2d5ee5811672f732eb73bb6d618daa2d498a02adc295c

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require 'xporter_on_demand/factory'

module XporterOnDemand
  class Endpoint
    include XporterOnDemand::Utils

    attr_accessor :endpoint, :options, :parameters, :pagination, :uri, :id

    def self.create(endpoint, args = {})
      endpoint_name = endpoint.to_s.classify

      unless const_defined?(endpoint_name, false)
        class_name = Class.new(self)
        endpoint_class = const_set(endpoint_name, class_name)
      end

      const_get(endpoint_name).new(args).tap do |s|
        s.instance_variable_set(:@endpoint, endpoint)
      end
    end

    def initialize(args = {})
      @options    = args.fetch(:options, [])
      @parameters = args.fetch(:parameters, {})

      @parameters[:page]      ||= 1
      @parameters[:page_size] ||= 25

      @id = args[:id]

      # Check that all the options are valid?
      # Check that the parameters are valid?
      # Check the pagination params are valid?
    end

    def build_query
      URI.escape(resource + "?" + build_parameters)
    end

    private
      def resource
        endpoint.to_s.camelize + "/" + (@id || "")
      end

      def build_options
        @options.any? ? ["options=" + @options.map{ |o| parameterize(o) }.join(',')] : []
      end

      def build_parameters
        (@parameters.map{ |k, v| parameterize(k) + "=" + v.to_s } + build_options).join('&')
      end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
xporter_on_demand-0.2.4 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.2.3 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.2.2 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.2.1 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.2.0 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.1.4 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.1.3 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.1.2 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.1.1 lib/xporter_on_demand/endpoint.rb
xporter_on_demand-0.1.0 lib/xporter_on_demand/endpoint.rb