Skip to content

Commit

Permalink
Adding check credit, dummy test details, HTTP library.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfi committed May 8, 2012
1 parent 7521fd3 commit 6c8b37b
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
doc/ doc/
.yardoc/ .yardoc/
spec/spec_authentication_details*
8 changes: 8 additions & 0 deletions Gemfile
@@ -0,0 +1,8 @@
source 'https://rubygems.org'

gem 'nokogiri'
gem 'faraday'

group :development do
gem 'rspec'
end
24 changes: 24 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,24 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
faraday (0.8.0)
multipart-post (~> 1.1)
multipart-post (1.1.5)
nokogiri (1.5.2)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.0)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)

PLATFORMS
ruby

DEPENDENCIES
faraday
nokogiri
rspec
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -75,6 +75,19 @@ Send multiple SMS messages with advanced options set:
end end
end end


Test Setup
----------

First, create a file at spec/spec_authentication_details.rb containing the following:

34023ada3ec0d99213f91a12a2329ba932665ed7
MyLegacyAPIUsername@mydomain.com
MyPassword

Substitute your own API key, username and password on lines 1, 2, and 3 of the file.

Then, run `rspec`.

License License
------- -------


Expand Down
5 changes: 5 additions & 0 deletions lib/clockwork.rb
@@ -1,7 +1,12 @@
lib_dir = File.expand_path(File.dirname(__FILE__)) lib_dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include?(lib_dir) $LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include?(lib_dir)


require 'net/http'
require 'nokogiri'

require 'clockwork/errors' require 'clockwork/errors'
require 'clockwork/http'
require 'clockwork/xml/xml'


require 'clockwork/api' require 'clockwork/api'
require 'clockwork/sms' require 'clockwork/sms'
10 changes: 8 additions & 2 deletions lib/clockwork/api.rb
@@ -1,5 +1,8 @@
# A wrapper around the Clockwork API. # A wrapper around the Clockwork API.
module Clockwork module Clockwork

# Current API wrapper version
VERSION = 'DEV'


# @author James Inman <james@mediaburst.co.uk> # @author James Inman <james@mediaburst.co.uk>
# #
Expand All @@ -9,7 +12,7 @@ class API
# URL of the SMS API send action # URL of the SMS API send action
SMS_URL = "api.clockworksms.com/xml/send" SMS_URL = "api.clockworksms.com/xml/send"
# URL of the SMS API check credit action # URL of the SMS API check credit action
CREDIT_URL = "api.clockworksms.com/xml/credit" CREDIT_URL = "api.clockworksms.com/xml/credit"


# @!attribute from # @!attribute from
# The from address displayed on a phone when the SMS is received. This can be either a 12 digit number or 11 characters long. # The from address displayed on a phone when the SMS is received. This can be either a 12 digit number or 11 characters long.
Expand Down Expand Up @@ -83,7 +86,10 @@ def initialize *args
# @raise Clockwork::AuthenticationError - if API login details are incorrect # @raise Clockwork::AuthenticationError - if API login details are incorrect
# @return [integer] Number of messages remaining # @return [integer] Number of messages remaining
def credit def credit

xml = Clockwork::XML::Credit.build( self )
# TODO: Set @use_ssl here
response = Clockwork::HTTP.post( CREDIT_URL, xml, true )
credit = Clockwork::XML::Credit.parse( response )
end end


# Alias for Clockwork::API#credit to preserve backwards compatibility with original Mediaburst API. # Alias for Clockwork::API#credit to preserve backwards compatibility with original Mediaburst API.
Expand Down
9 changes: 9 additions & 0 deletions lib/clockwork/errors.rb
@@ -1,5 +1,14 @@
module Clockwork module Clockwork


# Raised if the entered authentication details (API key or username and password) are incorrect.
class AuthenticationError < StandardError
end

# Raised if a HTTP connection to the API fails
class HTTPError < StandardError
end

# Raised if the API key is in an invalid format.
class InvalidAPIKeyError < StandardError class InvalidAPIKeyError < StandardError
end end


Expand Down
43 changes: 43 additions & 0 deletions lib/clockwork/http.rb
@@ -0,0 +1,43 @@
module Clockwork

# Wrapper around NET/HTTP
class HTTP

# Build a HTTP POST request.
# @param [string] url URL to POST to
# @param [string] data Body of the POST request.
# @param [boolean] use_ssl Whether to use SSL when making the request.
# @return [string] XML data
def self.post url, data = '', use_ssl = true

if use_ssl
uri = URI.parse "https://#{url}"
req = Net::HTTP::Post.new( uri.path )

socket = Net::HTTP.new( uri.host, uri.port )
socket.use_ssl = true
socket.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
uri = URI.parse "http://#{url}"
req = Net::HTTP::Post.new( uri.path )

socket = Net::HTTP.new( uri.host, uri.port )
socket.use_ssl = true
socket.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

req.content_type = "text/xml"
# req.initialize_http_header( 'User-Agent' => "Clockwork .NET Wrapper/#{Clockwork::VERSION}" )
req.body = data

response = socket.start do |http|
http.request( req )
end

response

end

end

end
43 changes: 43 additions & 0 deletions lib/clockwork/xml/credit.rb
@@ -0,0 +1,43 @@
module Clockwork
module XML

# XML building and parsing for checking credit.
class Credit

# Build the XML data to check the credit from the XML API.
# @param [Clockwork::API] api Instance of Clockwork::API
# @return [string] XML data
def self.build api
if api.api_key
builder = Nokogiri::XML::Builder.new do |xml|
xml.Credit {
xml.Key api.api_key
}
end
else
builder = Nokogiri::XML::Builder.new do |xml|
xml.Credit {
xml.Username api.username
xml.Password api.password
}
end
end
builder.to_xml
end

# Parse the XML response
# @param [Net::HTTPResponse] api Instance of Clockwork::API
# @return [string] XML data
def self.parse response
if response.code.to_i == 200
doc = Nokogiri.parse( response.body )
doc.css('Credit').inner_html.to_i
else
raise Clockwork::HTTPError, "Could not connect to the Clockwork API to check credit."
end
end

end

end
end
16 changes: 16 additions & 0 deletions lib/clockwork/xml/xml.rb
@@ -0,0 +1,16 @@
require 'nokogiri'

module Clockwork

# Wrapper for the XML builder/parser
module XML
end

end

# Require everything in this directory
dir_path = File.dirname(__FILE__)
Dir["#{dir_path}/*.rb"].each do |file|
next if file =~ /xml.rb$/
require file
end

0 comments on commit 6c8b37b

Please sign in to comment.