Class: SMSTradeRB::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



10
11
12
# File 'lib/smstraderb/server.rb', line 10

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/smstraderb/server.rb', line 8

def params
  @params
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/smstraderb/server.rb', line 14

def [](key)
  @options[key]
end

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smstraderb/server.rb', line 18

def call(env)
  @params = Rack::Request.new(env).params

  # The route param is mandatory.
  unless @params['route']
    return respond_with('40')
  end

  # There are only a few valid route values.
  unless SMSTradeRB::ROUTES.include?(@params['route'].to_sym)
    return respond_with('40')
  end

  # The to params is mandatory.
  unless @params['to']
    return respond_with('10')
  end

  # The to params needs to be in a valid format.
  unless @params['to'] =~ /^\+?\d+/
    return respond_with('10')
  end

  # The key param is mandatory.
  unless @params['key']
    return respond_with('50')
  end

  # The message params is mandatory.
  unless @params['message']
    return respond_with('30')
  end

  # Check if the messagetype is valid.
  if @params['messagetype'] and !SMSTradeRB::MESSAGE_TYPES.include?(@params['messagetype'].to_sym)
    return respond_with('31')
  end

  # Everything is fine so far. We return 100.
  ret = [@options[:code] || 100]

  # Some optional parameters which modify the return value.
  if @params['message_id'] == '1'
    ret[1] = '123456789'
  end

  if @params['cost'] == '1'
    ret[2] = '0.055'
  end

  if @params['count'] == '1'
    ret[3] = '1'
  end

  respond_with(*ret)
end