module ClubhouseRuby::PathBuilder

Public Class Methods

included(_) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 3
def self.included(_)
  class_exec do
    attr_accessor :path
  end
end

Public Instance Methods

clear_path() click to toggle source

You can build a path without executing in stages like this:

`foo.stories(story_id)`

This will partly populate foo:path, but won't execute the call (which clears it). In case you made a mistake and want to start again, you can clear the path using this public method.

# File lib/clubhouse_ruby/path_builder.rb, line 46
def clear_path
  self.path = []
end
method_missing(name, *args) click to toggle source

Uh oh! This will allow the class including this module to “build a path” by chaining calls to resources, terminated with a method linked to an action that will execute the api call.

For example:

`foo.stories(story_id).comments.update(id: comment_id, text: “comment text”)`

This example will execute a call to:

`api.clubhouse.io/api/v3/stories/{story-id}/comments/{comment-id}`

with arguments:

`{ text: "comment text" }`
Calls superclass method
# File lib/clubhouse_ruby/path_builder.rb, line 25
def method_missing(name, *args)
  if known_action?(name)
    execute_request(ACTIONS[name], args.first)
  elsif known_resource?(name)
    build_path(name, args.first)
  elsif known_exception?(name)
    build_path(EXCEPTIONS[name][:path], nil)
    execute_request(EXCEPTIONS[name][:action], args.first)
  else
    super
  end
end
respond_to_missing?(name, include_private = false) click to toggle source

We'd better not lie when asked.

Calls superclass method
# File lib/clubhouse_ruby/path_builder.rb, line 52
def respond_to_missing?(name, include_private = false)
  known_action?(name) ||
    known_resource?(name) ||
    known_exception?(name) ||
    super
end

Private Instance Methods

build_path(resource, id) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 83
def build_path(resource, id)
  self.path ||= []
  self.path << resource
  self.path << id if id
  self
end
execute_request(action, params) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 73
def execute_request(action, params)
  req = Request.new(
    self, 
    action: action,
    params: params
  )
  clear_path
  req.fetch
end
known_action?(name) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 61
def known_action?(name)
  ACTIONS.keys.include?(name)
end
known_exception?(name) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 69
def known_exception?(name)
  EXCEPTIONS.keys.include?(name)
end
known_resource?(name) click to toggle source
# File lib/clubhouse_ruby/path_builder.rb, line 65
def known_resource?(name)
  RESOURCES.include?(name)
end