Class: Sumomo::Stack::APIGenerator

Inherits:
Object
  • Object
show all
Extended by:
HasIrregularlyNamedFunctions
Defined in:
lib/sumomo/api.rb

Defined Under Namespace

Classes: CorsInfo

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasIrregularlyNamedFunctions

defi

Constructor Details

#initialize(pretty_print: false, &block) ⇒ APIGenerator



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sumomo/api.rb', line 29

def initialize(pretty_print: false, &block)
  @methods = {}
  @cors = CorsInfo.new
  @script = ''
  # defaults
  @cors.apply do
    AllowOrigin '*'
    AllowHeader 'origin'
    AllowHeader 'content-type'
    AllowHeader 'accept'
    AllowHeader 'cache-control'
    AllowHeader 'x-requested-with'
    AllowHeader 'if-modified-since'
  end
  @pretty_print = pretty_print
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sumomo/api.rb', line 47

def method_missing(name, *args, &block)
  name.to_s.split('_').each do |meth|
    valid_methods = %w[GET PUT POST PATCH DELETE HEAD OPTIONS]
    if valid_methods.include?(meth.to_s)
      path = args[0]
      @methods[path] = {} unless @methods.key?(path)

      @methods[path][meth] = { script: args.last, params: args.select { |arg| arg.is_a?(Symbol) && /\A[a-zA-Z\-0-9_]+\Z/.match(arg.to_s) } }
    else
      super
    end
  end
end

Class Method Details

.combine_modules(dest) ⇒ Object



73
74
75
76
77
78
# File 'lib/sumomo/api.rb', line 73

def self.combine_modules(dest)
  orig_modules = File.join(Gem.loaded_specs['sumomo'].full_gem_path, 'data', 'sumomo', 'api_modules', 'node_modules')

  `cp -Ra #{orig_modules}/ #{dest}/`
  `cp -Ra node_modules/* #{dest}/`
end

Instance Method Details

#generateObject



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sumomo/api.rb', line 80

def generate
  pretty_print = if @pretty_print
                   ', null, 2'
                 else
                   ''
                 end

  result = ''

  all_methods = @methods.clone

  # Generate appropriate options methods as well

  @methods.each do |path, resource|
    meths = resource.map { |meth, _info| meth }

    # Insert OPTIONS method if there isn't already one
    next if all_methods[path].key?('OPTIONS')

    all_methods[path]['OPTIONS'] = { script: "              var headers = {}\n              headers[\"Access-Control-Allow-Origin\"] = \#{@cors.allowed_origins.join(',').inspect}\n              headers[\"Access-Control-Request-Method\"] = '*'\n              headers[\"Access-Control-Allow-Methods\"] = '\#{meths.join(', ')}'\n              headers[\"Access-Control-Allow-Headers\"] = \#{@cors.allowed_headers.join(',').inspect}\n\n              respond_with({methods: \#{meths.inspect}}, 200, headers)\n    SCRIPT\n  end\n\n  all_methods.each do |path, resource|\n    resource.each do |method, method_info|\n      parameter_list = method_info[:params].join(', ')\n      parameter_call_list = method_info[:params].map { |parm| \"params['\#{parm}']\" }.join(', ')\n\n      result += <<-SCRIPT\n\n    router.\#{method.downcase}('\#{path}', prepare(function (event, callback) {\n\nvar retval = {};\n\nvar bodyParameters = parseQuery(event.body);\n\nvar params = merge(event.queryStringParameters || {}, event.pathParameters || {});\nparams = merge(params, bodyParameters);\n\nfunction respond_with(response_object, response_status, response_headers)\n{\n  var headers = {}\n  headers[\"Content-Type\"] = \"application/json; charset=utf-8\"\n  headers[\"Access-Control-Allow-Origin\"] = \#{@cors.allowed_origins.join(',').inspect}\n\n  if (response_headers)\n  {\n      for(var key in response_headers)\n      {\n          headers[key] = response_headers[key];\n      }\n  }\n\n  var response = {\n    statusCode: response_status || 200,\n    headers: headers,\n    body: JSON.stringify(response_object\#{pretty_print})\n  };\n\n  return callback(null, response);\n}\n\nvar retval = function (\#{parameter_list}) {\n  \#{method_info[:script]}\n}( \#{parameter_call_list} );\n\n    }));\n\n      SCRIPT\n    end\n  end\n  result\nend\n", params: [] }

#init_scriptObject



69
70
71
# File 'lib/sumomo/api.rb', line 69

def init_script
  @script
end