Class: ASPSMS::Config
- Inherits:
-
Object
- Object
- ASPSMS::Config
- Defined in:
- lib/aspsms.rb
Overview
Represents a configuration, file based or hash based.
Class Method Summary collapse
Instance Method Summary collapse
- #autoload ⇒ Object
- #gateways ⇒ Object
- #http_class ⇒ Object
-
#initialize(confdata) ⇒ Config
constructor
A new instance of Config.
- #load(fn) ⇒ Object
- #originator ⇒ Object
- #password ⇒ Object
- #set(conf) ⇒ Object
- #useragent ⇒ Object
- #userkey ⇒ Object
- #utf8(str) ⇒ Object
Constructor Details
#initialize(confdata) ⇒ Config
Returns a new instance of Config.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aspsms.rb', line 50 def initialize(confdata) if confdata.kind_of?(Hash) set(confdata) elsif confdata.kind_of?(String) load(confdata) else autoload end @charset = ASPSMS::Config.charset_from_environment if ENV.has_key?('http_proxy') uri = URI.parse(ENV['http_proxy']) user, pass = uri.userinfo.split(/:/) if uri.userinfo @http_class = Net::HTTP::Proxy(uri.host, uri.port, user, pass) else @http_class = Net::HTTP end end |
Class Method Details
Instance Method Details
#autoload ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aspsms.rb', line 68 def autoload FILES.each do |fn| begin load(fn) return rescue Errno::ENOENT # ignore if not found end end raise "No configuration file found (#{FILES.join(' ')})" end |
#gateways ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/aspsms.rb', line 126 def gateways if @gateway.nil? return [ 'xml1.aspsms.com:5061', 'xml2.aspsms.com:5098', 'xml1.aspsms.com:5098', 'xml2.aspsms.com:5061' ] else return [ @gateway ] end end |
#http_class ⇒ Object
139 140 141 |
# File 'lib/aspsms.rb', line 139 def http_class @http_class end |
#load(fn) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/aspsms.rb', line 80 def load(fn) conf = {} File.open(fn).each do |line| line.chomp.scan(/^\s*([a-zA-Z0-9]+)\s+([^\s#]+)/) do |k,v| case k.downcase when 'password' conf[:password] = v when 'userkey' conf[:userkey] = v when 'sender' # backwards compat conf[:originator] = v when 'originator' conf[:originator] = v when 'gateway' conf[:gateway] = v end end end set(conf) end |
#originator ⇒ Object
122 123 124 |
# File 'lib/aspsms.rb', line 122 def originator @originator.nil? ? 'aspsms' : @originator end |
#password ⇒ Object
118 119 120 |
# File 'lib/aspsms.rb', line 118 def password @password end |
#set(conf) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/aspsms.rb', line 101 def set(conf) @password = conf[:password] if conf.has_key?(:password) @userkey = conf[:userkey] if conf.has_key?(:userkey) @originator = conf[:originator] if conf.has_key?(:originator) @gateway = conf[:gateway] if conf.has_key?(:gateway) raise "#{fn}: 'userkey' missing!" if @userkey.nil? raise "#{fn}: 'password' missing!" if @password.nil? if !@gateway.nil? && !@gateway.match(/^[a-zA-Z0-9\[\]:._-]+:[0-9]{1,5}$/) raise "#{fn}: 'gateway' not in format 'host:port'!" end end |
#useragent ⇒ Object
135 136 137 |
# File 'lib/aspsms.rb', line 135 def useragent "ASPSMS::Gateway (http://www.roe.ch/ASPSMS)" end |
#userkey ⇒ Object
114 115 116 |
# File 'lib/aspsms.rb', line 114 def userkey @userkey end |
#utf8(str) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/aspsms.rb', line 143 def utf8(str) unless @charset.match(/utf-?8/i) return Iconv::iconv('utf-8', @charset, str)[0] else return str end end |