Class: RightApi::Resources

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/right_api_client/resources.rb

Overview

This class defines the different resource types and the methods that one can call on them This class dynamically adds methods and properties to instances depending on what type of resource they are. This is a filler class so that we don’t always have to do an index before anything else This class gets instantiated when the user calls (for example) client.clouds … (ie. when you want the generic class: no id present)

Constant Summary

Constants included from Helper

Helper::INCONSISTENT_RESOURCE_TYPES, Helper::INSTANCE_FACING_RESOURCES, Helper::RESOURCE_SPECIAL_ACTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#add_id_and_params_to_path, #api_methods, #define_instance_method, #fix_array_of_hashes, #get_and_delete_href_from_links, #get_associated_resources, #get_href_from_links, #get_singular, #has_id, #insert_in_path, #is_singular?, #simple_singularize

Constructor Details

#initialize(client, path, resource_type) ⇒ Resources

Since this is just a filler class, only define instance methods and the method api_methods() Resource_type should always be plural. All parameters are treated as read only



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/right_api_client/resources.rb', line 18

def initialize(client, path, resource_type)
  @client = client
  @path   = path

  if INCONSISTENT_RESOURCE_TYPES.has_key?(get_singular(resource_type))
    resource_type = INCONSISTENT_RESOURCE_TYPES[get_singular(resource_type)] + 's'
  end
  @resource_type = resource_type
  # Add create methods for the relevant root RightApi::Resources
  self.define_instance_method('create') do |*args|
    client.send(:do_post, path, *args)
  end

  # Add in index methods for the relevant root RightApi::Resources
  self.define_instance_method('index') do |*args|
    # Session uses .index like a .show (so need to treat it as a special case)
    if resource_type == 'session'
      ResourceDetail.new(client, *client.send(:do_get, path, *args))
    else
      RightApi::Resource.process(client, *client.send(:do_get, path, *args))
    end
  end

  # Adding in special cases
  Helper::RESOURCE_SPECIAL_ACTIONS[resource_type].each do |meth, action|
    # Insert_in_path will NOT modify path
    action_path = insert_in_path(path, meth)
    self.define_instance_method(meth) do |*args|
      client.send(action, action_path, *args)
    end
  end if Helper::RESOURCE_SPECIAL_ACTIONS[resource_type]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Any other method other than standard actions (create, index) is simply appended to the href and called with a POST.



59
60
61
62
# File 'lib/right_api_client/resources.rb', line 59

def method_missing(m, *args)
  # note that 'href' method is not defined on this class; use 'path' instead.
  client.send(:do_post, [ path, m.to_s ].join('/'), *args)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/right_api_client/resources.rb', line 9

def client
  @client
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/right_api_client/resources.rb', line 9

def path
  @path
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



9
10
11
# File 'lib/right_api_client/resources.rb', line 9

def resource_type
  @resource_type
end

Instance Method Details

#inspectObject



10
11
12
13
# File 'lib/right_api_client/resources.rb', line 10

def inspect
  "#<#{self.class.name} " +
  "resource_type=\"#{@resource_type}\">"
end

#to_aryObject

Catch to_ary in cases like ‘puts @client.<resources>’ so it doesn’t go into method_missing



52
53
54
55
# File 'lib/right_api_client/resources.rb', line 52

def to_ary
  ["#<#{self.class.name} ", 
  "resource_type=\"#{@resource_type}\">"]
end