Class: ASPSMS::Config

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

Overview

Represents a configuration, file based or hash based.

Class Method Summary collapse

Instance Method Summary collapse

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

.charset_from_environmentObject

Determine the charset to convert from for stdin or argv from the locale; if unset, default to UTF-8, which is compatible with 7 bit ASCII.



39
40
41
42
43
44
45
46
# File 'lib/aspsms.rb', line 39

def self.charset_from_environment
  ['LC_ALL', 'LC_CTYPE', 'LANG'].each do |key|
    if ENV.has_key?(key)
      return ENV[key].match(/\./) ? ENV[key].sub(/^.*\./, '') : 'utf-8'
    end
  end
  return 'utf-8'
end

Instance Method Details

#autoloadObject



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

#gatewaysObject



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_classObject



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

#originatorObject



122
123
124
# File 'lib/aspsms.rb', line 122

def originator
  @originator.nil? ? 'aspsms' : @originator
end

#passwordObject



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

#useragentObject



135
136
137
# File 'lib/aspsms.rb', line 135

def useragent
  "ASPSMS::Gateway (http://www.roe.ch/ASPSMS)"
end

#userkeyObject



114
115
116
# File 'lib/aspsms.rb', line 114

def userkey
  @userkey
end

#utf8(str) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/aspsms.rb', line 143

def utf8(str)
  unless @charset.match(/^UTF-?8$/i)
    return str.encode('UTF-8', @charset, {
      :invalid => :replace,
      :undef => :replace,
      :replace => "?"})
  else
    return str
  end
end