Sha256: a3a434003fe304437d38bf5a5a9323b814e010c9d909f4a3ad8cc67e2e94ada5

Contents?: true

Size: 828 Bytes

Versions: 1

Compression:

Stored size: 828 Bytes

Contents

module Velocity
  class Base < OpenStruct
    class NotFoundError < StandardError; end

    class << self
      attr_reader :resource_class_name

      def resource_class(class_name = nil)
        @resource_class_name ||= class_name
      end
    end

    def initialize(args)
      super(args.deep_transform_keys {|key| key.to_s.underscore })
    end

    def self.create(args)
      data = resource_class.new(args).create
      new(data["attributes"].merge(id: data["id"]))
    end

    def self.where(args)
      resource_class.new(args).fetch.map do |data|
        new(data["attributes"].merge(id: data["id"]))
      end
    end

    def self.find_by(args)
      where(args).first
    end

    def self.find_by!(args)
      where(args).first or raise NotFoundError, "record not found with #{args.inspect}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
velocity_client_ruby-0.2.1 lib/velocity/base.rb