TemplateFunctions.template_api_key_func()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
dl 0
loc 32
rs 8.8571
1
module Rapis
2
  module TemplateFunctions
3
    def template_params
4
      %w(query header path formData body)
5
    end
6
7
    def template_include_func
8
      <<-EOS
9
      def _include(path, args = {})
10
        instance_eval(File.read(path))
11
      end
12
      EOS
13
    end
14
15
    def template_params_func
16
      funcs = <<-EOS
17
      def _param(name, i, block)
18
        @__hash__['parameters'] << {
19
          'name' => name,
20
          'in' => i
21
        }.merge(Dslh::ScopeBlock.nest(binding, 'block', name))
22
      end
23
      EOS
24
      template_params.each do |i|
25
        funcs << <<-EOS
26
          def #{i}(name, value = nil, &block)
27
            _param(name, #{i.inspect}, block)
28
          end
29
        EOS
30
      end
31
      <<-EOS
32
      def parameters(value = nil, &block)
33
        if value.nil?
34
          @__hash__['parameters'] = []
35
36
          #{funcs}
37
38
          if block
39
            value = instance_eval(&block)
40
          end
41
        else
42
          @__hash__['parameters'] = value
43
        end
44
      end
45
      EOS
46
    end
47
48
    def template_api_key_func
49
      funcs = <<-EOS
50
      def _api_key(name, i, block)
51
        @__hash__['api_key'] = Dslh::ScopeBlock.nest(binding, 'block', name).merge({
52
          'name' => name,
53
          'in' => i
54
        })
55
      end
56
      EOS
57
      template_params.each do |i|
58
        funcs << <<-EOS
59
          def #{i}(name, value = nil, &block)
60
            _api_key(name, #{i.inspect}, block)
61
          end
62
        EOS
63
      end
64
      <<-EOS
65
      def api_key(value = nil, &block)
66
        if value.nil?
67
          @__hash__['api_key'] = []
68
69
          #{funcs}
70
71
          if block
72
            value = instance_eval(&block)
73
          end
74
        else
75
          @__hash__['api_key'] = value
76
        end
77
      end
78
      EOS
79
    end
80
81
    def template_item_func
82
      <<-EOS
83
      def item(key, value = nil, &block)
84
        if block
85
          value = Dslh::ScopeBlock.nest(binding, 'block', key)
86
        end
87
        @__hash__[key] = value
88
      end
89
      EOS
90
    end
91
92
    def template_code_func
93
      <<-EOS
94
      def code(key, value = nil, &block)
95
        if block
96
          value = Dslh::ScopeBlock.nest(binding, 'block', key)
97
        end
98
        @__hash__[key] = value
99
      end
100
      EOS
101
    end
102
  end
103
end
104