Class: VoipfoneClient::Client
- Inherits:
-
Object
- Object
- VoipfoneClient::Client
- Defined in:
- lib/voipfone_client/client.rb,
lib/voipfone_client/voicemail.rb,
lib/voipfone_client/phone_numbers.rb,
lib/voipfone_client/account_balance.rb,
lib/voipfone_client/account_details.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
-
#account_balance ⇒ Float
Return the balance of the account as a float.
-
#account_details ⇒ Hash
Of account details.
-
#initialize ⇒ VoipfoneClient::Client
constructor
Intantiates a new Voipfone client, with username and password from the ‘VoipfoneClient::Configuration` class.
-
#parse_response(request) ⇒ Hash
Responses from the private Voipfone API are always in the form [“message”, content] We will strip the message (hopefully “OK”), raise if not OK, and return the content.
-
#phone_numbers ⇒ Array
Return the phone numbers for this account, as strings.
-
#voicemail ⇒ Hash
Return a list of voicemail entries, with details for each.
Constructor Details
#initialize ⇒ VoipfoneClient::Client
Intantiates a new Voipfone client, with username and password from the ‘VoipfoneClient::Configuration` class
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/voipfone_client/client.rb', line 6 def initialize() if VoipfoneClient.configuration.username.nil? || VoipfoneClient.configuration.password.nil? raise LoginCredentialsMissing, "You need to include a username and password to log in." end username = VoipfoneClient.configuration.username password = VoipfoneClient.configuration.password @browser = Mechanize.new login_url = "#{VoipfoneClient::BASE_URL}/login.php?method=process" @browser.post(login_url,{"hash" => "urlHash", "login" => username, "password" => password}) #We'll do a call to a cheap endpoint (the balance) and if it returns 'authfirst', there's a problem #with the username / password. Oddly it still returns 200 OK. if account_balance == "authfirst" raise NotAuthenticatedError, "Username or Password weren't accepted. You'll need to instantiate a new VoipfoneClient::Client object." end end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
3 4 5 |
# File 'lib/voipfone_client/client.rb', line 3 def browser @browser end |
Instance Method Details
#account_balance ⇒ Float
Return the balance of the account as a float.
4 5 6 7 |
# File 'lib/voipfone_client/account_balance.rb', line 4 def account_balance request = @browser.get("#{VoipfoneClient::API_GET_URL}?balance&builder") parse_response(request)["balance"] end |
#account_details ⇒ Hash
Returns of account details.
4 5 6 7 |
# File 'lib/voipfone_client/account_details.rb', line 4 def account_details request = @browser.get("#{VoipfoneClient::API_GET_URL}?account") parse_response(request) end |
#parse_response(request) ⇒ Hash
Responses from the private Voipfone API are always in the form [“message”, content] We will strip the message (hopefully “OK”), raise if not OK, and return the content.
26 27 28 29 30 31 32 |
# File 'lib/voipfone_client/client.rb', line 26 def parse_response(request) raw = JSON.parse(request.body) unless raw.first == "ok" || raw.first == "OK" raise VoipfoneAPIError, raw.first end raw.last end |
#phone_numbers ⇒ Array
Return the phone numbers for this account, as strings
5 6 7 8 |
# File 'lib/voipfone_client/phone_numbers.rb', line 5 def phone_numbers request = @browser.get("#{VoipfoneClient::API_GET_URL}?nums") parse_response(request)["nums"].collect {|n| n.first} end |
#voicemail ⇒ Hash
Return a list of voicemail entries, with details for each
4 5 6 7 |
# File 'lib/voipfone_client/voicemail.rb', line 4 def voicemail request = @browser.get("#{VoipfoneClient::API_GET_URL}?vm_view") parse_response(request)["vm_view"].first["voicemail"] end |