Class: Switchvox::Base

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

Overview

The primary class used to interact with Switchvox.

Constant Summary collapse

URL =
"/json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.

Raises:



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

def initialize(host, user, pass, options={})
  {:debug => false}.merge! options
  @debug = options[:debug]

  @host = host
  @user = user
  @pass = pass
  @url  = URI.parse("https://" + @host + URL)
  @ssl  = false
  @ssl  = true if @url.scheme == "https"
      
  @connection  = false
  @auth_header = false
  login!
  raise LoginError, "Invalid Username or Password" unless logged_in?
end

Instance Attribute Details

#auth_headerObject

Returns the value of attribute auth_header.



35
36
37
# File 'lib/switchvox/base.rb', line 35

def auth_header
  @auth_header
end

#connectionObject

Returns the value of attribute connection.



32
33
34
# File 'lib/switchvox/base.rb', line 32

def connection
  @connection
end

#debugObject

Returns the value of attribute debug.



34
35
36
# File 'lib/switchvox/base.rb', line 34

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



28
29
30
# File 'lib/switchvox/base.rb', line 28

def host
  @host
end

#passObject (readonly)

Returns the value of attribute pass.



31
32
33
# File 'lib/switchvox/base.rb', line 31

def pass
  @pass
end

#sessionObject

Returns the value of attribute session.



33
34
35
# File 'lib/switchvox/base.rb', line 33

def session
  @session
end

#urlObject

Returns the value of attribute url.



29
30
31
# File 'lib/switchvox/base.rb', line 29

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



30
31
32
# File 'lib/switchvox/base.rb', line 30

def user
  @user
end

Instance Method Details

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

A standard REST call to get a list of entries



55
56
57
58
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
85
86
87
88
89
90
# File 'lib/switchvox/base.rb', line 55

def request(method, parameters={})
  login! unless logged_in?
  json = wrap_json(method, parameters)
  
  # Send the request
  header   = {'Content-Type' => "text/json"}
  request  = Net::HTTP::Post.new(@url.path, header)
  request.digest_auth(@user, @pass, @auth_header)
  request.body = json
  response = @connection.request(request)

  if @debug
    puts "#{method}: Request"
    puts json
    puts "\n"
  end

  case response
    when Net::HTTPOK
      raise EmptyResponse unless response.body
      response_json = JSON.parse response.body
      if @debug
        puts "#{method}: Response:"
        pp response_json 
        puts "\n\n"
      end
      response_obj = response_json["response"]["result"].to_obj
      return response_obj
    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