Module: Trample::Swagger

Defined in:
lib/trample/swagger.rb

Constant Summary collapse

CONDITION_OPTION_WHITELIST =
[
  :single,
  :range,
  :prefix,
  :autocomplete,
  :search_analyzed,
  :not,
  :and,
  :any_text
]

Instance Method Summary collapse

Instance Method Details

#trample_swagger(search_class, path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trample/swagger.rb', line 70

def trample_swagger(search_class, path)
  swagger_path "#{path}/new" do
    operation :get do
      key :description, "Instantiate default search. See the corresponding PUT operation for valid inputs."
      key :tags, ['search']

      response 200 do
        key :description, 'Trample response'
        schema do
          key :'$ref', :TrampleSearchResponse
        end
      end
    end
  end

  swagger_path "#{path}/{id}" do
    operation :put do
      description = "<p>Trample search <a target='_blank' href='http://richmolj.github.io/trample'>View Full Trample Documentation</a></p><p><strong>Conditions:</strong></p><ul>"
      search_class._conditions.each_pair do |name, condition|
        attrs = condition.attributes.select { |k,v| !!v }.map { |k,v| k }
        attrs.select! { |a| CONDITION_OPTION_WHITELIST.include?(a) }
        attrs = attrs.present? ? "(#{attrs.join(', ')})" : ''
        description << "<li>#{name} #{attrs}</li>"
      end
      description << "</ul>"

      if search_class._aggs.present?
        description << "<p><strong>Aggregations:</strong></p><ul>"
        search_class._aggs.each_pair do |name, agg|
          description << "<li>#{name}</li>"
        end
        description << "</ul>"
      end

      key :description, description
      key :tags, ['search']

      parameter paramType: :path do
        key :name, :id
        key :type, :integer
        key :default, SecureRandom.uuid
      end

      parameter do
        key :name, :data
        key :in, :body

        schema do
          key :'$ref', :TrampleSearch
        end
      end

      response 200 do
        key :description, 'Trample response'
        schema do
          key :'$ref', :TrampleSearchResponse
        end
      end
    end
  end
end

#trample_swagger_schemaObject



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/trample/swagger.rb', line 18

def trample_swagger_schema
  swagger_schema :TrampleSearch do
    property :data do
      key :type, :object

      property :attributes do
        key :type, :object

        property :conditions do
          key :type, :object
        end

        property :metadata do
          key :type, :object

          property :pagination do
            key :type, :object
            key :example, {current_page: 1, per_page: 20}
          end
        end

        property :aggregations do
          key :type, :array
          key :example, []

          items do
            key :type, :object
          end
        end
      end
    end
  end

  swagger_schema :TrampleSearchResponse do
    allOf do
      schema do
        key :'$ref', :TrampleSearch
      end

      schema do
        property :results do
          key :type, :array

          items do
            key :type, :object
          end
        end
      end
    end
  end
end