module Agave::ApiClient::ClassMethods

Public Instance Methods

json_schema(subdomain) click to toggle source
# File lib/agave/api_client.rb, line 26
def json_schema(subdomain)
  define_method(:initialize) do |options = {}|
    @token = ENV['AGAVE_API_TOKEN']
    @base_url = options[:base_url] || ENV["AGAVECMS_BASE_URL"]
    @extra_headers = options[:extra_headers] || {}
  end

  response = Faraday.get(
    "#{ENV["AGAVECMS_BASE_URL"]}/docs/site-api-hyperschema.json"
  )

  schema = JsonSchema.parse!(JSON.parse(response.body))
  schema.expand_references!

  schema.definitions.each do |type, schema|
    is_collection = schema.links.select{|x| x.rel === "instances"}.any?
    namespace = is_collection ? type.pluralize : type

    define_method(namespace) do
      instance_variable_set(
        "@#{namespace}",
        instance_variable_get("@#{namespace}") ||
        Agave::Repo.new(self, type, schema)
      )
    end
  end
end