Module: ShortcutRuby::PathBuilder

Included in:
Shortcut
Defined in:
lib/shortcut_ruby/path_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

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.app.shortcut.com/api/v3/stories/story-id/comments/comment-id`

with arguments:

`{ text: "comment text" }`


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shortcut_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

Class Method Details

.included(_) ⇒ Object



3
4
5
6
7
# File 'lib/shortcut_ruby/path_builder.rb', line 3

def self.included(_)
  class_exec do
    attr_accessor :path
  end
end

Instance Method Details

#clear_pathObject

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.



46
47
48
# File 'lib/shortcut_ruby/path_builder.rb', line 46

def clear_path
  self.path = []
end

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

We’d better not lie when asked.

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/shortcut_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