params_extractor 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,6 +5,9 @@ Used to encode or decode a URL params. This is useful for sending out encrypted
5
5
  ## Getting started example
6
6
 
7
7
  ```ruby
8
+ Params::Crypter.off! # turn encryption off (is on by default) - global setting
9
+ Params::Crypter.on! # turn encryption back on - global setting
10
+
8
11
  # singleton Crypter shared between Encoder and Decoder (uses 'gibberish' gem)
9
12
  # Recommended: load password from a DB table, YAML file or similar
10
13
 
@@ -33,6 +36,14 @@ Used to encode or decode a URL params. This is useful for sending out encrypted
33
36
  # retrieve param values from hash
34
37
  id = hash[:id].to_i
35
38
  name = hash[:name]
39
+
40
+ # set crypter on/off for individual encoder or decoder
41
+
42
+ # turn off for encoder
43
+ encoder = Params::Encoder.new "a=7", :crypter => false
44
+
45
+ # turn off for decoder
46
+ decoder = Params::Decoder.new token, :crypter => false
36
47
  ```
37
48
 
38
49
  Look at the specs for more on usage.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -0,0 +1,19 @@
1
+ module Params
2
+ module Base
3
+ def use_crypter?
4
+ crypter? || Params::Crypter.on?
5
+ end
6
+
7
+ def crypter?
8
+ !(@crypter == false)
9
+ end
10
+
11
+ def crypter_off!
12
+ @crypter = false
13
+ end
14
+
15
+ def crypter_on!
16
+ @crypter = true
17
+ end
18
+ end
19
+ end
@@ -5,21 +5,49 @@ module Params
5
5
  class Crypter
6
6
  include Singleton
7
7
 
8
- def self.password
9
- Params::Crypter.instance.password
10
- end
11
-
12
- def self.password= pass
13
- Params::Crypter.instance.password = pass
8
+ class << self
9
+ def on!
10
+ @on = true
11
+ end
12
+
13
+ def off!
14
+ @on = false
15
+ end
16
+
17
+ def on?
18
+ !(@on == false)
19
+ end
20
+
21
+ def off?
22
+ !on?
23
+ end
24
+
25
+ def password
26
+ Params::Crypter.instance.password
27
+ end
28
+
29
+ def password= pass
30
+ Params::Crypter.instance.password = pass
31
+ end
14
32
  end
15
33
 
16
34
  attr_writer :password
17
35
 
36
+ def on?
37
+ Params::Crypter.on?
38
+ end
39
+
40
+ def off?
41
+ Params::Crypter.off?
42
+ end
43
+
18
44
  def encrypt data
45
+ return data if off?
19
46
  cipher.enc data
20
47
  end
21
48
 
22
49
  def decrypt data
50
+ return data if off?
23
51
  cipher.dec data
24
52
  end
25
53
 
@@ -1,12 +1,19 @@
1
1
  module Params
2
- class Decoder
2
+ class Decoder
3
+ include Params::Base
4
+
3
5
  attr_reader :params, :hash
4
6
 
5
- def initialize params
6
- raise "No profile params" if !params || params.empty?
7
- @params = params
8
- end
7
+ def initialize *args
8
+ raise "Decoder needs params to encode" if !args || args.empty?
9
+ args = args.flatten
10
+ options = args.last
11
+ if options.kind_of? Hash
12
+ @crypter = options.delete(:crypter) if options
13
+ end
9
14
 
15
+ @params = args.first
16
+ end
10
17
 
11
18
  # fx decoder.as_hash['a'] returns value of a
12
19
  def as_hash mode = :str
@@ -21,7 +28,7 @@ module Params
21
28
 
22
29
  # decode in reverse order!
23
30
  def decoded
24
- @decoded ||= crypter.decrypt decoded_params
31
+ @decoded ||= use_crypter? ? crypter.decrypt(decoded_params) : decoded_params
25
32
  end
26
33
 
27
34
  protected
@@ -31,7 +38,7 @@ module Params
31
38
  end
32
39
 
33
40
  def crypter
34
- Crypter.instance
41
+ Crypter.instance if crypter?
35
42
  end
36
43
  end
37
44
  end
@@ -1,14 +1,22 @@
1
1
  module Params
2
2
  class Encoder
3
+ include Params::Base
4
+
3
5
  attr_reader :params
4
6
 
5
- def initialize params
6
- raise "No profile params" if !params || params.empty?
7
- @params = case params
7
+ def initialize *args
8
+ raise "Encoder needs params to encode" if !args || args.empty?
9
+ args = args.flatten
10
+ options = args.last
11
+ if options.kind_of? Hash
12
+ @crypter = options.delete(:crypter)
13
+ end
14
+ arg = args.first
15
+ @params = case arg
8
16
  when Hash
9
- create_from params
17
+ create_from arg
10
18
  when String
11
- params
19
+ arg
12
20
  else
13
21
  raise "Must be a Hash or String, was #{arg}"
14
22
  end
@@ -16,7 +24,7 @@ module Params
16
24
 
17
25
  # encode after encryption to ensure Base64 compatibility in link
18
26
  def encoded
19
- @encoded ||= Base64.encode64 encrypted_params
27
+ @encoded ||= use_crypter? ? Base64.encode64(encrypted_params) : Base64.encode64(params)
20
28
  end
21
29
 
22
30
  protected
@@ -34,7 +42,7 @@ module Params
34
42
  end
35
43
 
36
44
  def crypter
37
- Crypter.instance
45
+ Crypter.instance if crypter?
38
46
  end
39
47
  end
40
48
  end
@@ -1,3 +1,4 @@
1
1
  require 'params/crypter'
2
+ require 'params/base'
2
3
  require 'params/encoder'
3
4
  require 'params/decoder'
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "params_extractor"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-04-28"
12
+ s.date = "2012-05-07"
13
13
  s.description = "Encode and encrypt prams and leter unencryt and decode them - useful when sending invites with tokens"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -25,18 +25,23 @@ Gem::Specification.new do |s|
25
25
  "README.md",
26
26
  "Rakefile",
27
27
  "VERSION",
28
+ "lib/params/base.rb",
28
29
  "lib/params/crypter.rb",
29
30
  "lib/params/decoder.rb",
30
31
  "lib/params/encoder.rb",
31
32
  "lib/params_extractor.rb",
32
33
  "params_extractor.gemspec",
33
- "spec/params_extractor_spec.rb",
34
+ "spec/decoder_ex.rb",
35
+ "spec/encoder_ex.rb",
36
+ "spec/params_extractor/crypter_spec.rb",
37
+ "spec/params_extractor/decoder_spec.rb",
38
+ "spec/params_extractor/encoder_spec.rb",
34
39
  "spec/spec_helper.rb"
35
40
  ]
36
41
  s.homepage = "http://github.com/kristianmandrup/params_extractor"
37
42
  s.licenses = ["MIT"]
38
43
  s.require_paths = ["lib"]
39
- s.rubygems_version = "1.8.22"
44
+ s.rubygems_version = "1.8.24"
40
45
  s.summary = "Package and unpackage URL params as encrypted tokens"
41
46
 
42
47
  if s.respond_to? :specification_version then
@@ -0,0 +1,9 @@
1
+ shared_examples 'a decoder' do
2
+ specify { subject.decoded.should_not == data}
3
+
4
+ specify do
5
+ subject.as_hash.should == decoded_data
6
+ end
7
+
8
+ specify { subject.as_hash(:sym).should == data }
9
+ end
@@ -0,0 +1,3 @@
1
+ shared_examples 'an encoder' do
2
+ specify { subject.encoded.should_not == data }
3
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Params::Crypter do
4
+ subject { crypter }
5
+
6
+ let(:params) do
7
+ {}
8
+ end
9
+
10
+ let(:crypter) { Params::Crypter.instance }
11
+ let(:data) { "a=5&b=7" }
12
+
13
+ before do
14
+ Params::Crypter.password = "secret"
15
+ end
16
+
17
+ describe 'on?' do
18
+ specify { subject.on?.should be_true }
19
+ end
20
+
21
+ describe 'off?' do
22
+ specify { subject.off?.should be_false }
23
+ end
24
+
25
+ describe 'encrypt' do
26
+ specify { subject.encrypt(data).should_not == data }
27
+ end
28
+
29
+ describe 'decrypt' do
30
+ let(:encrypted) do
31
+ subject.encrypt(data)
32
+ end
33
+
34
+ specify { subject.decrypt(encrypted).should == data }
35
+ end
36
+
37
+ context 'Global Crypter turned off' do
38
+ before do
39
+ Params::Crypter.off!
40
+ end
41
+
42
+ specify { subject.encrypt(data).should == data }
43
+
44
+ describe 'on?' do
45
+ specify { subject.on?.should be_false }
46
+ end
47
+
48
+ describe 'off?' do
49
+ specify { subject.off?.should be_true }
50
+ end
51
+
52
+ describe 'turn crypter back on' do
53
+ before :each do
54
+ Params::Crypter.on!
55
+ end
56
+
57
+ describe 'on?' do
58
+ specify { subject.on?.should be_true }
59
+ end
60
+
61
+ describe 'off?' do
62
+ specify { subject.off?.should be_false }
63
+ end
64
+
65
+ specify { subject.encrypt(data).should_not == data }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+ require 'decoder_ex'
3
+
4
+ describe Params::Decoder do
5
+ subject { decoder }
6
+
7
+ let(:params) do
8
+ {}
9
+ end
10
+
11
+ let(:encoder) { Params::Encoder.new data, params }
12
+ let(:decoder) { Params::Decoder.new encoded }
13
+ let(:encoded) do
14
+ encoder.encoded
15
+ end
16
+
17
+ let(:data) do
18
+ {:id => '7', :name => 'kris'}
19
+ end
20
+
21
+ let(:decoded_data) do
22
+ {'id' => '7', 'name' => 'kris'}
23
+ end
24
+
25
+ # before do
26
+ # Params::Crypter.on!
27
+ # end
28
+
29
+ describe 'crypter?' do
30
+ specify { subject.crypter?.should be_true }
31
+ end
32
+
33
+ describe 'use_crypter?' do
34
+ specify { subject.use_crypter?.should be_true }
35
+ end
36
+
37
+ describe 'decode' do
38
+ it_behaves_like 'a decoder'
39
+ end
40
+
41
+ context 'Global Crypter turned off' do
42
+ before :each do
43
+ Params::Crypter.off!
44
+ end
45
+
46
+ describe 'decode' do
47
+ it_behaves_like 'a decoder'
48
+ end
49
+ end
50
+
51
+ context 'Decoder crypter turned off' do
52
+ let(:params) do
53
+ {:crypter => false}
54
+ end
55
+
56
+ describe 'decode' do
57
+ it_behaves_like 'a decoder'
58
+ end
59
+ end
60
+
61
+ context 'Decoder crypter turned off after init' do
62
+ before :each do
63
+ subject.crypter_off!
64
+ end
65
+
66
+ describe 'crypter?' do
67
+ specify { subject.crypter?.should be_false }
68
+ end
69
+
70
+ describe 'decode' do
71
+ it_behaves_like 'a decoder'
72
+ end
73
+ end
74
+
75
+ context 'Decoder crypter turned back on after init' do
76
+ let(:params) do
77
+ {:crypter => false}
78
+ end
79
+
80
+ before :each do
81
+ subject.crypter_on!
82
+ end
83
+
84
+ describe 'crypter?' do
85
+ specify { subject.crypter?.should be_true }
86
+ end
87
+
88
+ describe 'decode' do
89
+ it_behaves_like 'a decoder'
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'encoder_ex'
3
+
4
+ describe Params::Encoder do
5
+ subject { encoder }
6
+
7
+ let(:params) do
8
+ {}
9
+ end
10
+
11
+ let(:data) do
12
+ {:id => 7, :name => 'kris'}
13
+ end
14
+
15
+ let(:encoder) { Params::Encoder.new data, params }
16
+
17
+ # before do
18
+ # Params::Crypter.on!
19
+ # end
20
+
21
+ describe 'crypter?' do
22
+ specify { subject.crypter?.should be_true }
23
+ end
24
+
25
+ describe 'use_crypter?' do
26
+ specify { subject.use_crypter?.should be_true }
27
+ end
28
+
29
+ describe 'encode' do
30
+ specify { subject.encoded.should_not == data }
31
+ end
32
+
33
+ context 'Global Crypter turned off' do
34
+ before do
35
+ Params::Crypter.off!
36
+ end
37
+
38
+ it_behaves_like 'an encoder'
39
+ end
40
+
41
+ context 'Encoder crypter turned off' do
42
+ let(:params) do
43
+ {:crypter => false}
44
+ end
45
+
46
+ it_behaves_like 'an encoder'
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-28 00:00:00.000000000 Z
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gibberish
@@ -124,12 +124,17 @@ files:
124
124
  - README.md
125
125
  - Rakefile
126
126
  - VERSION
127
+ - lib/params/base.rb
127
128
  - lib/params/crypter.rb
128
129
  - lib/params/decoder.rb
129
130
  - lib/params/encoder.rb
130
131
  - lib/params_extractor.rb
131
132
  - params_extractor.gemspec
132
- - spec/params_extractor_spec.rb
133
+ - spec/decoder_ex.rb
134
+ - spec/encoder_ex.rb
135
+ - spec/params_extractor/crypter_spec.rb
136
+ - spec/params_extractor/decoder_spec.rb
137
+ - spec/params_extractor/encoder_spec.rb
133
138
  - spec/spec_helper.rb
134
139
  homepage: http://github.com/kristianmandrup/params_extractor
135
140
  licenses:
@@ -146,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
151
  version: '0'
147
152
  segments:
148
153
  - 0
149
- hash: -2391327292111278477
154
+ hash: -448216205111061566
150
155
  required_rubygems_version: !ruby/object:Gem::Requirement
151
156
  none: false
152
157
  requirements:
@@ -155,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
160
  version: '0'
156
161
  requirements: []
157
162
  rubyforge_project:
158
- rubygems_version: 1.8.22
163
+ rubygems_version: 1.8.24
159
164
  signing_key:
160
165
  specification_version: 3
161
166
  summary: Package and unpackage URL params as encrypted tokens
@@ -1,64 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Params::Crypter do
4
- subject { crypter }
5
-
6
- let(:crypter) { Params::Crypter.instance }
7
- let(:data) { "a=5&b=7" }
8
-
9
- before do
10
- Params::Crypter.password = "secret"
11
- end
12
-
13
- describe 'encrypt' do
14
- specify { subject.encrypt(data).should_not == data }
15
- end
16
-
17
- describe 'decrypt' do
18
- let(:encrypted) do
19
- subject.encrypt(data)
20
- end
21
-
22
- specify { subject.decrypt(encrypted).should == data }
23
- end
24
- end
25
-
26
- describe Params::Encoder do
27
- subject { encoder }
28
-
29
- let(:data) do
30
- {:id => 7, :name => 'kris'}
31
- end
32
-
33
- let(:encoder) { Params::Encoder.new data }
34
-
35
- describe 'encode' do
36
- specify { subject.encoded.should_not == data }
37
- end
38
- end
39
-
40
- describe Params::Decoder do
41
- subject { decoder }
42
-
43
- let(:encoder) { Params::Encoder.new data }
44
-
45
- let(:data) do
46
- {:id => '7', :name => 'kris'}
47
- end
48
-
49
- describe 'decode' do
50
- let(:encoded) do
51
- encoder.encoded
52
- end
53
-
54
- let(:decoder) { Params::Decoder.new encoded }
55
-
56
- specify { subject.decoded.should_not == data}
57
-
58
- specify do
59
- subject.as_hash.should == {'id' => '7', 'name' => 'kris'}
60
- end
61
-
62
- specify { subject.as_hash(:sym).should == data }
63
- end
64
- end