Class: Cleon::PortDecor

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/cleon/port_decor.rb

Overview

The Cleon's service metadata decorator

Instance Method Summary collapse

Instance Method Details

#api_helper_methodObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cleon/port_decor.rb', line 85

def api_helper_method
  <<~EOF
    # helper for #{name}
    # #{parameters_string}
    post '#{api_helper_method_name}' do
      #{short_name}Port.(request.body.string, parameters).to_json
    rescue #{name.split('::').first}::Error => e
      {error: e.message}.to_json
    end
  EOF
end

#api_helper_method_nameObject



81
82
83
# File 'lib/cleon/port_decor.rb', line 81

def api_helper_method_name
  "/" + short_name.gsub(/([a-z\d])([A-Z])/, '\1-\2').downcase
end

#arguments_stringObject



24
25
26
27
28
29
30
31
32
# File 'lib/cleon/port_decor.rb', line 24

def arguments_string
  parameters.map{|(spec, name)|
    case spec
    when :req, :opt then "#{name}"
    when :key, :keyreq then "#{name}: #{name}"
    else '&block'
    end
  }.join(', ')
end

#lib_helper_methodObject



71
72
73
74
75
76
77
78
79
# File 'lib/cleon/port_decor.rb', line 71

def lib_helper_method
  <<~EOF
    # helper for #{name}
    def #{lib_helper_method_name}(#{parameters_string})
      #{parameters_sym_hash}
      #{short_name}.(**parameters)
    end
  EOF
end

#lib_helper_method_nameObject



67
68
69
# File 'lib/cleon/port_decor.rb', line 67

def lib_helper_method_name
  short_name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end

#parameters_str_hashObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cleon/port_decor.rb', line 49

def parameters_str_hash
  required = parameters
    .filter{|(spec, _)| spec == :req || spec == :keyreq}
    .map{|(spec, name)| "\"#{name}\" => #{name}" }.join(', ')
    .prepend('parameters = {').concat(?})

  optional = parameters
    .reject{|(spec, _)| spec == :req || spec == :keyreq}
    .map{|(spec, name)| "parameters[\"#{name}\"] = #{name} if #{name}"}
    .join(?\n)

  [required, optional].reject{|s| s.nil? || s.empty?}.join(?\n)
end

#parameters_stringObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cleon/port_decor.rb', line 12

def parameters_string
  parameters.map{|(spec, name)|
    case spec
    when :req then "#{name}"
    when :opt then "#{name} = nil"
    when :keyreq then "#{name}:"
    when :key then "#{name}: nil"
    else '&block'
    end
  }.join(', ')
end

#parameters_sym_hashObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cleon/port_decor.rb', line 34

def parameters_sym_hash
  required = parameters
    .filter{|(spec, _)| spec == :req || spec == :keyreq}
    .map{|(spec, name)| "#{name}: #{name}" }.join(', ')
    .prepend('parameters = {').concat(?})

  optional = parameters
    .reject{|(spec, _)| spec == :req || spec == :keyreq}
    .map{|(spec, name)| "parameters[:#{name}] = #{name} if #{name}"}
    .map{|s| "  #{s}"}
    .join(?\n)

  [required, optional].reject{|s| s.nil? || s.empty?}.join(?\n)
end

#short_nameObject



63
64
65
# File 'lib/cleon/port_decor.rb', line 63

def short_name
  name.split('::').last
end