idiot 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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in idiot.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "idiot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "idiot"
7
+ s.version = Idiot::VERSION
8
+ s.authors = ["Chris Serino"]
9
+ s.email = ["themindoverall@gmail.com"]
10
+ s.homepage = "http://github.com/themindoverall/idiot"
11
+ s.summary = %q{Idiot encodes your numerical IDs.}
12
+ s.description = %q{Idiot lets you create encoded IDs like youtube or bitly.}
13
+
14
+ s.rubyforge_project = "idiot"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,62 @@
1
+ require "idiot/version"
2
+ require "idiot/config"
3
+
4
+ require 'digest/sha2'
5
+
6
+ if defined?(Rails)
7
+ require "idiot/railtie"
8
+ end
9
+
10
+ module Idiot
11
+ extend self
12
+
13
+ def initialize
14
+ @index = Idiot::Config.alphabet
15
+ pass_key = Idiot::Config.pass_key
16
+
17
+ i = @index.split(//)
18
+
19
+ passhash = (Digest::SHA2.new << pass_key).to_s
20
+ passhash = if passhash.size < @index.size
21
+ (Digest::SHA2.new(512) << pass_key).to_s
22
+ else
23
+ passhash
24
+ end
25
+ p = passhash.split(//)
26
+ @index = i.zip(p).sort{|x, y| x[1] <=> y[1] }.collect{|x| x[0]}.join
27
+
28
+ @base = @index.size
29
+ @pad_up = 5
30
+ end
31
+
32
+ def encode(num)
33
+ initialize
34
+ num += @base ** (@pad_up - 1)
35
+
36
+ result = ""
37
+ n = Math.log(num, @base).floor
38
+
39
+ n.step(0, -1) {|i|
40
+ bcp = @base ** i
41
+ a = ((num / bcp) % @base).floor
42
+ result << @index.slice(a, 1)
43
+ num = num - (a * bcp)
44
+ }
45
+
46
+ result.reverse!
47
+ end
48
+
49
+ def decode(str)
50
+ initialize
51
+ str.reverse!
52
+ result = 0
53
+ len = str.size - 1
54
+ (0..len).each do |i|
55
+ bcp = @base ** (len - i)
56
+ result += @index.index(str.slice(i, 1)) * bcp
57
+ end
58
+
59
+ result -= @base ** (@pad_up - 1)
60
+ result.to_i
61
+ end
62
+ end
@@ -0,0 +1,21 @@
1
+ module Idiot
2
+ module Config
3
+ extend self
4
+
5
+ def pass_key
6
+ @pass_key or "Im An IdI0t! :)"
7
+ end
8
+
9
+ def pass_key= value
10
+ @pass_key = value
11
+ end
12
+
13
+ def alphabet
14
+ @alphabet or "abcdfghjklmnpqrstvwxyz0123456789BCDFGHJKLMNPQRSTVWXYZ"
15
+ end
16
+
17
+ def alphabet= value
18
+ @alphabet = value
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Rails
2
+ module Idiot
3
+ class Railtie < Rails::Railtie
4
+ config.idiot = ::Idiot::Config
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Idiot
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idiot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Chris Serino
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-03-03 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Idiot lets you create encoded IDs like youtube or bitly.
17
+ email:
18
+ - themindoverall@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - Gemfile
28
+ - Rakefile
29
+ - idiot.gemspec
30
+ - lib/idiot.rb
31
+ - lib/idiot/config.rb
32
+ - lib/idiot/railtie.rb
33
+ - lib/idiot/version.rb
34
+ homepage: http://github.com/themindoverall/idiot
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project: idiot
57
+ rubygems_version: 1.8.15
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Idiot encodes your numerical IDs.
61
+ test_files: []
62
+