Class: ShotgridApiRuby::Client

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/shotgrid_api_ruby/client.rb

Overview

Main class for connection.

This should be only instanciated once to re-use tokens

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth:, site_url: nil, shotgun_site: nil, shotgrid_site: nil) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
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
# File 'lib/shotgrid_api_ruby/client.rb', line 19

def initialize(auth:, site_url: nil, shotgun_site: nil, shotgrid_site: nil)
  raise 'No site given' unless site_url || shotgun_site || shotgrid_site
  if !Auth::Validator.valid?(
       client_id: auth[:client_id],
       client_secret: auth[:client_secret],
       username: auth[:username],
       password: auth[:password],
       session_token: auth[:session_token],
       refresh_token: auth[:refresh_token],
     )
    raise 'auth param not valid'
  end

  site_url ||=
    if shotgun_site
      "https://#{shotgun_site}.shotgunstudio.com/api/v1"
    elsif shotgrid_site
      "https://#{shotgrid_site}.shotgrid.autodesk.com/api/v1"
    end

  @connection =
    T.let(
      Faraday.new(url: site_url) do |faraday|
        faraday.use(ShotgridApiRuby::Auth, auth: auth, site_url: site_url)
        faraday.adapter Faraday.default_adapter
      end,
      Faraday::Connection,
    )
  @preferences = T.let(nil, T.nilable(Preferences))
  @server_info = T.let(nil, T.nilable(ServerInfo))
  @entity_caller = T.let(nil, T.nilable(Entities))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/shotgrid_api_ruby/client.rb', line 89

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



54
55
56
# File 'lib/shotgrid_api_ruby/client.rb', line 54

def connection
  @connection
end

Instance Method Details

#entities(type) ⇒ Object



70
71
72
# File 'lib/shotgrid_api_ruby/client.rb', line 70

def entities(type)
  public_send(type)
end

#preferencesObject



58
59
60
# File 'lib/shotgrid_api_ruby/client.rb', line 58

def preferences
  @preferences = Preferences.new(connection)
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/shotgrid_api_ruby/client.rb', line 77

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

#server_infoObject



64
65
66
# File 'lib/shotgrid_api_ruby/client.rb', line 64

def server_info
  @server_info || ServerInfo.new(connection)
end