softwear-lib 1.9.4 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76a6c901c0fdabb3e406471cd1f56d9fa663c4c3
4
- data.tar.gz: efefa401b769ba1f484a4a6538ac74cdfc288ac2
3
+ metadata.gz: f4ffc6852fd827bfa7f32f9db01d2b19e73cd5e1
4
+ data.tar.gz: 9e869b8b7166141a64c1f261800b45e184e4f478
5
5
  SHA512:
6
- metadata.gz: 8023d0eb9c4eb1b4a0bbc9b31dd691deb45f21bdf38aea33892a07209cc881185507ffb326a82232a13cd32d4cc012f3316a17abc09aa52f64764e0f60d0add3
7
- data.tar.gz: a007ebf93851b2f5a483678ab5120bbd57eca4f2f2ae007439764417ff2b81a300449087c72e71e2d60281f5a8689da591c5eaf6983936516924f2d9d1c78ab0
6
+ metadata.gz: fb32f5c83861fccdee7594c31ffa62553dcadb5e380502c7f405ddaba0b6e5b34d92e10bf9631e5f10e38c1fae31f78904674781bf33ccba111cdb4f67e0dea7
7
+ data.tar.gz: 116fedc2e671d956da3a8bed15b5925c53ce26cb8a62095996fd7e1c77e11c468b60b0fcb33a5f3d628e3e56a3d81c7fff223037751fde1ffd63a5e61995457d
@@ -394,8 +394,8 @@ module Softwear
394
394
  # Given an invalid signin token:
395
395
  # Returns false
396
396
  # ====================
397
- def auth(token)
398
- response = validate_response query "auth #{Figaro.env.hub_app_name} #{token}"
397
+ def auth(token, app_name = nil)
398
+ response = validate_response query "auth #{app_name || Figaro.env.hub_app_name} #{token}"
399
399
 
400
400
  return false unless response =~ /^yes .+$/
401
401
 
@@ -65,6 +65,14 @@ users:
65
65
  @users_yml
66
66
  end
67
67
 
68
+ #
69
+ # Transforms
70
+ # ['email@email.com', { 'attr1' => 'val1', 'attr2' => 'val2' }]
71
+ # From the yml format
72
+ #
73
+ # Into
74
+ # { 'email' => 'email@email.com', 'attr1' => 'val1', 'attr2' => 'val2' }
75
+ #
68
76
  def yml_entry(entry, id_if_default = nil)
69
77
  attributes = {}.with_indifferent_access
70
78
 
@@ -95,7 +103,7 @@ users:
95
103
  all.select { |u| !u.roles.nil? && roles.any? { |r| u.roles.include?(r) } }
96
104
  end
97
105
 
98
- def auth(_token)
106
+ def auth(_token, _appname = nil)
99
107
  signed_in = users_yml[:signed_in]
100
108
 
101
109
  yml_entry [signed_in, users_yml[:users][signed_in]] unless signed_in.blank?
@@ -0,0 +1,107 @@
1
+ def split(string, limit = nil)
2
+ string.split(/\s+/, limit)
3
+ end
4
+
5
+ def report_error(client, whole_command, error)
6
+ $stderr.puts "=== ERROR WHILE PROCESSING THE COMMAND \"#{whole_command}\" ===\n"\
7
+ "#{error.class.name}: #{error.message}\n#{error.backtrace.join("\n")}"
8
+ client.puts "sorry"
9
+ end
10
+
11
+ def dev_log(*a)
12
+ $stdout.puts(*a) if Rails.env.development?
13
+ end
14
+
15
+ def log(*a)
16
+ $stdout.puts(*a) unless Rails.env.test?
17
+ end
18
+
19
+ # TODO for production, we want to use the SSL server instead of straight up TCP.
20
+
21
+ # ==== Send Format: =======
22
+ # One line strings: "#{command} #{arg1} #{arg2} #{etc}"
23
+ # The last argument, depending on the command, may contain spaces (but usually does not need to)
24
+ # =========================
25
+ # === Receive Format: =====
26
+ # Usually one string, like "yes", or "no".
27
+ # Returns "denied" if an unauthorized command was attempted.
28
+ # Returns "invalid" if an invalid command was attempted.
29
+ # Returns "sorry" if an error was raised while processing the command.
30
+ # Can be a json argument, often following "yes ".
31
+ # =========================
32
+ def address_of(socket)
33
+ _family, port, name, host = socket.addr
34
+ if host == name
35
+ "#{host}:#{port}"
36
+ else
37
+ "#{name}(#{host}):#{port}"
38
+ end
39
+ end
40
+
41
+ def start_server!(*args)
42
+ log "Connecting...!"
43
+
44
+ if args.size > 1
45
+ port = args.first
46
+ else
47
+ port = ENV['port'] || ENV['PORT'] || 2900
48
+ end
49
+
50
+ server = TCPServer.new port
51
+ log "Ready! Using \"#{ActiveRecord::Base.connection.current_database}\" database"
52
+
53
+ client_count = 0
54
+ commands = args.last
55
+
56
+ loop do
57
+ Thread.start(server.accept) do |client|
58
+ client_count += 1
59
+ dev_log "New client! ##{client_count} #{address_of client}"
60
+
61
+ if Rails.env.development?
62
+ response_logger = Module.new do
63
+ def puts(s)
64
+ $stdout.puts "==> #{s}"
65
+ super
66
+ end
67
+ end
68
+ client.singleton_class.send :include, response_logger
69
+ end
70
+
71
+ while line_in = client.gets.chomp
72
+ log "Processing \"#{line_in}\"" if Rails.env.test?
73
+
74
+ command, rest_of_command = split(line_in, 2)
75
+
76
+ before = Time.now
77
+ begin
78
+ command = commands[command.downcase.to_sym]
79
+
80
+ if command.nil?
81
+ log "SOMEONE attempted invalid command: \"#{line_in}\""
82
+ else
83
+ dev_log "<== #{line_in}"
84
+ ActiveRecord::Base.connection_pool.with_connection do
85
+ command.call(client, rest_of_command)
86
+ end
87
+ end
88
+
89
+ rescue StandardError => e
90
+ report_error(client, line_in, e)
91
+ rescue Exception => e
92
+ report_error(client, line_in, e)
93
+ break
94
+ end
95
+ after = Time.now
96
+
97
+ ms = (after - before) * 1000
98
+ dev_log %((#{'%.2f' % ms}ms) from #{address_of(client)})
99
+ dev_log ""
100
+ end
101
+
102
+ client_count -= 1
103
+ client.close rescue nil
104
+ dev_log "Client disconnected. #{address_of client}"
105
+ end
106
+ end
107
+ end
@@ -1,5 +1,5 @@
1
1
  module Softwear
2
2
  module Lib
3
- VERSION = "1.9.4"
3
+ VERSION = "1.10.1"
4
4
  end
5
5
  end
data/softwear-lib.gemspec CHANGED
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.2.0"
24
24
 
25
- spec.add_dependency "activesupport", "4.2.6"
25
+ spec.add_dependency "activesupport", "~> 4.2.5"
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: softwear-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.4
4
+ version: 1.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Baillie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-18 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '='
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 4.2.6
61
+ version: 4.2.5
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '='
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 4.2.6
68
+ version: 4.2.5
69
69
  description:
70
70
  email:
71
71
  - nigel@annarbortees.com
@@ -97,6 +97,7 @@ files:
97
97
  - lib/softwear/lib/controller_authentication.rb
98
98
  - lib/softwear/lib/enqueue.rb
99
99
  - lib/softwear/lib/spec.rb
100
+ - lib/softwear/lib/tcp_server.rb
100
101
  - lib/softwear/lib/version.rb
101
102
  - softwear-lib.gemspec
102
103
  - spec/spec_helper.rb