Class: Shortener::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/shortener/configuration.rb

Overview

The class for storing Configuration Information

Constant Summary collapse

OPTIONS =
[:SHORTENER_URL, :DEFAULT_URL, :REDISTOGO_URL, :S3_KEY_PREFIX,
:S3_ACCESS_KEY_ID, :S3_SECRET_ACCESS_KEY, :S3_DEFAULT_ACL, :S3_BUCKET,
:DOTFILE_PATH, :S3_ENABLED, :SHORTENER_NS, :REQUIRE_AUTH, :ALLOW_SIGNUP,
:VIEWS]
HEROKU_IGNORE =
[:DOTFILE_PATH, :SHORTENER_URL, :REDISTOGO_URL, :REQUIRE_AUTH]
END_POINTS =
[:add, :fetch, :upload, :index, :delete]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = Hash.new) ⇒ Configuration

store any paassed options and parse ~/.shortener if exists priority goes dotfile < env < passed option



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/shortener/configuration.rb', line 28

def initialize(opts = Hash.new)
  # TODO check keys by calls opts.delete {|k| !OPTIONS.include?(k)
  unless opts.empty?
    opts.delete(:conf_current_placeholder)
    @options = Hash.new
    check_dotfile
    check_env
    @options = @options.merge!(opts)
    @options[:DEFAULT_URL] ||= '/index'
    if @options[:SHORTENER_URL] && @options[:SHORTENER_URL][-1] == '/'
      @options[:SHORTENER_URL].chop! 
    end
    @options[:SHORTENER_NS] ||= :shortener
    @options[:VIEWS] = !(@options[:VIEWS] == false || @options[:VIEWS] == 'false')
    @options[:ALLOW_SIGNUP] = @options.has_key?(:ALLOW_SIGNUP)
    if @options[:REQUIRE_AUTH].is_a?(String) || @options[:REQUIRE_AUTH].is_a?(Symbol)
      @options[:REQUIRE_AUTH] = @options[:REQUIRE_AUTH].to_s.split(',').map do |auth|
        auth.upcase.to_sym
      end
    end
    @options[:REQUIRE_AUTH] ||= []
  else
    @options = Configuration.current.options
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/shortener/configuration.rb', line 15

def options
  @options
end

Class Method Details

.currentObject



10
11
12
# File 'lib/shortener/configuration.rb', line 10

def current
  @current_configuration ||= Configuration.new(conf_current_placeholder: nil)
end

Instance Method Details

#auth_route?(url) ⇒ Boolean

check if this endpoint needs auth

Returns:

  • (Boolean)


158
159
160
161
162
163
# File 'lib/shortener/configuration.rb', line 158

def auth_route?(url)
  ep = url.split('/').last
  return false if ep.nil?
  ep.gsub!('.json', '')
  @options[:REQUIRE_AUTH].include?(ep.upcase.to_sym)
end

#authenticate?Boolean

a boolean indicating whether we should use authentication

Returns:

  • (Boolean)


153
154
155
# File 'lib/shortener/configuration.rb', line 153

def authenticate?
  !(@options[:REQUIRE_AUTH] == [])
end

#redisObject

a configured redis namespace instance



90
91
92
93
94
# File 'lib/shortener/configuration.rb', line 90

def redis
  uri = self.redistogo_url
  _r = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
  Redis::Namespace.new(self.ns, redis: _r)
end

#redistogo_urlObject

return the URI for the redistogo url



97
98
99
100
101
102
103
104
105
106
# File 'lib/shortener/configuration.rb', line 97

def redistogo_url
  begin
    URI.parse(@options[:REDISTOGO_URL])
  rescue Exception => boom
    puts "Error parsing redistogo_url: #{@options[:REDISTOGO_URL]}"
    puts "should probably be something like: redis://localhost:6379 if run locally"
    puts "if you're on Heroku, make sure you have the RedisToGo addon installed.\n\n"
    raise boom
  end
end

#s3_availableObject

is S3 enabled and configured?



124
125
126
# File 'lib/shortener/configuration.rb', line 124

def s3_available
  s3_enabled && s3_configured
end

#s3_configuredObject

are the necessary options present for S3 to work?



114
115
116
117
118
119
120
121
# File 'lib/shortener/configuration.rb', line 114

def s3_configured
  ret = true
  [:S3_KEY_PREFIX, :S3_ACCESS_KEY_ID, :S3_SECRET_ACCESS_KEY,
    :S3_DEFAULT_ACL, :S3_BUCKET ].each do |k|
    ret = !@options[k].nil? unless ret == false
  end
  ret
end

#s3_enabledObject

return a boolean of the S3_ENABLED option



109
110
111
# File 'lib/shortener/configuration.rb', line 109

def s3_enabled
  @options[:S3_ENABLED].to_s == 'true'
end

#s3_policyObject

build an S3 policy.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/shortener/configuration.rb', line 129

def s3_policy
  expiration_date = (Time.now + 36000).utc.strftime('%Y-%m-%dT%H:%M:%S.000Z') # 10.hours.from_now
  max_filesize = 2147483648 # 2.gigabyte
  policy = Base64.encode64(
    "{'expiration': '#{expiration_date}',
      'conditions': [
      {'bucket': '#{s3_bucket}'},
      ['starts-with', '$key', '#{s3_key_prefix}'],
      {'acl': '#{s3_default_acl}'},
      {'success_action_status': '201'},
      ['starts-with', '$Filename', ''],
      ['content-length-range', 0, #{max_filesize}]
      ]
      }"
  ).gsub(/\n|\r/, '')
end

#s3_signature(policy) ⇒ Object

Sign an S3 policy



147
148
149
150
# File 'lib/shortener/configuration.rb', line 147

def s3_signature(policy)
  signature = Base64.encode64(OpenSSL::HMAC.digest(
    OpenSSL::Digest::Digest.new('sha1'), s3_secret_access_key, policy)).gsub("\n","")
end

#to_jsonObject



73
74
75
76
77
78
# File 'lib/shortener/configuration.rb', line 73

def to_json
  safe_options = @options.delete_if do |k|
    [:S3_SECRET_ACCESS_KEY, :S3_ACCESS_KEY_ID, :REDISTOGO_URL].include?(k)
  end
  safe_options.to_json
end

#to_paramsObject

return a string ENV’s for command line use.



65
66
67
68
69
70
71
# File 'lib/shortener/configuration.rb', line 65

def to_params
  ret = Array.new
  @options.each {|k,v| ret << "#{k}=#{v}" unless HEROKU_IGNORE.include?(k)}
  _ra = @options[:REQUIRE_AUTH].join(',')
  ret << "REQUIRE_AUTH=#{_ra}"
  ret.join(" ")
end

#uri_for(end_point, opts = nil) ⇒ Object

return a URI for an endpoint based on this configuration



55
56
57
58
59
60
61
62
# File 'lib/shortener/configuration.rb', line 55

def uri_for(end_point, opts = nil)
  if END_POINTS.include?(end_point.to_sym)
    path = path_for(end_point, opts)
    URI.parse("#{@options[:SHORTENER_URL]}/#{path}.json")
  else
    raise "BAD ENDPOINT: #{end_point} is not a valid shortener end point."
  end
end