Sha256: fd6b76bf428332d39a9129a8842353741cef16a4c3f0498984e4081a857aa85d

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

module CasinoHub
  class ApiResource
    include Operations::Get
    attr_accessor :id, :opts, :attributes

    def initialize(id=nil, opts={})
      @id = id
      @opts = opts
      @values = {}
      @attributes = {}
    end

    def self.class_name
      self.name.split('::')[-1]
    end

    def self.resource_url
      if self == ApiResource
        raise NotImplementedError
      end
      "/api/#{CGI.escape(class_name.tableize)}"
    end

    def self.get(id, opts={})
      instance = self.new(id, opts)
      instance.refresh
      instance
    end

    def inspect
      JSON.pretty_generate(attributes)
    end

    def resource_url
      "#{self.class.resource_url}/#{CGI.escape(id)}"
    end

    def refresh
      response = request(:get, resource_url, opts)
      initialize_from(response, opts)
    end

    private

    # Convert hash key value pair to method
    def initialize_from(values, opts={})
      @opts = opts
      added = Set.new(values.keys - @values.keys)
      @attributes = values
      add_accessors(added, values)
      self
    end

    def add_accessors(keys, values)
      metaclass.instance_eval do
        keys.each do |k|
          define_method(k) { values[k] }

          if [FalseClass, TrueClass].include?(values[k].class)
            k_bool = :"#{k}?"
            define_method(k_bool) { values[k] }
          end
        end
      end
    end

    def metaclass
      class << self; self; end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
casino_hub-0.5.0 lib/casino_hub/api_resource.rb
casino_hub-0.4.0 lib/casino_hub/api_resource.rb
casino_hub-0.3.0 lib/casino_hub/api_resource.rb
casino_hub-0.2.0 lib/casino_hub/api_resource.rb
casino_hub-0.1.6 lib/casino_hub/api_resource.rb
casino_hub-0.1.5 lib/casino_hub/api_resource.rb
casino_hub-0.1.4 lib/casino_hub/api_resource.rb
casino_hub-0.1.3 lib/casino_hub/api_resource.rb
casino_hub-0.1.2 lib/casino_hub/api_resource.rb
casino_hub-0.1.1 lib/casino_hub/api_resource.rb
casino_hub-0.1.0 lib/casino_hub/api_resource.rb