atech 1.0.15 → 1.0.16

Sign up to get free protection for your applications and to get access to all the features.
data/lib/atech/base.rb ADDED
@@ -0,0 +1,8 @@
1
+ ## To provide a basic set of Atech functionality which would be used in
2
+ ## most normal applications. Simply require this file which will handle things
3
+ ## for you.
4
+ ##
5
+ ## require 'atech/base'
6
+
7
+ require 'atech/extensions/string'
8
+ require 'atech/helpers'
@@ -1,3 +1,8 @@
1
+ ## This file should be required in your 'db/seeds.rb' file if you'd like some
2
+ ## nice reporting whenever you run your database seed.
3
+ ##
4
+ ## require 'atech/extensions/seeds'
5
+
1
6
  module ActiveRecord
2
7
  class Base
3
8
  def save(options = {})
@@ -0,0 +1,40 @@
1
+ ## Extensions the standard String ruby class to provide additional functionality.
2
+ ## Enable this in your application using:
3
+ ##
4
+ ##  require 'atech/extensions/string'
5
+
6
+ require 'digest'
7
+
8
+ class String
9
+
10
+ class << self
11
+ ## Returns a psuedo-random UUID which sepearated by hyphens in standard UUID
12
+ ## format. It's worth noting this doesn't actually follow the specification
13
+ ## for Version 4 UUIDs.
14
+ def generate_token(options = {})
15
+ values = [rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x1000000), rand(0x1000000)]
16
+ "%04x%04x-%04x-%04x-%04x-%06x%06x" % values
17
+ end
18
+
19
+ ## Returns a random string of the size specified. This can be used for salts
20
+ ## or generated passwords. The generated string will only include lower case
21
+ ## characters and numbers 0 though 9.
22
+ def random(size = 25)
23
+ array = ('a'..'z').to_a + (0..9).to_a
24
+ (0...size).map{ array[rand(array.size)] }.join
25
+ end
26
+ end
27
+
28
+ ## Returns the hex-encoded SHA1 hash value of the current string. Optionally, pass
29
+ ## a value to limit the length of the returned SHA.
30
+ def to_sha1(length = 40)
31
+ Digest::SHA1.hexdigest(self)[0,length]
32
+ end
33
+
34
+ ## Returns the hex-encoded MD5 hash value of the current string. Optionally, pass
35
+ ## a value to limit the length of the returned SHA.
36
+ def to_md5(length = 32)
37
+ Digest::MD5.hexdigest(self)[0,length]
38
+ end
39
+
40
+ end
data/lib/atech/helpers.rb CHANGED
@@ -1,3 +1,13 @@
1
+ ## Include this file in your ApplicationHelper to add some standard and useful
2
+ ## HTML helpers for your Rails applications.
3
+ ##
4
+ ## module ApplicationHelper
5
+ ## include Atech::Helpers
6
+ ##
7
+ ## ... your own helpers..
8
+ ##
9
+ ## end
10
+
1
11
  module Atech
2
12
  module Helpers
3
13
 
@@ -0,0 +1,59 @@
1
+ ## Implements support for detecting whether an IP address belongs to a pre-defined
2
+ ## set of networks specified in CIDR notation. By default, it uses the standard
3
+ ## aTech Media IP addresses + localhost. The range of IPs can be changed by
4
+ ## changing `Atech::NetworkRestrictions.networks` to an array containing strings
5
+ ## of other networks.
6
+
7
+ ## You can use the included Atech::NetworkRestrictions::RouteConstraint in your
8
+ ## routing files to restrict routes. For example:
9
+ ##
10
+ ## constraints Atech::NetworkRestrictions::RouteConstraint do
11
+ ## namespace :admin do
12
+ ## resources :accounts
13
+ ## resources :tickets
14
+ ## end
15
+ ## end
16
+ ##
17
+ ## You can also use this method to query IP addresses whenever required using
18
+ ## `Atech::NetworkRestrictions.approved_ip?('123.123.123.123')`.
19
+
20
+ require 'ipaddr'
21
+
22
+ module Atech
23
+ module NetworkRestrictions
24
+
25
+ class << self
26
+ attr_accessor :networks
27
+
28
+ def networks
29
+ @networks ||= ['127.0.0.1/32', '109.224.145.104/29', '83.170.74.114/32', '10.0.1.0/24']
30
+ end
31
+
32
+ def approved_ip?(requested_ip)
33
+ !!approved_ip(requested_ip)
34
+ end
35
+
36
+ def approved_ip(requested_ip)
37
+ self.networks.each do |i|
38
+ if IPAddr.new(i).include?(requested_ip)
39
+ return i
40
+ end
41
+ end
42
+ return false
43
+ rescue ArgumentError => e
44
+ if e.message == 'invalid address'
45
+ return false
46
+ else
47
+ raise
48
+ end
49
+ end
50
+ end
51
+
52
+ class RouteConstraint
53
+ def self.matches?(request)
54
+ Atech::NetworkRestrictions.approved_ip?(request.ip)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atech
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 55
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 15
10
- version: 1.0.15
9
+ - 16
10
+ version: 1.0.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - aTech Media
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 +01:00
18
+ date: 2010-09-09 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,9 +28,12 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
+ - lib/atech/base.rb
31
32
  - lib/atech/extensions/controller_tests.rb
32
33
  - lib/atech/extensions/seeds.rb
34
+ - lib/atech/extensions/string.rb
33
35
  - lib/atech/helpers.rb
36
+ - lib/atech/network_restrictions.rb
34
37
  has_rdoc: true
35
38
  homepage: http://github.com/atech/atech
36
39
  licenses: []