params_extractor 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'gibberish'
4
+
5
+ group :development do
6
+ gem "rspec", ">= 2.8.0"
7
+ gem "rdoc", ">= 3.12"
8
+ gem "bundler", ">= 1.1.0"
9
+ gem "jeweler", ">= 1.8.3"
10
+ gem "simplecov", ">= 0.5"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ gibberish (1.2.0)
6
+ git (1.2.5)
7
+ jeweler (1.8.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.6.6)
13
+ multi_json (1.3.2)
14
+ rake (0.9.2.2)
15
+ rdoc (3.12)
16
+ json (~> 1.4)
17
+ rspec (2.9.0)
18
+ rspec-core (~> 2.9.0)
19
+ rspec-expectations (~> 2.9.0)
20
+ rspec-mocks (~> 2.9.0)
21
+ rspec-core (2.9.0)
22
+ rspec-expectations (2.9.1)
23
+ diff-lcs (~> 1.1.3)
24
+ rspec-mocks (2.9.0)
25
+ simplecov (0.6.2)
26
+ multi_json (~> 1.3)
27
+ simplecov-html (~> 0.5.3)
28
+ simplecov-html (0.5.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (>= 1.1.0)
35
+ gibberish
36
+ jeweler (>= 1.8.3)
37
+ rdoc (>= 3.12)
38
+ rspec (>= 2.8.0)
39
+ simplecov (>= 0.5)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Params extractor
2
+
3
+ Used to encode or decode a URL params. This is useful for sending out encrypted links with token and then unencrypting, decoding and parsing them later when they come back to the app.
4
+
5
+ ## Getting started example
6
+
7
+ ```ruby
8
+ # singleton Crypter shared between Encoder and Decoder (uses 'gibberish' gem)
9
+ # Recommended: load password from a DB table, YAML file or similar
10
+
11
+ Params::Crypter.password = "my very secret password"
12
+
13
+ # encrypt and encode the params
14
+ params = {:id => 7, :name => 'kris'}
15
+
16
+ encoder = Params::Encoder.new(params)
17
+ encoder.params # => 'id=7&name=kris'
18
+
19
+ encoded_params = encoder.encoded
20
+
21
+ email_link = "#{domain}/invite?token=" + encoded_params
22
+
23
+ # send link in email or Facebook notification or similar
24
+
25
+ # later... someone clicks on the link
26
+
27
+ # decrypt and decode the params
28
+ decoded_params = Params::Decoder.new(params).decoded
29
+
30
+ # retrieve hash from param values
31
+ hash = decoded_params.as_hash(:sym)
32
+
33
+ # retrieve param values from hash
34
+ id = hash[:id].to_i
35
+ name = hash[:name]
36
+ ```
37
+
38
+ Look at the specs for more on usage.
39
+
40
+ ## Contributing to params_extractor
41
+
42
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
43
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
44
+ * Fork the project.
45
+ * Start a feature/bugfix branch.
46
+ * Commit and push until you are happy with your contribution.
47
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
48
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
49
+
50
+ ## Copyright
51
+
52
+ Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
53
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "params_extractor"
18
+ gem.homepage = "http://github.com/kristianmandrup/params_extractor"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Package and unpackage URL params as encrypted tokens}
21
+ gem.description = %Q{Encode and encrypt prams and leter unencryt and decode them - useful when sending invites with tokens}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "params_extractor #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,36 @@
1
+ require 'singleton'
2
+ require 'gibberish'
3
+
4
+ module Params
5
+ class Crypter
6
+ include Singleton
7
+
8
+ def self.password
9
+ Params::Crypter.instance.password
10
+ end
11
+
12
+ def self.password= pass
13
+ Params::Crypter.instance.password = pass
14
+ end
15
+
16
+ attr_writer :password
17
+
18
+ def encrypt data
19
+ cipher.enc data
20
+ end
21
+
22
+ def decrypt data
23
+ cipher.dec data
24
+ end
25
+
26
+ protected
27
+
28
+ def cipher
29
+ @cipher ||= Gibberish::AES.new(password)
30
+ end
31
+
32
+ def password
33
+ @password ||= 'xyZ1/!2kdi9o_#'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ module Params
2
+ class Decoder
3
+ attr_reader :params, :hash
4
+
5
+ def initialize params
6
+ raise "No profile params" if !params || params.empty?
7
+ @params = params
8
+ end
9
+
10
+
11
+ # fx decoder.as_hash['a'] returns value of a
12
+ def as_hash mode = :str
13
+ @hash ||= Hash[decoded.split('&').collect{|p| p.split("=")}]
14
+ case mode
15
+ when :sym
16
+ Hash[hash.map{ |k, v| [k.to_sym, v] }]
17
+ else
18
+ hash
19
+ end
20
+ end
21
+
22
+ # decode in reverse order!
23
+ def decoded
24
+ @decoded ||= crypter.decrypt decoded_params
25
+ end
26
+
27
+ protected
28
+
29
+ def decoded_params
30
+ Base64.decode64(params)
31
+ end
32
+
33
+ def crypter
34
+ Crypter.instance
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ module Params
2
+ class Encoder
3
+ attr_reader :params
4
+
5
+ def initialize params
6
+ raise "No profile params" if !params || params.empty?
7
+ @params = case params
8
+ when Hash
9
+ create_from params
10
+ when String
11
+ params
12
+ else
13
+ raise "Must be a Hash or String, was #{arg}"
14
+ end
15
+ end
16
+
17
+ # encode after encryption to ensure Base64 compatibility in link
18
+ def encoded
19
+ @encoded ||= Base64.encode64 encrypted_params
20
+ end
21
+
22
+ protected
23
+
24
+ def encrypted_params
25
+ crypter.encrypt(params)
26
+ end
27
+
28
+ # {:a => b, :c = 2} -> a=b&c=2
29
+ def create_from arg
30
+ @params = arg.inject([]) do |res, hash|
31
+ res << [hash.first, hash.last].join('=')
32
+ res
33
+ end.join('&')
34
+ end
35
+
36
+ def crypter
37
+ Crypter.instance
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ require 'params/crypter'
2
+ require 'params/encoder'
3
+ require 'params/decoder'
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "params_extractor"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2012-04-28"
13
+ s.description = "Encode and encrypt prams and leter unencryt and decode them - useful when sending invites with tokens"
14
+ s.email = "kmandrup@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/params/crypter.rb",
29
+ "lib/params/decoder.rb",
30
+ "lib/params/encoder.rb",
31
+ "lib/params_extractor.rb",
32
+ "params_extractor.gemspec",
33
+ "spec/params_extractor_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = "http://github.com/kristianmandrup/params_extractor"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "1.8.22"
40
+ s.summary = "Package and unpackage URL params as encrypted tokens"
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<gibberish>, [">= 0"])
47
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
48
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
49
+ s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
50
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
51
+ s.add_development_dependency(%q<simplecov>, [">= 0.5"])
52
+ else
53
+ s.add_dependency(%q<gibberish>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
55
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
56
+ s.add_dependency(%q<bundler>, [">= 1.1.0"])
57
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
58
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<gibberish>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
63
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
64
+ s.add_dependency(%q<bundler>, [">= 1.1.0"])
65
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
66
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,64 @@
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
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'params_extractor'
3
+
4
+ RSpec.configure do |config|
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: params_extractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gibberish
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.3
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.3
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0.5'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0.5'
110
+ description: Encode and encrypt prams and leter unencryt and decode them - useful
111
+ when sending invites with tokens
112
+ email: kmandrup@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ files:
119
+ - .document
120
+ - .rspec
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - VERSION
127
+ - lib/params/crypter.rb
128
+ - lib/params/decoder.rb
129
+ - lib/params/encoder.rb
130
+ - lib/params_extractor.rb
131
+ - params_extractor.gemspec
132
+ - spec/params_extractor_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: http://github.com/kristianmandrup/params_extractor
135
+ licenses:
136
+ - MIT
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ segments:
148
+ - 0
149
+ hash: -2391327292111278477
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.22
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Package and unpackage URL params as encrypted tokens
162
+ test_files: []