Sha256: 7125a6beee9a672c3d7499fa95f6d6a1129ca1a325b88baa407b4a7e4a7c441e

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ShotgunApiRuby
  class Entities
    class Schema
      def initialize(connection, type, base_url_prefix)
        @connection = connection.dup
        @type = type
        @connection.url_prefix = "#{base_url_prefix}/schema/#{type}"
      end
      attr_reader :type, :connection

      def read
        resp = @connection.get('')

        if resp.status >= 300
          raise "Error while read schema for #{type}: #{resp.body}"
        end

        resp_body = JSON.parse(resp.body)

        OpenStruct.new(resp_body['data'].transform_values { |v| v['value'] })
      end

      def fields
        resp = @connection.get('fields')
        resp_body = JSON.parse(resp.body)

        if resp.status >= 300
          raise "Error while read schema fields for #{type}: #{resp.body}"
        end

        OpenStruct.new(
          resp_body['data'].transform_values do |value|
            OpenStruct.new(
              value.transform_values { |attribute| attribute['value'] }.merge(
                properties:
                  OpenStruct.new(
                    value['properties'].transform_values do |prop|
                      prop['value']
                    end,
                  ),
              ),
            )
          end,
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shotgun_api_ruby-0.1.1 lib/shotgun_api_ruby/entities/schema.rb
shotgun_api_ruby-0.1.0.1 lib/shotgun_api_ruby/entities/schema.rb