Module: KongSchema::Resource::Api

Extended by:
Functional, Api
Included in:
Api
Defined in:
lib/kong_schema/resource/api.rb

Instance Method Summary collapse

Methods included from Functional

blank?, try

Instance Method Details

#allObject



22
23
24
# File 'lib/kong_schema/resource/api.rb', line 22

def all(*)
  Kong::Api.all
end

#changed?(record, attributes) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kong_schema/resource/api.rb', line 34

def changed?(record, attributes)
  current = record.attributes.keys.reduce({}) do |map, key|
    value = record.attributes[key]

    case key
    when 'hosts', 'methods', 'uris'
      map[key] = blank?(value) ? nil : Array(value)
    else
      map[key] = value
    end

    map
  end

  Adapter.for(Kong::Api).changed?(current, attributes)
end

#creatable?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/kong_schema/resource/api.rb', line 30

def creatable?(*)
  true
end

#create(attributes) ⇒ Object



26
27
28
# File 'lib/kong_schema/resource/api.rb', line 26

def create(attributes)
  Adapter.for(Kong::Api).create(serialize_outbound(attributes))
end

#delete(record) ⇒ Object



58
59
60
# File 'lib/kong_schema/resource/api.rb', line 58

def delete(record)
  Adapter.for(Kong::Api).delete(record)
end

#identify(record) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/kong_schema/resource/api.rb', line 13

def identify(record)
  case record
  when Kong::Api
    record.name
  when Hash
    record['name']
  end
end

#serialize_outbound(attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kong_schema/resource/api.rb', line 62

def serialize_outbound(attributes)
  attributes.keys.reduce({}) do |map, key|
    case key
    when 'hosts', 'methods', 'uris'
      map[key] = Array(attributes[key]).join(',')
    else
      map[key] = attributes[key]
    end

    map
  end
end

#update(record, partial_attributes) ⇒ Object



51
52
53
54
55
56
# File 'lib/kong_schema/resource/api.rb', line 51

def update(record, partial_attributes)
  Adapter.for(Kong::Api).update(
    record,
    serialize_outbound(partial_attributes)
  )
end