Class: PBXtra::Base

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

Constant Summary collapse

URL =
"/cpa.cgi"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, opts = {}) ⇒ Base

Returns a new instance of Base.

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pbxtra.rb', line 25

def initialize(user, pass, opts={})
  options = { 
    :host   => "https://cp52.fonality.com",
    :debug  => false
  }.merge! opts
  @debug = options[:debug]
  
  @user = user
  @pass = pass
  @host = options[:host]
  @url  = URI.parse(@host + URL)
  
  # Handles http/https in url string
  @ssl  = false
  @ssl  = true if @url.scheme == "https"
  
  @connection = false
  login!
  raise LoginError, "Invalid Username or Password" unless logged_in?
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



21
22
23
# File 'lib/pbxtra.rb', line 21

def connection
  @connection
end

Returns the value of attribute cookie.



23
24
25
# File 'lib/pbxtra.rb', line 23

def cookie
  @cookie
end

#debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/pbxtra.rb', line 22

def debug
  @debug
end

#passObject (readonly)

Returns the value of attribute pass.



20
21
22
# File 'lib/pbxtra.rb', line 20

def pass
  @pass
end

#urlObject

Returns the value of attribute url.



18
19
20
# File 'lib/pbxtra.rb', line 18

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



19
20
21
# File 'lib/pbxtra.rb', line 19

def user
  @user
end

Instance Method Details

#get(method, parameters = {}) ⇒ Object



46
47
48
49
# File 'lib/pbxtra.rb', line 46

def get(method, parameters={})
  @url.query = wrap_url_query(method, parameters)
  request(Net::HTTP::Get.new(@url.path + @url.query, header)).body
end

#post(method = nil, parameters = {}, body = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/pbxtra.rb', line 51

def post(method=nil, parameters={}, body={})
  @url.query = wrap_url_query(method, parameters)
  req = Net::HTTP::Post.new(@url.path + @url.query, header)
  req.body = wrap_body(body)
  request(req)
end

#request(request) ⇒ Object

Send a request to the CP. Send Auth information if we have it.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pbxtra.rb', line 59

def request(request)
  login! unless logged_in?
  if @debug
    puts "Request Type: #{request.class}"
    puts "Request Path: #{@url}"
    puts "Request Body: #{request.body}"
    puts "\n"
  end
  response = @connection.request(request)
  if @debug
    puts "Response Header: " + response.header.to_s
    puts "Response Body: " + response.body 
    puts "\n"
  end
  case response
    when Net::HTTPOK
      return response
    when Net::HTTPUnauthorized
      login!
      request(method, parameters)
    when Net::HTTPForbidden
      raise LoginError, "Invalid Username or Password" 
    else 
      raise UnhandledResponse, "Can't handle response #{response}"
  end
end