Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[RELEASE] 1.1 Update to use API#balance.

  • Loading branch information...
commit 6774dfecdc845b0e84b620d3ec4601aeb49254e0 1 parent d5c32ad
James Inman jfi authored
20 README.md
Source Rendered
@@ -77,12 +77,12 @@ You should not use the `Clockwork::Message#deliver` method for each message, but
77 77 end
78 78 end
79 79
80   -### Check credit
  80 +### Check balance
81 81
82 82 require 'clockwork'
83 83 api = Clockwork::API.new( 'API_KEY_GOES_HERE' )
84   - remaining_messages = Clockwork::API.credit
85   - puts remaining messages # => 240
  84 + balance = Clockwork::API.balance
  85 + puts balance # => { :account_type => "PAYG", :balance => 575.23, :currency => { :code => "GBP", :symbol => "£" } }
86 86
87 87 ## License
88 88
@@ -105,7 +105,7 @@ First, create a file at spec/spec_authentication_details containing the followin
105 105
106 106 YOUR_API_KEY_HERE
107 107
108   -Substitute your own API key, username and password on lines 1, 2, and 3 of the file.
  108 +Substitute your own API key on line 1 of the file and replace the telephone numbers in the spec files with your own.
109 109
110 110 Then, run `rspec`.
111 111
@@ -114,4 +114,14 @@ Then, run `rspec`.
114 114 [3]: http://www.github.com/mediaburst/clockwork-ruby
115 115 [4]: http://rubydoc.info/github/mediaburst/clockwork-ruby/master/Clockwork/API
116 116 [5]: http://rubydoc.info/github/mediaburst/clockwork-ruby/master/Clockwork/SMS
117   -[6]: http://rubydoc.info/github/mediaburst/clockwork-ruby/master/Clockwork/SMS/Response
  117 +[6]: http://rubydoc.info/github/mediaburst/clockwork-ruby/master/Clockwork/SMS/Response
  118 +
  119 +## Changelog
  120 +
  121 +### 1.0.0 (18th July, 2012)
  122 +
  123 +* Initial release of wrapper [JI]
  124 +
  125 +### 1.1.0 (10th September, 2012)
  126 +
  127 +* API#credit is now deprecated: use API#balance instead.
15 lib/clockwork/api.rb
@@ -2,7 +2,7 @@
2 2 module Clockwork
3 3
4 4 # Current API wrapper version
5   - VERSION = '1.0.0'
  5 + VERSION = '1.1.0'
6 6
7 7 # @author James Inman <james@mediaburst.co.uk>
8 8 # You must create an instance of Clockwork::API to begin using the API.
@@ -11,7 +11,9 @@ class API
11 11 # URL of the SMS API send action
12 12 SMS_URL = "api.clockworksms.com/xml/send"
13 13 # URL of the SMS API check credit action
14   - CREDIT_URL = "api.clockworksms.com/xml/credit"
  14 + CREDIT_URL = "api.clockworksms.com/xml/credit"
  15 + # URL of the SMS API check balance action
  16 + BALANCE_URL = "api.clockworksms.com/xml/balance"
15 17
16 18 # API key provided in Clockwork::API#initialize.
17 19 # @return [string]
@@ -120,6 +122,15 @@ def credit
120 122 credit = Clockwork::XML::Credit.parse( response )
121 123 end
122 124
  125 + # Check the remaining credit for this account.
  126 + # @raise Clockwork::Error::Authentication - if API login details are incorrect
  127 + # @return [integer] Number of messages remaining
  128 + def balance
  129 + xml = Clockwork::XML::Balance.build( self )
  130 + response = Clockwork::HTTP.post( Clockwork::API::BALANCE_URL, xml, @use_ssl )
  131 + balance = Clockwork::XML::Balance.parse( response )
  132 + end
  133 +
123 134 # Deliver multiple messages created using Clockwork::API#messages.build.
124 135 # @return [array] Array of Clockwork::SMS::Response objects for messages.
125 136 def deliver
53 lib/clockwork/xml/balance.rb
... ... @@ -0,0 +1,53 @@
  1 +module Clockwork
  2 + module XML
  3 +
  4 + # @author James Inman <james@mediaburst.co.uk>
  5 + # XML building and parsing for checking balance.
  6 + class Balance
  7 +
  8 + # Build the XML data to check the balance from the XML API.
  9 + # @param [Clockwork::API] api Instance of Clockwork::API
  10 + # @return [string] XML data
  11 + def self.build api
  12 + builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
  13 + xml.Balance {
  14 + if api.api_key
  15 + xml.Key api.api_key
  16 + else
  17 + xml.Username api.username
  18 + xml.Password api.password
  19 + end
  20 + }
  21 + end
  22 + builder.to_xml
  23 + end
  24 +
  25 + # Parse the XML response.
  26 + # @param [Net::HTTPResponse] response Instance of Net::HTTPResponse
  27 + # @raise Clockwork:HTTPError - if a connection to the Clockwork API cannot be made
  28 + # @raise Clockwork::Error::Generic - if the API returns an error code other than 2
  29 + # @raise Clockwork::Error::Authentication - if API login details are incorrect
  30 + # @return [string] Number of remaining credits
  31 + def self.parse response
  32 + if response.code.to_i == 200
  33 + doc = Nokogiri.parse( response.body )
  34 + if doc.css('ErrDesc').empty?
  35 + hsh = {}
  36 + hsh[:account_type] = doc.css('Balance_Resp').css('AccountType').inner_html
  37 + hsh[:balance] = doc.css('Balance_Resp').css('Balance').inner_html.to_f
  38 + hsh[:currency] = { :code => doc.css('Balance_Resp').css('Currency').css('Code').inner_html, :symbol => doc.css('Balance_Resp').css('Currency').css('Symbol').inner_html }
  39 + hsh
  40 + elsif doc.css('ErrNo').inner_html.to_i == 2
  41 + raise Clockwork::Error::Authentication, doc.css('ErrDesc').inner_html
  42 + else
  43 + raise Clockwork::Error::Generic, doc.css('ErrDesc').inner_html
  44 + end
  45 + else
  46 + raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to check balance."
  47 + end
  48 + end
  49 +
  50 + end
  51 +
  52 + end
  53 +end
34 spec/api_spec.rb
@@ -72,26 +72,38 @@
72 72 api = Clockwork::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1'
73 73 expect { api.credit }.to raise_error Clockwork::Error::Generic
74 74 end
75   -
76   - end
77   -
78   - describe "#get_credit" do
79   -
80   - it "should return the number of messages remaining with an API key" do
81   - api = Clockwork::API.new test_api_key
82   - api.get_credit.should be > 0
83   - end
84 75
85 76 it "should return the number of messages remaining over standard HTTP" do
86 77 api = Clockwork::API.new test_api_key
87 78 api.use_ssl = false
88   - api.get_credit.should be > 0
  79 + api.credit.should be > 0
89 80 end
  81 +
  82 + end
  83 +
  84 + describe "#balance" do
  85 +
  86 + it "should return the balance remaining with an API key" do
  87 + api = Clockwork::API.new test_api_key
  88 + balance = api.balance
  89 + balance.should have_key :account_type
  90 + balance.should have_key :balance
  91 + balance.should have_key :currency
  92 + end
90 93
91 94 it "should raise an error with an invalid API key" do
92 95 api = Clockwork::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1'
93   - expect { api.get_credit }.to raise_error Clockwork::Error::Generic
  96 + expect { api.balance }.to raise_error Clockwork::Error::Generic
94 97 end
  98 +
  99 + it "should return the balance over standard HTTP" do
  100 + api = Clockwork::API.new test_api_key
  101 + api.use_ssl = false
  102 + balance = api.balance
  103 + balance.should have_key :account_type
  104 + balance.should have_key :balance
  105 + balance.should have_key :currency
  106 + end
95 107
96 108 end
97 109

0 comments on commit 6774dfe

Please sign in to comment.
Something went wrong with that request. Please try again.