Class: VoipfoneClient::Client

Inherits:
Object
  • Object
show all
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

DivertListItem, GlobalDivert, RegisteredMobile, SMS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVoipfoneClient::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
	 = "#{VoipfoneClient::BASE_URL}/login.php?method=process"
	@browser.post(,{"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  == "authfirst"
		raise NotAuthenticatedError, "Username or Password weren't accepted. You'll need to instantiate a new VoipfoneClient::Client object."
	end
end

Instance Attribute Details

#browserObject (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_balanceFloat

Return the balance of the account as a float.

Returns:

  • (Float)

    Account balance as a float. Should be rounded to 2dp before presentation.



4
5
6
7
# File 'lib/voipfone_client/account_balance.rb', line 4

def 
  request = @browser.get("#{VoipfoneClient::API_GET_URL}?balance&builder")
  parse_response(request)["balance"]
end

#account_detailsHash

Returns of account details.

Returns:

  • (Hash)

    of account details



4
5
6
7
# File 'lib/voipfone_client/account_details.rb', line 4

def 
	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.

Parameters:

  • request (JSON)

    The raw request response from the Voipfone API

Returns:

  • (Hash)

    the parsed JSON



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_numbersArray

Return the phone numbers for this account, as strings

Returns:

  • (Array)

    Phone numbers 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

#voicemailHash

Return a list of voicemail entries, with details for each

Returns:

  • (Hash)

    of voicemail information. This includes a reference to a WAV file which isn’t accessible (yet?)



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