Module: Agave::ApiClient::ClassMethods

Defined in:
lib/agave/api_client.rb

Instance Method Summary collapse

Instance Method Details

#json_schema(subdomain) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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