Class: Sorry::Api::Client

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

Overview

The core API Client class, instantiated using the access token from your account. All requests stem from here.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil) ⇒ Client

Initialize the API client.



12
13
14
15
16
17
18
19
20
21
# File 'lib/sorry/api/client.rb', line 12

def initialize(access_token: nil)
  # Define the API Key, fallback to class config and/or environment variables.
  @access_token = access_token || ENV['SORRY_ACCESS_TOKEN']

  # TODO: Validate that it was able to intialize with an API key.

  # Create an empty array to store the path
  # parts for the recursive request building.
  @path_parts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Use the missing methods to build the path using the method name as the part in the request path.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sorry/api/client.rb', line 56

def method_missing(method, *args)
        # To support underscores, we replace them with hyphens when calling the API
        @path_parts << method.to_s.gsub("_", "-").downcase
        # Append any arguments to the path.
        @path_parts << args if args.length > 0
        # Flatter the parts to remove any duplcuates.
        @path_parts.flatten!
        # Return an instance of self
        # so we can chain methods together.
        self
end

Instance Attribute Details

#access_tokenObject

Attributes.



9
10
11
# File 'lib/sorry/api/client.rb', line 9

def access_token
  @access_token
end

Instance Method Details

#pathObject

 Get the entire path.



48
49
50
51
# File 'lib/sorry/api/client.rb', line 48

def path
  # Combine the parts into a single string.
  @path_parts.join('/')
end

#reset_pathObject

Reset the path parts once a CRUD method has been called.



70
# File 'lib/sorry/api/client.rb', line 70

def reset_path; @path_parts = []; end