appril 0.0.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bbd7e7cfc93a83e1a2b21f54f9ca1f6ff982188b
4
- data.tar.gz: 29c6281c4353748e7ae403445e780e143c2e0ab7
3
+ metadata.gz: 9fac08ec14ea8a2fc574565add3a62e956ffe315
4
+ data.tar.gz: c41b91b89bd6a9f81c95c0cb9cb54da5e1d2e8ad
5
5
  SHA512:
6
- metadata.gz: 47596d9940d92dcd4c29c75320239e867b1625b08108d8e70419abcef8813e013f03504c4311525f517ca38748e5dd2622cea762688d2ab100f6a97c0a2e9e25
7
- data.tar.gz: c6f28e28ff524b7ecf0afe6563a35d6e83c37d5301d3b23750b0efaafb29b1bc0e1b154a55f7e89a5c2b58612ebb6172649c02c9aa0dd86d3667be82e7316371
6
+ metadata.gz: f9db9bd50c5909293335a56efce0ca3de492ef6ec8dcbc8228d2d4276f418b5d5535f99fdae83ef7aaad64e73fc6bccad48bf8d121510c01408bdab742b8d022
7
+ data.tar.gz: 43ceccf951f7e3df393b6cbadc0eff0eb8ef15884a0788bdfa0d025cf7e437b0befd606efad79dce49a3711030ad91b3c8e3efc204ac28be1a09a4607dd21269
@@ -0,0 +1,97 @@
1
+ require 'fileutils'
2
+ require 'rocketio'
3
+ require 'appril/load_configs'
4
+
5
+ module Appril
6
+ extend self
7
+
8
+ def generate_configs dir
9
+ generate_components_file(dir)
10
+ generate_config_file(dir)
11
+ generate_store_modules_file(dir)
12
+ end
13
+
14
+ def generate_components_file dir
15
+ components = JSON.pretty_generate(controllers_map(dir))
16
+ File.open(File.expand_path('components.json', dir), 'w') {|f| f << components}
17
+ end
18
+
19
+ def generate_config_file dir
20
+ config = Appril.load_configs("#{dir}/config", env: :development)
21
+ File.open File.expand_path('config.js', dir), 'w' do |f|
22
+ f << "// auto-generated file, do not edit\n"
23
+ f << "module.exports = " << JSON.pretty_generate({
24
+ build_path: config[:build_path],
25
+ client_url: config[:client_url],
26
+ server_url: config[:server_url],
27
+ default_api: config[:default_api],
28
+ webpack_entries: webpack_entries(dir, controllers_map(dir))
29
+ })
30
+ end
31
+ end
32
+
33
+ def generate_store_modules_file dir
34
+ lines = controllers_map(dir).each_with_object([
35
+ '// auto-generated file, do not edit',
36
+ 'const modules = {}'
37
+ ]) do |c,o|
38
+ next unless c[:name] # skip anonymous controllers
39
+ pattern = "base/components/#{c[:path]}/store.*"
40
+ next unless file = Dir[File.expand_path(pattern, dir)].find {|e| File.file?(e)}
41
+ path = file.sub(dir, 'app')
42
+ o << "
43
+ import #{c[:name]} from '#{path}'
44
+ modules.#{c[:name]} = #{c[:name]}
45
+ "
46
+ end
47
+ lines.push('export default modules')
48
+ file = File.expand_path('store_modules.js', dir)
49
+ FileUtils.mkdir_p(File.dirname(file))
50
+ File.open(file, 'w') {|f| f << lines.join("\n")}
51
+ end
52
+
53
+ def controllers_map dir
54
+ path_prefix = File.expand_path('base/components', dir)
55
+ RocketIO.controllers.each_with_object([]) do |controller,o|
56
+ next if controller.to_s =~ /BaseController|RTCPController/
57
+ o << {
58
+ path: controller.dirname.sub(path_prefix, '').gsub(/\A\/|\/\Z/, ''),
59
+ url: controller.url,
60
+ url_pattern: url_pattern(controller),
61
+ name: controller.name.gsub('::', '__'),
62
+ controller_name: controller.name,
63
+ api: controller.api.keys
64
+ }
65
+ end.sort do |a,b|
66
+ b[:url].split('/').size <=> a[:url].split('/').size
67
+ end
68
+ end
69
+
70
+ def webpack_entries dir, controllers
71
+ entries = controllers.each_with_object({}) do |controller,o|
72
+ pattern = "base/components/#{controller[:path]}/index.*"
73
+ next unless entry = Dir[File.expand_path(pattern, dir)].find {|e| File.file?(e)}
74
+ o[controller[:path]] = entry.sub(dir, '.')
75
+ end
76
+
77
+ pattern = "base/core.*"
78
+ if core = Dir[File.expand_path(pattern, dir)].find {|e| File.file?(e)}
79
+ entries[:core] = core.sub(dir, '.')
80
+ end
81
+
82
+ entries
83
+ end
84
+
85
+ def url_pattern controller
86
+ controller.url *controller.instance_method(:index).parameters.each_with_object([]) {|param,o|
87
+ pattern = if param[0] == :rest
88
+ "*"
89
+ elsif param[0] == :req
90
+ ":#{param[1]}"
91
+ elsif param[0] == :opt
92
+ ":#{param[1]}?"
93
+ end
94
+ o << pattern if pattern
95
+ }
96
+ end
97
+ end
@@ -0,0 +1,60 @@
1
+ require 'yaml'
2
+ require 'rocketio'
3
+
4
+ module Appril
5
+ extend self
6
+
7
+ def load_configs *dirs, env: RocketIO.environment
8
+
9
+ config = RocketIO.indifferent_params({environment: env.to_s.freeze})
10
+
11
+ dirs.each do |dir|
12
+ config.update(load_config_file("#{dir}/config.yml"))
13
+ config.update(load_config_file("#{dir}/env/#{env}.yml"))
14
+
15
+ loaded = [
16
+ File.expand_path("config.yml", dir),
17
+ File.expand_path("env/#{env}.yml", dir)
18
+ ]
19
+
20
+ %w[
21
+ *.yml
22
+ **/*.yml
23
+ ].each do |pattern|
24
+ Dir[File.join(dir, pattern)].each do |file|
25
+
26
+ path = File.expand_path(file, dir)
27
+ next if loaded.include?(path)
28
+ loaded << path
29
+
30
+ key = File.basename(file, '.yml')
31
+ key_config = load_config_file(file)
32
+ key_config_keys = key_config.keys.map(&:to_s)
33
+
34
+ config[key] = if key_config_keys.include?(config[:environment])
35
+ # current environment found, use it
36
+ key_config[config[:environment]]
37
+ else
38
+ if RocketIO::ENVIRONMENTS.keys.find {|k| key_config_keys.include?(k)}
39
+ # there are some environment(s), but no current one so set current environment to nil
40
+ nil
41
+ else
42
+ # there are no environments, so this config is available on any environment
43
+ key_config
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def config.method_missing key
51
+ self[key]
52
+ end
53
+
54
+ config
55
+ end
56
+
57
+ def load_config_file file
58
+ RocketIO.indifferent_params(YAML.load(File.read(file)) || {})
59
+ end
60
+ end
@@ -5,8 +5,6 @@ module Appril
5
5
  def index
6
6
  return unless websocket?
7
7
 
8
- @router = RocketIO::Router.new(*RocketIO.controllers)
9
-
10
8
  @socket = Tubesock.hijack(env)
11
9
  @socket.onopen(&method(:on_open))
12
10
  @socket.onmessage(&method(:on_message))
@@ -20,7 +18,7 @@ module Appril
20
18
 
21
19
  def on_open
22
20
  connected
23
- write(serial: 0, data: initialization_data)
21
+ write(serial: 0, data: _env)
24
22
  end
25
23
 
26
24
  def on_message msg
@@ -32,17 +30,20 @@ module Appril
32
30
  controller,
33
31
  msg[:method],
34
32
  msg[:arguments],
35
- env.merge(rtcp_serial: msg[:serial]).update(rtcp_env)
33
+ env.merge(RocketIO::REQUEST_METHOD => msg[:request_method], rtcp_serial: msg[:serial], **controller_env)
36
34
  )
37
35
 
38
- if status == 200
36
+ if status.to_i / 100 == 2
39
37
 
40
38
  if body.is_a?(Proc)
41
39
  body.call
42
40
  return
43
41
  end
44
42
 
45
- write(serial: msg[:serial], data: body) if msg[:reply]
43
+ if msg[:reply]
44
+ write(serial: msg[:serial], data: body)
45
+ end
46
+
46
47
  return
47
48
  end
48
49
  else
@@ -69,16 +70,17 @@ module Appril
69
70
 
70
71
  def on_close
71
72
  @socket.close! if @socket
72
- @socket, @user, @router = nil
73
+ @socket = nil
73
74
  disconnected
74
75
  end
75
76
 
76
- def resolve_controller url
77
- @router.resolve_path(url)[0]
77
+ def resolve_controller controller
78
+ ::RocketIO.controllers.find {|c| c.name == controller}
78
79
  end
79
80
 
80
81
  def call_controller controller, method, arguments, env
81
- controller.new(method.to_sym, arguments).call(env)
82
+ params = arguments.last.is_a?(Hash) ? arguments.pop : {}
83
+ controller.new(method, arguments).call(env, params: params)
82
84
  end
83
85
 
84
86
  def write data
@@ -89,13 +91,13 @@ module Appril
89
91
  def connected
90
92
  end
91
93
 
92
- # data sent to client after connection established
93
- def initialization_data
94
+ # environmental data to be sent to client at App initialization
95
+ def _env
94
96
  {}
95
97
  end
96
98
 
97
- # merged into original env when calling a controller
98
- def rtcp_env
99
+ # merged into original env when calling a controller method
100
+ def controller_env
99
101
  {}
100
102
  end
101
103
 
@@ -1,3 +1,3 @@
1
1
  module Appril
2
- VERSION = '0.0.8'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appril
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slee Woo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-05 00:00:00.000000000 Z
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rocketio
@@ -51,6 +51,8 @@ files:
51
51
  - appril.gemspec
52
52
  - lib/appril.rb
53
53
  - lib/appril/base_controller.rb
54
+ - lib/appril/generate_configs.rb
55
+ - lib/appril/load_configs.rb
54
56
  - lib/appril/rtcp_controller.rb
55
57
  - lib/appril/version.rb
56
58
  homepage: https://github.com/appril/ruby-server
@@ -76,5 +78,5 @@ rubyforge_project:
76
78
  rubygems_version: 2.5.1
77
79
  signing_key:
78
80
  specification_version: 4
79
- summary: '["appril-0.0.8", "Opinionated framework for building opinionated web applications"]'
81
+ summary: '["appril-0.2.0", "Opinionated framework for building opinionated web applications"]'
80
82
  test_files: []