api_key_maker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/api_key_maker.gemspec +23 -0
- data/lib/api_key_maker.rb +13 -0
- data/lib/api_key_maker/model_additions.rb +11 -0
- data/lib/api_key_maker/railtie.rb +9 -0
- data/lib/api_key_maker/version.rb +3 -0
- data/spec/api_key_maker/model_additions_spec.rb +28 -0
- data/spec/api_key_maker_spec.rb +22 -0
- data/spec/spec_helper.rb +2 -0
- metadata +87 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Johnathan Pulos
|
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,35 @@
|
|
1
|
+
# API Key Maker
|
2
|
+
|
3
|
+
Using SHA1 encryption and the current time, this gem generates a random string that is useful for API keys. The length of the string can be set as an attribute up to 39 characters in length. This is my attempt at creating a gem.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem "api_key_maker"
|
10
|
+
```
|
11
|
+
|
12
|
+
**Requires Ruby 1.9.2 or later.**
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
Whenever you need to generate a key, just call the following method:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
ApiKeyMaker.make_api_key(10)
|
19
|
+
```
|
20
|
+
The first parameter tells what length you want the key to be. It defaults to 10 characters, but can go up to 39 characters in length.
|
21
|
+
|
22
|
+
If you want an attribute of a model to default to an api key, then just add the following in your model:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class ApiAccess < ActiveRecord::Base
|
26
|
+
make_api_key :api_token, 13
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
This method takes 2 parameters. The first is the attribute that will be set to the api key. The second is the length (1-39 defaults to 10) of the string you want. This api key is generated before the model's validation.
|
31
|
+
|
32
|
+
## Development
|
33
|
+
Questions or problems? Please post them on the [issue tracker](https://github.com/codemis/api_key_maker/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
|
34
|
+
|
35
|
+
This gem is created by Johnathan Pulos and is under the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "api_key_maker/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "api_key_maker"
|
7
|
+
s.version = ApiKeyMaker::VERSION
|
8
|
+
s.authors = ["Johnathan Pulos"]
|
9
|
+
s.email = ["johnathan@still-water.com"]
|
10
|
+
s.homepage = "https://github.com/codemis/api_key_maker"
|
11
|
+
s.summary = %q{Generates a random unique api key string}
|
12
|
+
s.description = %q{Using SHA1 encryption and the current time, this gem generates a random string that is useful for API keys. The length of the string can be set as an attribute up to 39 characters in length.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "api_key_maker"
|
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
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "supermodel"
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "api_key_maker/version"
|
2
|
+
require "api_key_maker/model_additions"
|
3
|
+
require "api_key_maker/railtie" if defined? Rails
|
4
|
+
require "digest/sha1"
|
5
|
+
|
6
|
+
module ApiKeyMaker
|
7
|
+
|
8
|
+
# Create an API Key
|
9
|
+
def self.make_api_key(length = 10)
|
10
|
+
Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..length]
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class ApiAccess < SuperModel::Base
|
4
|
+
include ActiveModel::Validations::Callbacks
|
5
|
+
extend ApiKeyMaker::ModelAdditions
|
6
|
+
attr_accessor :api_token, :api_special_token
|
7
|
+
make_api_key :api_token, 13
|
8
|
+
make_api_key :api_special_token
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ApiKeyMaker::ModelAdditions do
|
12
|
+
|
13
|
+
it "should create the api_token upon saving" do
|
14
|
+
ApiAccess.create!().api_token.blank?.should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should create the api_token upon saving to a length of 13 characters" do
|
18
|
+
ApiAccess.create!().api_token.length.should eq(13)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should create the api_special_token upon saving" do
|
22
|
+
ApiAccess.create!().api_special_token.blank?.should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should create the api_special_token upon saving to a default of 10 characters" do
|
26
|
+
ApiAccess.create!().api_special_token.length.should eq(10)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ApiKeyMaker do
|
5
|
+
|
6
|
+
it "should create an api key" do
|
7
|
+
ApiKeyMaker.make_api_key.blank?.should be_false
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should create an api key to a set length" do
|
11
|
+
ApiKeyMaker.make_api_key(5).length.should eq(5)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should create an api key to a default length" do
|
15
|
+
ApiKeyMaker.make_api_key.length.should eq(10)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return 39 characters even though you ask for more" do
|
19
|
+
ApiKeyMaker.make_api_key(80).length.should eq(39)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_key_maker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Johnathan Pulos
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2156061700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156061700
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: supermodel
|
27
|
+
requirement: &2156060020 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156060020
|
36
|
+
description: Using SHA1 encryption and the current time, this gem generates a random
|
37
|
+
string that is useful for API keys. The length of the string can be set as an attribute
|
38
|
+
up to 39 characters in length.
|
39
|
+
email:
|
40
|
+
- johnathan@still-water.com
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- .rspec
|
47
|
+
- CHANGELOG.md
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- api_key_maker.gemspec
|
53
|
+
- lib/api_key_maker.rb
|
54
|
+
- lib/api_key_maker/model_additions.rb
|
55
|
+
- lib/api_key_maker/railtie.rb
|
56
|
+
- lib/api_key_maker/version.rb
|
57
|
+
- spec/api_key_maker/model_additions_spec.rb
|
58
|
+
- spec/api_key_maker_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: https://github.com/codemis/api_key_maker
|
61
|
+
licenses: []
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: api_key_maker
|
80
|
+
rubygems_version: 1.8.10
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Generates a random unique api key string
|
84
|
+
test_files:
|
85
|
+
- spec/api_key_maker/model_additions_spec.rb
|
86
|
+
- spec/api_key_maker_spec.rb
|
87
|
+
- spec/spec_helper.rb
|