class Sorry::Api::Client

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

Attributes

access_token[RW]

Attributes.

Public Class Methods

new(access_token: nil) click to toggle source

Initialize the API client.

# 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

Public Instance Methods

method_missing(method, *args) click to toggle source

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

# 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
path() click to toggle source

 Get the entire path.

# File lib/sorry/api/client.rb, line 48
def path
        # Combine the parts into a single string.
        @path_parts.join('/')
end
reset_path() click to toggle source

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

# File lib/sorry/api/client.rb, line 70
def reset_path; @path_parts = []; end