rack-sl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8d7e4d19c1e06a7f94592b8feea46b4d8ce21f1
4
+ data.tar.gz: 9db9c15713f1ba8289416f6234c2640bd4f45215
5
+ SHA512:
6
+ metadata.gz: 2defb1bf2c89381c4d2f69476733ff41cce817f6d6c67285af9b5ae4ae6266204e20c2ecb10a3e26c80a4b807b17431b853db2b5d0771151a14e9a3358c937d1
7
+ data.tar.gz: 55bf8718d3414c64f5a638d5c7092011377a720a57e32ec833b1994208f1da59b2f234a1189ba0c79546a221136e347f5aaf2511abde8892f97edb7acddb1d06
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-sl (0.0.1)
5
+ rack (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ celluloid (0.15.2)
11
+ timers (~> 1.1.0)
12
+ coderay (1.1.0)
13
+ diff-lcs (1.2.5)
14
+ ffi (1.9.3)
15
+ formatador (0.2.4)
16
+ guard (2.2.5)
17
+ formatador (>= 0.2.4)
18
+ listen (~> 2.1)
19
+ lumberjack (~> 1.0)
20
+ pry (>= 0.9.12)
21
+ thor (>= 0.18.1)
22
+ guard-rspec (2.6.0)
23
+ guard (>= 1.8)
24
+ rspec (~> 2.13)
25
+ listen (2.4.0)
26
+ celluloid (>= 0.15.2)
27
+ rb-fsevent (>= 0.9.3)
28
+ rb-inotify (>= 0.9)
29
+ lumberjack (1.0.4)
30
+ method_source (0.8.2)
31
+ pry (0.9.12.4)
32
+ coderay (~> 1.0)
33
+ method_source (~> 0.8)
34
+ slop (~> 3.4)
35
+ rack (1.5.2)
36
+ rb-fsevent (0.9.3)
37
+ rb-inotify (0.9.3)
38
+ ffi (>= 0.5.0)
39
+ rspec (2.14.1)
40
+ rspec-core (~> 2.14.0)
41
+ rspec-expectations (~> 2.14.0)
42
+ rspec-mocks (~> 2.14.0)
43
+ rspec-core (2.14.7)
44
+ rspec-expectations (2.14.4)
45
+ diff-lcs (>= 1.1.3, < 2.0)
46
+ rspec-mocks (2.14.4)
47
+ slop (3.4.7)
48
+ thor (0.18.1)
49
+ timers (1.1.0)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ guard-rspec (~> 2.0)
56
+ rack-sl!
57
+ rspec (~> 2.0)
@@ -0,0 +1,23 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bennett Goble
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Rack::SL
2
+
3
+ Rack::SL adds basic recognition of [Second Life][sl]&reg; ([LSL][lsl] script) requests. It parses [X-SecondLife][headers] headers into structs for easy reference within [Rack][rack] applications.
4
+
5
+ ## Install
6
+
7
+ Add the gem to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-sl'
11
+ ```
12
+
13
+ ### Accessing Parsed Information
14
+
15
+ **Rack::SL** adds value to [Rack's][rack] `env` Hash with the key `sl`. This value is an *OpenStruct* with the following data:
16
+
17
+ | Key | Value | Description | Present |
18
+ |----------|-----------|-----------------------------------------------------|----------------|
19
+ | from_sl | bool | Whether the request had all of SL's X headers | Always |
20
+ | error | exception | Exception caught while parsing headers | If encountered |
21
+ | name | string | Originating object's name | If from_sl |
22
+ | key | string | UUID of originating object | If from_sl |
23
+ | region | struct | Struct with region name and `x` and `y` coordinates | If from_sl |
24
+ | owner | struct | Struct with owner's `name` and `key` (UUID) | If from_sl |
25
+ | position | struct | Struct with `x`, `y` and `z` coordinates | If from_sl |
26
+ | rotation | struct | Struct with `x`, `y`, `z` and `s` coordinates | If from_sl |
27
+ | velocity | struct | Struct with `x`, `y` and `z` coordinates | If from_sl |
28
+
29
+ ### Rails
30
+
31
+ Configure Rails to use *Rack::SL* in the `config/application.rb` file:
32
+
33
+ ```ruby
34
+ module MyApp
35
+ class Application < Rails::Application
36
+
37
+ # ...
38
+
39
+ config.middleware.use Rack::SL
40
+
41
+ end
42
+ end
43
+ ```
44
+
45
+ ### Sinatra
46
+
47
+ ```ruby
48
+ require 'sinatra'
49
+
50
+ configure do
51
+ use Rack::SL
52
+ end
53
+
54
+ # ...
55
+
56
+ ```
57
+ ...or in a modular app:
58
+
59
+ ```ruby
60
+ require 'sinatra/base'
61
+
62
+ class MyApp < Sinatra::Base
63
+ configure do
64
+
65
+ # ...
66
+
67
+ use Rack::SL
68
+ end
69
+
70
+ # ...
71
+
72
+ run!
73
+ end
74
+ ```
75
+
76
+ [sl]: http://secondlife.com/ "Official Second Life Homepage"
77
+ [lsl]: http://wiki.secondlife.com/wiki/LSL_Portal "Linden Scripting Language"
78
+ [headers]: http://wiki.secondlife.com/wiki/LlHTTPRequest "X-SecondLife HTTP Header Documentation"
79
+ [rack]: https://github.com/rack/rack "Rack Github Repository"
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,57 @@
1
+ require 'ostruct'
2
+
3
+ SLVector3 = Struct.new(:x, :y, :z)
4
+ SLQuaternion = Struct.new(:x, :y, :z, :s)
5
+ SLRegion = Struct.new(:name, :x, :y)
6
+ SLOwner = Struct.new(:name, :key)
7
+
8
+ module Rack
9
+ class SL
10
+
11
+ def initialize(app)
12
+ @app = app
13
+ end
14
+
15
+ def call(env)
16
+ sl = env['HTTP_X_SECONDLIFE_REGION'] ? parse_headers(env) : {from_sl: false}
17
+ env['sl'] = OpenStruct.new(sl)
18
+ @app.call(env)
19
+ end
20
+
21
+ private
22
+
23
+ def parse_headers(env)
24
+ begin
25
+ sl = {from_sl: true}
26
+ sl[:shard] = env['HTTP_X_SECONDLIFE_SHARD']
27
+ sl[:name] = env['HTTP_X_SECONDLIFE_OBJECT_NAME']
28
+ sl[:key] = env['HTTP_X_SECONDLIFE_OBJECT_KEY']
29
+ sl[:region] = parse_region(env['HTTP_X_SECONDLIFE_REGION'])
30
+ sl[:position] = parse_vector3(env['HTTP_X_SECONDLIFE_LOCAL_POSITION'])
31
+ sl[:rotation] = parse_rotation(env['HTTP_X_SECONDLIFE_LOCAL_ROTATION'])
32
+ sl[:velocity] = parse_vector3(env['HTTP_X_SECONDLIFE_LOCAL_VELOCITY'])
33
+ sl[:owner] = SLOwner.new(env['HTTP_X_SECONDLIFE_OWNER_NAME'],
34
+ env['HTTP_X_SECONDLIFE_OWNER_KEY'])
35
+ sl
36
+ rescue => e
37
+ {from_sl: false, error: e}
38
+ end
39
+ end
40
+
41
+ def parse_region(value)
42
+ name = /^.+(?= \()/.match(value).to_s
43
+ x, y = value.match(/\((\-?[0-9]+), (\-?[0-9]+)\)/).captures
44
+ SLRegion.new(name, x.to_i, y.to_i)
45
+ end
46
+
47
+ def parse_vector3(value)
48
+ x, y, z = value.scan(/\-?\d+.\d+/)
49
+ SLVector3.new(x.to_f, y.to_f, z.to_f)
50
+ end
51
+
52
+ def parse_rotation(value)
53
+ x, y, z, s = value.scan(/\-?\d+.\d+/)
54
+ SLQuaternion.new(x.to_f, y.to_f, z.to_f, s.to_f)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class SL
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ require './lib/rack/sl/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'rack-sl'
6
+ s.version = Rack::SL::VERSION
7
+ s.summary = 'Rack middleware for parsing X-SecondLife headers.'
8
+ s.homepage = 'https://github.com/nivardus/rack-sl'
9
+ s.authors = ['Bennett Goble']
10
+ s.license = 'MIT'
11
+
12
+ s.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' }
13
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
14
+ s.require_paths = ['lib']
15
+
16
+ s.add_dependency 'rack', '~> 1.0'
17
+ s.add_development_dependency 'rspec', '~> 2.0'
18
+ s.add_development_dependency 'guard-rspec', '~> 2.0'
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'rack/mock'
2
+
3
+ module Helpers
4
+
5
+ def valid_headers
6
+ env = default_env
7
+ env['HTTP_X_SECONDLIFE_SHARD'] = 'Production'
8
+ env['HTTP_X_SECONDLIFE_OBJECT_NAME'] = 'Object'
9
+ env['HTTP_X_SECONDLIFE_OBJECT_KEY'] = '01234567-89ab-cdef-0123-456789abcdef'
10
+ env['HTTP_X_SECONDLIFE_REGION'] = 'Jin Ho (264448, 233984)'
11
+ env['HTTP_X_SECONDLIFE_LOCAL_POSITION'] = '(173.009827, 75.551231, 60.950001)'
12
+ env['HTTP_X_SECONDLIFE_LOCAL_ROTATION'] = '-0.17304, 0.14914, 0.22335, 0.94759'
13
+ env['HTTP_X_SECONDLIFE_LOCAL_VELOCITY'] = '1.000000, 0.500000, -0.250000'
14
+ env['HTTP_X_SECONDLIFE_OWNER_NAME'] = 'Nivardus String'
15
+ env['HTTP_X_SECONDLIFE_OWNER_KEY'] = '01234567-89ab-cdef-0123-456789abcdef'
16
+ env
17
+ end
18
+
19
+ def invalid_headers
20
+ env = default_env
21
+ env['HTTP_X_SECONDLIFE_SHARD'] = 'Production'
22
+ env['HTTP_X_SECONDLIFE_REGION'] = 'Jin Ho (264448, 233984)'
23
+ env['HTTP_X_SECONDLIFE_LOCAL_POSITION'] = '(173.009827, 75.551231, 60.950001)'
24
+ env
25
+ end
26
+
27
+ def default_env
28
+ Rack::MockRequest.env_for
29
+ end
30
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::SL do
4
+
5
+ context '.call' do
6
+ let(:app) { lambda { |env| env }}
7
+ let(:middleware) do
8
+ Rack::SL.new(app)
9
+ end
10
+
11
+ context 'with no SL headers' do
12
+ let(:request_env) { default_env }
13
+ let(:env) { middleware.call(request_env) }
14
+
15
+ it 'should add SL information to env' do
16
+ env.should include('sl')
17
+ end
18
+
19
+ it 'should identify that the request is not from SL' do
20
+ env['sl'].from_sl.should eq(false)
21
+ end
22
+ end
23
+
24
+ context 'with valid SL headers' do
25
+ let(:request_env) { valid_headers }
26
+ let(:env) { middleware.call(request_env) }
27
+ let(:sl) { env['sl'] }
28
+
29
+ it 'should add SL information to env' do
30
+ env.should include('sl')
31
+ end
32
+
33
+ it 'should identify that the request is from SL' do
34
+ sl.from_sl.should eq(true)
35
+ end
36
+
37
+ it 'should parse header information' do
38
+ sl.region.name.should eq('Jin Ho')
39
+ sl.region.x.should eq(264448)
40
+ sl.region.y.should eq(233984)
41
+ sl.name.should eq('Object')
42
+ sl.key.should eq('01234567-89ab-cdef-0123-456789abcdef')
43
+ sl.owner.name.should eq('Nivardus String')
44
+ sl.owner.key.should eq('01234567-89ab-cdef-0123-456789abcdef')
45
+ sl.position.x.should eq(173.009827)
46
+ sl.position.y.should eq(75.551231)
47
+ sl.position.z.should eq(60.950001)
48
+ sl.rotation.x.should eq(-0.17304)
49
+ sl.rotation.y.should eq(0.14914)
50
+ sl.rotation.z.should eq(0.22335)
51
+ sl.rotation.s.should eq(0.94759)
52
+ sl.velocity.x.should eq(1.0)
53
+ sl.velocity.y.should eq(0.5)
54
+ sl.velocity.z.should eq(-0.25)
55
+ end
56
+ end
57
+
58
+ context 'with invalid SL headers' do
59
+ let(:request_env) { invalid_headers }
60
+ let(:env) { middleware.call(request_env) }
61
+
62
+ it 'should add SL information to env' do
63
+ env.should include('sl')
64
+ end
65
+
66
+ it 'should identify that the request is not from SL' do
67
+ env['sl'].from_sl.should eq(false)
68
+ end
69
+
70
+ it 'should include the first error' do
71
+ env['sl'].error.should_not eq(nil)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack/sl'
2
+ require_relative 'helpers'
3
+
4
+ RSpec.configure do |config|
5
+ config.include Helpers
6
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-sl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bennett Goble
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - Guardfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rack/sl.rb
68
+ - lib/rack/sl/version.rb
69
+ - rack-sl.gemspec
70
+ - spec/helpers.rb
71
+ - spec/sl_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/nivardus/rack-sl
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.1.11
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Rack middleware for parsing X-SecondLife headers.
97
+ test_files:
98
+ - spec/helpers.rb
99
+ - spec/sl_spec.rb
100
+ - spec/spec_helper.rb