Class: Sipgate::Base

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

Direct Known Subclasses

Fax

Constant Summary

PATH =
'/RPC2'

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(user = nil, password = nil)

A new instance of Base



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rsipgate/base.rb', line 10

def initialize(user = nil, password = nil)
  user ||= Sipgate.user
  password ||= Sipgate.password
  @client = XMLRPC::Client.new3({
    :host => my_api_host,
    :path => PATH,
    :use_ssl => true,
    :user => user,
    :password => password,
    :port => 443
  })
end

Instance Method Details

- (Object) call(*args)



29
30
31
32
33
34
35
# File 'lib/rsipgate/base.rb', line 29

def call(*args)
  response = Response.new(rubyized_hash(@client.call(*args)))
  unless response.success?
    raise ::Sipgate::Exception, "Response failed with #{response.status_string} with code #{response.status_code}" 
  end
  response
end

- (Object) my_api_host



6
7
8
# File 'lib/rsipgate/base.rb', line 6

def my_api_host
  Sipgate.api_host || 'api.sipgate.net'
end

- (Object) rubyized_hash(h)

make server responses look more Ruby like (underscored Symbol as Hash keys)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rsipgate/base.rb', line 38

def rubyized_hash(h)
  h.inject({}) do |memo, (k, v)|
    value = case v
    when ::Hash
      rubyized_hash(v)
    when ::Array
      v.map {|item| is_a?(::Hash) ? rubyized_hash(item) : item }
    else
      v
    end
    memo[k.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase.to_sym] = value
    memo
  end
end

- (Object) status(session_id)

queries the status of a fax



24
25
26
27
# File 'lib/rsipgate/base.rb', line 24

def status(session_id)
  call "samurai.SessionStatusGet",
       'SessionID' => session_id
end