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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

  normal_attributes = attributes.keys.reduce({}) do |map, key|
    value = attributes[key]

    case key
    # sometimes the API reports it an array, sometimes a string, sometimes
    # nil...
    when 'methods'
      map[key] = Array(value).join(',').split(',')
    else
      map[key] = value
    end

    map
  end

  Adapter.for(Kong::Api).changed?(current, normal_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



73
74
75
# File 'lib/kong_schema/resource/api.rb', line 73

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kong_schema/resource/api.rb', line 77

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



66
67
68
69
70
71
# File 'lib/kong_schema/resource/api.rb', line 66

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