Module: Kong::Base::ClassMethods

Defined in:
lib/kong/base.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kong/base.rb', line 32

def method_missing(method, *arguments, &block)
  if method.to_s.start_with?('find_by_')
    attribute = method.to_s.sub('find_by_', '')
    if self.attribute_names.include?(attribute)
      self.list({ attribute => arguments[0] })[0]
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#create(attributes = {}) ⇒ Object

Create resource

Parameters:

  • attributes (Hash) (defaults to: {})


22
23
24
# File 'lib/kong/base.rb', line 22

def create(attributes = {})
  self.new(attributes).create
end

#find(id) ⇒ Object

Find resource

Parameters:

  • id (String)


28
29
30
# File 'lib/kong/base.rb', line 28

def find(id)
  self.new.get(id)
end

#list(params = {}) ⇒ Array Also known as: all

List resources

Returns:

  • (Array)


7
8
9
10
11
12
13
14
15
16
# File 'lib/kong/base.rb', line 7

def list(params = {})
  result = []
  json_data = Client.instance.get(self::API_END_POINT, params)
  if json_data['data']
    json_data['data'].each do |instance|
      result << self.new(instance)
    end
  end
  result
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kong/base.rb', line 45

def respond_to?(method, include_private = false)
  if method.to_s.start_with?('find_by_')
    attribute = method.to_s.sub('find_by_', '')
    if self.attribute_names.include?(attribute)
      return true
    else
      super
    end
  else
    super
  end
end