Sha256: 398ca4c314638afb872fe7941c46124663d4e0cd1a471907707e74dd358167b3

Contents?: true

Size: 959 Bytes

Versions: 12

Compression:

Stored size: 959 Bytes

Contents

module Jess
  # Wraps a JSON object that is returned from the JSS API. The underlying raw
  # JSON is available via `_json`. Properties of the JSON can be accessed via
  # `method_missing`, so that a Resource behaves like a typical Ruby object.
  # Accessing a non-existent JSON property will raise `NoMethodError`.
  #
  class Resource
    attr_reader :_json

    def initialize(json)
      @_json = json.freeze
    end

    def to_hash
      _json
    end

    private

    def method_missing(symbol, *args)
      if _json.key?(symbol.to_s)
        _as_resource(_json.public_send(:[], symbol.to_s, *args))
      else
        super
      end
    end

    def respond_to_missing?(symbol, include_all)
      super || _json.key?(symbol.to_s)
    end

    def _as_resource(json)
      case json
      when Hash
        Resource.new(json)
      when Array
        json.map { |j| _as_resource(j) }.freeze
      else
        json.freeze
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jess-1.4.1 lib/jess/resource.rb
jess-1.4.0 lib/jess/resource.rb
jess-1.3.2 lib/jess/resource.rb
jess-1.3.1 lib/jess/resource.rb
jess-1.3.0 lib/jess/resource.rb
jess-1.2.5 lib/jess/resource.rb
jess-1.2.4 lib/jess/resource.rb
jess-1.2.3 lib/jess/resource.rb
jess-1.2.2 lib/jess/resource.rb
jess-1.2.1 lib/jess/resource.rb
jess-1.2.0 lib/jess/resource.rb
jess-1.1.3 lib/jess/resource.rb