Sha256: 839cb014175fb9744c3abb9d6f2d1f04934e664d275a6ff3bf717dc490597f00

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

module Eligible
  class APIResource < EligibleObject
    def self.class_name
      name.split('::').last
    end

    def self.api_url(base, params = nil, param_id = nil)
      if params.nil?
        "/#{base}.json"
      else
        id = Util.value(params, param_id)
        "/#{base}/#{id}.json"
      end
    end

    def self.url
      if self == APIResource
        fail NotImplementedError, 'APIResource is an abstract class.  You should perform actions on its subclasses (Plan, Service, etc.)'
      end
      "/#{CGI.escape(class_name.downcase)}/"
    end

    def self.require_param(value, name)
      fail ArgumentError, "#{name} of the #{class_name} is required" if value.nil? || (value.is_a?(String) && value.empty?)
    end

    def self.send_request(method, url, api_key, params, required_param_name = nil)
      unless required_param_name.nil?
        required_param = Util.value(params, required_param_name)
        require_param(required_param, required_param_name)
      end
      response, api_key = Eligible.request(method, url, api_key, **params)
      Util.convert_to_eligible_object(response, api_key)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eligible-3.0.2 lib/eligible/api_resource.rb
eligible-3.0.1 lib/eligible/api_resource.rb
eligible-3.0.0 lib/eligible/api_resource.rb