Class: MOCO::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/moco/client.rb

Overview

Main client class for interacting with the MOCO API Provides dynamic access to all API endpoints through method_missing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain:, api_key:, debug: false) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/moco/client.rb', line 9

def initialize(subdomain:, api_key:, debug: false)
  @connection = Connection.new(self, subdomain, api_key, debug: debug)
  @collections = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Dynamically handle entity collection access (e.g., client.projects)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moco/client.rb', line 15

def method_missing(name, *args, &)
  # Check if the method name corresponds to a known plural entity type
  if collection_name?(name)
    # Return a CollectionProxy directly for chainable queries
    # Cache it so subsequent calls return the same proxy instance
    @collections[name] ||= CollectionProxy.new(
      self,
      name.to_s, # Pass the plural name (e.g., "projects") as the path hint
      ActiveSupport::Inflector.classify(name.to_s) # Get class name (e.g., "Project")
    )
  else
    # Delegate to superclass for non-collection methods
    super
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/moco/client.rb', line 7

def connection
  @connection
end

Instance Method Details

#collection_name?(name) ⇒ Boolean

Check if the method name looks like a collection name (plural)

Returns:

  • (Boolean)


36
37
38
# File 'lib/moco/client.rb', line 36

def collection_name?(name)
  name.to_s == ActiveSupport::Inflector.pluralize(name.to_s)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/moco/client.rb', line 31

def respond_to_missing?(name, include_private = false)
  collection_name?(name) || super
end