Sha256: f14970e13ee061bfd2dc1f85e77ab655e896ae4339037c558d7ea59734b6aabe

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module ShotgunApiRuby
  class Client
    attr_reader :connection

    def initialize(auth:, site_url: nil, shotgun_site: nil)
      raise "No site given" unless site_url || shotgun_site
      raise "auth param not valid" unless auth && Auth::Validator.valid?(auth)

      site_url ||= "https://#{shotgun_site}.shotgunstudio.com/api/v1"
      @connection =
        Faraday.new(url: site_url) do |faraday|
          faraday.use(ShotgunApiRuby::Auth, auth: auth, site_url: site_url)
          faraday.adapter Faraday.default_adapter
        end
    end

    def entities(type)
      public_send(type)
    end

    def respond_to_missing?(_name, _include_private = false)
      true
    end

    def method_missing(name, *args, &block)
      if args.empty?
        name = formated_name(name)
        define_singleton_method(name) do
          if entities_client = instance_variable_get("@#{name}")
            entities_client
          else
            entities_client = entities_aux(name)
            instance_variable_set("@#{name}", entities_client)
          end
        end
        send(name)
      else
        super
      end
    end

    private

    def formated_name(name)
      name.to_s.camelize.singularize
    end

    def entities_aux(type)
      type = formated_name(type)
      @entity_caller = Entities.new(connection, type)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shotgun_api_ruby-0.0.1 lib/shotgun_api_ruby/client.rb