rfid 0.0.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.
Files changed (43) hide show
  1. data/INSTALL.rdoc +55 -0
  2. data/README.rdoc +60 -0
  3. data/bin/rfid +18 -0
  4. data/bin/rfid_checkout +22 -0
  5. data/config.ru +9 -0
  6. data/config/boot.rb +6 -0
  7. data/config/environment.rb +8 -0
  8. data/config/rfid.yml +6 -0
  9. data/config/rfid.yml.sample +6 -0
  10. data/config/thin.yml +14 -0
  11. data/config/thin.yml.sample +14 -0
  12. data/lib/rfid.rb +75 -0
  13. data/lib/rfid/application.rb +115 -0
  14. data/lib/rfid/application/public/favicon.ico +0 -0
  15. data/lib/rfid/application/public/javascripts/application.js +12 -0
  16. data/lib/rfid/application/public/javascripts/debug.js +59 -0
  17. data/lib/rfid/application/public/stylesheets/application.css +49 -0
  18. data/lib/rfid/application/public/stylesheets/bootstrap.css +2467 -0
  19. data/lib/rfid/application/views/events.erb +34 -0
  20. data/lib/rfid/application/views/index.erb +21 -0
  21. data/lib/rfid/application/views/key_sets.erb +13 -0
  22. data/lib/rfid/application/views/key_string.erb +11 -0
  23. data/lib/rfid/application/views/keys.erb +20 -0
  24. data/lib/rfid/application/views/layout.erb +56 -0
  25. data/lib/rfid/application/views/redis.erb +16 -0
  26. data/lib/rfid/application/views/usage.erb +47 -0
  27. data/lib/rfid/cli.rb +78 -0
  28. data/lib/rfid/config.rb +23 -0
  29. data/lib/rfid/core_ext.rb +3 -0
  30. data/lib/rfid/core_ext/json_engine.rb +2 -0
  31. data/lib/rfid/event.rb +101 -0
  32. data/lib/rfid/translator.rb +68 -0
  33. data/lib/rfid/version.rb +3 -0
  34. data/lib/rfid/websocket.rb +35 -0
  35. data/lib/runner.rb +18 -0
  36. data/lib/server.rb +19 -0
  37. data/lib/templates/init +109 -0
  38. data/spec/models/application_spec.rb +54 -0
  39. data/spec/models/config_spec.rb +33 -0
  40. data/spec/models/event_spec.rb +22 -0
  41. data/spec/rfid_spec.rb +58 -0
  42. data/spec/spec_helper.rb +25 -0
  43. metadata +228 -0
@@ -0,0 +1,3 @@
1
+ module Rfid
2
+ VERSION = "0.0.1".freeze
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'em-websocket'
2
+
3
+ module Rfid
4
+ module WebSocket
5
+ def self.start
6
+ @channel = EM::Channel.new
7
+ Rfid::Translator.channel = @channel
8
+
9
+ EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
10
+ ws.onopen {
11
+ socket_id = @channel.subscribe { |msg| ws.send msg }
12
+ socket_time = Time.now
13
+
14
+ ws.send({:event => 'connect', :data => { :sid => socket_id, :state => ws.state }}.to_json)
15
+
16
+ Rfid::Translator.web_log('socket_opened', socket_id, "Origin: #{ws.request['host']}")
17
+
18
+ ws.onmessage { |msg|
19
+ Rfid::Translator.web_log('client_message', socket_id, "Message", msg)
20
+ }
21
+
22
+ ws.onclose {
23
+ @channel.unsubscribe(socket_id)
24
+ lifetime = Time.now - socket_time
25
+ Rfid::Translator.web_log('channel_unsubscribed', socket_id, "Lifetime: #{lifetime}")
26
+ }
27
+ }
28
+
29
+ ws.onerror { |error|
30
+ Rfid::Translator.web_log('api_error', nil, error.to_s)
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ # http://daemons.rubyforge.org/
2
+ #
3
+ require 'rubygems'
4
+ require 'daemons'
5
+
6
+ options = {
7
+ :app_name => "rfid_websocket",
8
+ :dir_mode => :system,
9
+ :multiple => false,
10
+ :backtrace => true,
11
+ :monitor => true,
12
+ :log_output => true,
13
+ :log_dir => '/var/log/'
14
+ }
15
+
16
+ puts "RFID websocket on 127.0.0.1:8080"
17
+
18
+ Daemons.run(File.join(File.dirname(__FILE__), 'server.rb'), options)
@@ -0,0 +1,19 @@
1
+ # Load the rails application
2
+ require 'eventmachine'
3
+ require 'thread'
4
+ require 'rfid'
5
+
6
+ Thread.abort_on_exception = true
7
+ threads = []
8
+
9
+ # Creating a thread for the EM event loop
10
+ threads << Thread.new do
11
+ EventMachine.run { Rfid::WebSocket.start }
12
+ end
13
+
14
+ # Creating a thread for the redis subscribe block
15
+ threads << Thread.new do
16
+ EventMachine.run { Rfid::Translator.start }
17
+ end
18
+
19
+ threads.map(&:join)
@@ -0,0 +1,109 @@
1
+ #!/bin/sh
2
+
3
+ ### BEGIN INIT INFO
4
+ # Provides: rfid
5
+ # Required-Start: $all
6
+ # Required-Stop: $all
7
+ # Default-Start: 2 3 4 5
8
+ # Default-Stop: 0 1 6
9
+ # Short-Description: starts the rfid web server
10
+ # Description: starts rfid using start-stop-daemon
11
+ ### END INIT INFO
12
+
13
+ #------------------------------------------------------------------------------
14
+ # Functions
15
+ #------------------------------------------------------------------------------
16
+ . /lib/lsb/init-functions
17
+
18
+ #------------------------------------------------------------------------------
19
+ # Consts
20
+ #------------------------------------------------------------------------------
21
+
22
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23
+ DAEMON=/usr/local/bin/rfid
24
+ DAEMON_OPTS='websocket'
25
+ NAME=rfid
26
+ DESC=rfid_websocket
27
+ RUNAS=root
28
+ PIDFILE="/var/run/${DESC}.pid"
29
+
30
+ #------------------------------------------------------------------------------
31
+ # Simple Tests
32
+ #------------------------------------------------------------------------------
33
+
34
+ #test if rfid is a file and executable
35
+ test -x $DAEMON || exit 0
36
+
37
+ set -e
38
+
39
+ #------------------------------------------------------------------------------
40
+ # Functions
41
+ #------------------------------------------------------------------------------
42
+
43
+ setFilePerms(){
44
+ if [ -f $PIDFILE ]; then
45
+ chmod 400 $PIDFILE
46
+ fi
47
+ }
48
+
49
+ removePIDFile(){
50
+ if [ -f $PIDFILE ]; then
51
+ rm -f $PIDFILE
52
+ fi
53
+ }
54
+
55
+ start() {
56
+ echo -n "Starting $DESC: "
57
+
58
+ start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
59
+ setFilePerms
60
+
61
+ echo "$NAME."
62
+ }
63
+
64
+ stop() {
65
+ echo -n "Stopping $DESC: "
66
+
67
+ RETVAL=0
68
+ for PFILE in /var/run/$DESC*.pid; do
69
+ start-stop-daemon --stop --quiet --pidfile $PFILE --retry=TERM/30/KILL/5
70
+ [ $? -gt ${RETVAL} ] && RETVAL=$?
71
+ rm -f $PFILE
72
+ done
73
+ log_end_msg ${RETVAL}
74
+ }
75
+
76
+ reload() {
77
+ echo -n "Reloading $DESC configuration: "
78
+
79
+ start-stop-daemon --stop --signal HUP --quiet --retry 10 --pidfile $PIDFILE --exec $DESC
80
+
81
+ echo "$NAME."
82
+ }
83
+
84
+ case "$1" in
85
+ start)
86
+ start
87
+ ;;
88
+ stop)
89
+ stop
90
+ ;;
91
+ restart|force-reload)
92
+ stop
93
+ sleep 1
94
+ start
95
+ ;;
96
+ reload)
97
+ reload
98
+ ;;
99
+ status)
100
+ status_of_proc -p $PIDFILE "$DAEMON" rfid && exit 0 || exit $?
101
+ ;;
102
+ *)
103
+ FULLPATH=/etc/init.d/$NAME
104
+ echo "Usage: $FULLPATH {start|stop|restart|reload|force-reload|status}" >&2
105
+ exit 1
106
+ ;;
107
+ esac
108
+
109
+ exit 0
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rfid::Application do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ Rfid::Application
8
+ end
9
+
10
+ it "should render index template" do
11
+ get '/'
12
+ last_response.should be_ok
13
+ end
14
+
15
+ it "should render redis template" do
16
+ get '/redis'
17
+ last_response.should be_ok
18
+ end
19
+
20
+ it "should render usage template" do
21
+ get '/usage'
22
+ last_response.should be_ok
23
+ end
24
+
25
+ it "should render events template" do
26
+ get '/events'
27
+ last_response.should be_ok
28
+ end
29
+
30
+ it "should render keys template" do
31
+ get '/keys'
32
+ last_response.should be_ok
33
+ end
34
+
35
+ it "should render redis key info template" do
36
+ get "/keys/#{Rfid.config.redis_key}"
37
+ last_response.should be_ok
38
+ end
39
+
40
+ it "should create rfid event by html" do
41
+ params = { :event => {:name => "test", :data => "Message from #{__FILE__}"} }
42
+ request "/events", :method => :post, :params => params
43
+ last_response.should be_redirect
44
+ Rfid.redis.lrange(Rfid.config.redis_key, 0, 20).should include(params[:event].to_json)
45
+ end
46
+
47
+ it "should create rfid event by json" do
48
+ params = { :event => {:name => "test json", :data => "Message json"} }
49
+ request "/events.json", :method => :post, :params => params
50
+ last_response.should be_ok
51
+ last_response.body.should == {:response => params[:event]}.to_json
52
+ Rfid.redis.lrange(Rfid.config.redis_key, 0, 20).should include(params[:event].to_json)
53
+ end
54
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rfid::Config do
4
+ before(:each) do
5
+ @filepath = File.expand_path("../../../config/rfid.yml", __FILE__)
6
+ @config = Rfid::Config.new(@filepath)
7
+ end
8
+
9
+ it "should parse file" do
10
+ @config.redis_url.should == "redis://127.0.0.1:6379/10"
11
+ @config.redis_key.should == "rfid_events"
12
+ @config.log_path.should == "/var/log/rfid.log"
13
+ @config.api_url.should == "https://rfidapi.aimbulance.com/api/v1"
14
+ @config.device_id.should == "test"
15
+ @config.device_token.should == "test"
16
+ end
17
+
18
+ it "should return parsed file path" do
19
+ @config.filepath.should == @filepath
20
+ end
21
+
22
+ it "should reparse config file" do
23
+ @config.redis_url = 'test'
24
+ @config.api_url = 'test'
25
+
26
+ @config.redis_url.should == 'test'
27
+ @config.api_url.should == 'test'
28
+
29
+ @config.reload!
30
+ @config.redis_url.should == "redis://127.0.0.1:6379/10"
31
+ @config.api_url.should == "https://rfidapi.aimbulance.com/api/v1"
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rfid::Event do
4
+ before(:each) do
5
+ @cards = ["testcard1", "testcard2"]
6
+ @event = Rfid::Event.new("testdevice", @cards)
7
+ end
8
+
9
+ it "should return valid attributes" do
10
+ a = @event.attributes
11
+ a[:key].should == "testdevice"
12
+ a[:device_id].should == "test"
13
+ a[:cards].should == @cards
14
+ a[:url].should == "https://rfidapi.aimbulance.com/api/v1/events/test.json"
15
+ end
16
+
17
+ # it "should save event" do
18
+ # #FakeWeb.register_uri(:get, @event.attributes[:url],
19
+ # # :body => '{"event": {data: {event:"Api event", message: "Api message"} } }')
20
+ # @event.save
21
+ # end
22
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe Rfid do
5
+ it "should be valid" do
6
+ Rfid.should be_a(Module)
7
+ end
8
+
9
+ context "redis" do
10
+ it "should parse redis connection options" do
11
+ op = Rfid.redis_options
12
+
13
+ op[:host].should == "127.0.0.1"
14
+ op[:port].should == 6379
15
+ op[:db].should == 10
16
+ op[:password].should be_nil
17
+ end
18
+
19
+ it "should get redis id" do
20
+ Rfid.redis_id.should_not be_blank
21
+ end
22
+
23
+ it "should get redis keys" do
24
+ Rfid.push_event({:event => "test", :data => "test"})
25
+ Rfid.keys.should include(Rfid.config.redis_key)
26
+ end
27
+
28
+ it "should push event into list" do
29
+ hash = {:event => "test push", :data => "test data"}
30
+ Rfid.push_event(hash)
31
+ Rfid.redis.lrange(Rfid.config.redis_key, 0, 20).should include(hash.to_json)
32
+ end
33
+ end
34
+
35
+ context "config" do
36
+ it "should exists config file" do
37
+ File.exists?(Rfid.config_path).should be_true
38
+ end
39
+
40
+ it "should parse default configuration" do
41
+ Rfid.config.should_not be_nil
42
+
43
+ Rfid.config.redis_url.should == "redis://127.0.0.1:6379/10"
44
+ Rfid.config.redis_key.should == "rfid_events"
45
+ Rfid.config.log_path.should == "/var/log/rfid.log"
46
+ Rfid.config.api_url.should == "https://rfidapi.aimbulance.com/api/v1"
47
+ Rfid.config.device_id.should == "test"
48
+ Rfid.config.device_token.should == "test"
49
+ end
50
+ end
51
+
52
+ context "logger" do
53
+ it "should create new log file" do
54
+ Rfid.log("Spec testing message")
55
+ File.exists?(Rfid.config.log_path).should be_true
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,25 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require 'rack/test'
4
+
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ $:.push File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require "rfid"
9
+
10
+ # Load support files
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
12
+
13
+ RSpec.configure do |config|
14
+ # Remove this line if you don't want RSpec's should and should_not
15
+ # methods or matchers
16
+ require 'rspec/expectations'
17
+ config.include RSpec::Matchers
18
+
19
+ # == Mock Framework
20
+ config.mock_with :rspec
21
+
22
+ config.before(:suite) do
23
+ Rfid.redis.flushdb
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Galeta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: em-websocket
16
+ requirement: &76301570 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *76301570
25
+ - !ruby/object:Gem::Dependency
26
+ name: em-hiredis
27
+ requirement: &76300470 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *76300470
36
+ - !ruby/object:Gem::Dependency
37
+ name: redis
38
+ requirement: &76299980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.2.2
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *76299980
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: &75797700 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.3
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *75797700
58
+ - !ruby/object:Gem::Dependency
59
+ name: sinatra
60
+ requirement: &75796970 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.1
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *75796970
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-contrib
71
+ requirement: &75795150 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.1.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *75795150
80
+ - !ruby/object:Gem::Dependency
81
+ name: thin
82
+ requirement: &75794240 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 1.3.0
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *75794240
91
+ - !ruby/object:Gem::Dependency
92
+ name: curb
93
+ requirement: &75792670 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 0.7.16
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: *75792670
102
+ - !ruby/object:Gem::Dependency
103
+ name: json
104
+ requirement: &75790260 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.6.1
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *75790260
113
+ - !ruby/object:Gem::Dependency
114
+ name: i18n
115
+ requirement: &75789800 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: *75789800
124
+ - !ruby/object:Gem::Dependency
125
+ name: daemons
126
+ requirement: &75789120 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: *75789120
135
+ - !ruby/object:Gem::Dependency
136
+ name: thor
137
+ requirement: &75788340 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ type: :runtime
144
+ prerelease: false
145
+ version_requirements: *75788340
146
+ description: Provider for RFID API and driver by websokets protocol
147
+ email: galeta.igor@gmail.com
148
+ executables:
149
+ - rfid
150
+ - rfid_checkout
151
+ extensions: []
152
+ extra_rdoc_files:
153
+ - README.rdoc
154
+ - INSTALL.rdoc
155
+ files:
156
+ - bin/rfid_checkout
157
+ - bin/rfid
158
+ - lib/server.rb
159
+ - lib/rfid.rb
160
+ - lib/runner.rb
161
+ - lib/templates/init
162
+ - lib/rfid/translator.rb
163
+ - lib/rfid/core_ext/json_engine.rb
164
+ - lib/rfid/application/views/events.erb
165
+ - lib/rfid/application/views/usage.erb
166
+ - lib/rfid/application/views/redis.erb
167
+ - lib/rfid/application/views/keys.erb
168
+ - lib/rfid/application/views/key_string.erb
169
+ - lib/rfid/application/views/key_sets.erb
170
+ - lib/rfid/application/views/layout.erb
171
+ - lib/rfid/application/views/index.erb
172
+ - lib/rfid/application/public/stylesheets/application.css
173
+ - lib/rfid/application/public/stylesheets/bootstrap.css
174
+ - lib/rfid/application/public/favicon.ico
175
+ - lib/rfid/application/public/javascripts/application.js
176
+ - lib/rfid/application/public/javascripts/debug.js
177
+ - lib/rfid/websocket.rb
178
+ - lib/rfid/cli.rb
179
+ - lib/rfid/config.rb
180
+ - lib/rfid/application.rb
181
+ - lib/rfid/core_ext.rb
182
+ - lib/rfid/version.rb
183
+ - lib/rfid/event.rb
184
+ - config/thin.yml
185
+ - config/boot.rb
186
+ - config/thin.yml.sample
187
+ - config/rfid.yml
188
+ - config/environment.rb
189
+ - config/rfid.yml.sample
190
+ - config.ru
191
+ - README.rdoc
192
+ - INSTALL.rdoc
193
+ - spec/rfid_spec.rb
194
+ - spec/models/event_spec.rb
195
+ - spec/models/config_spec.rb
196
+ - spec/models/application_spec.rb
197
+ - spec/spec_helper.rb
198
+ homepage: http://www.aimbulance.com/
199
+ licenses: []
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ none: false
206
+ requirements:
207
+ - - ! '>='
208
+ - !ruby/object:Gem::Version
209
+ version: 1.9.2
210
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
+ none: false
212
+ requirements:
213
+ - - ! '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements:
217
+ - redis v2.4, libcurl
218
+ rubyforge_project: rfid
219
+ rubygems_version: 1.8.10
220
+ signing_key:
221
+ specification_version: 3
222
+ summary: Websocket for RFID
223
+ test_files:
224
+ - spec/rfid_spec.rb
225
+ - spec/models/event_spec.rb
226
+ - spec/models/config_spec.rb
227
+ - spec/models/application_spec.rb
228
+ - spec/spec_helper.rb