Class: Switchvox::Base
Overview
The primary class used to interact with Switchvox.
Constant Summary collapse
- URL =
"/json"
Instance Attribute Summary collapse
-
#auth_header ⇒ Object
Returns the value of attribute auth_header.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
Returns the value of attribute host.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#session ⇒ Object
Returns the value of attribute session.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(host, user, pass, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#request(method, parameters = {}) ⇒ Object
A standard REST call to get a list of entries.
Constructor Details
#initialize(host, user, pass, options = {}) ⇒ Base
Returns a new instance of Base.
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, ={}) {:debug => false}.merge! @debug = [: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_header ⇒ Object
Returns the value of attribute auth_header.
35 36 37 |
# File 'lib/switchvox/base.rb', line 35 def auth_header @auth_header end |
#connection ⇒ Object
Returns the value of attribute connection.
32 33 34 |
# File 'lib/switchvox/base.rb', line 32 def connection @connection end |
#debug ⇒ Object
Returns the value of attribute debug.
34 35 36 |
# File 'lib/switchvox/base.rb', line 34 def debug @debug end |
#host ⇒ Object
Returns the value of attribute host.
28 29 30 |
# File 'lib/switchvox/base.rb', line 28 def host @host end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
31 32 33 |
# File 'lib/switchvox/base.rb', line 31 def pass @pass end |
#session ⇒ Object
Returns the value of attribute session.
33 34 35 |
# File 'lib/switchvox/base.rb', line 33 def session @session end |
#url ⇒ Object
Returns the value of attribute url.
29 30 31 |
# File 'lib/switchvox/base.rb', line 29 def url @url end |
#user ⇒ Object (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 |