diff --git a/README.md b/README.md index 8fb6e74..ea42e8a 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,15 @@ Full documentation is at [http://rubydoc.info/github/mediaburst/clockwork-ruby/m ## Usage -For more information on the available optional parameters for the API (Clockwork::API), see [here][4]. +For more information on the available optional parameters for the API (ClockworkSMS::API), see [here][4]. -For more information on the available optional parameters for each SMS (Clockwork::SMS), see [here][5]. For more information on the response object returned from each SMS (Clockwork::SMS::Response), see [here][6]. +For more information on the available optional parameters for each SMS (ClockworkSMS::SMS), see [here][5]. For more information on the response object returned from each SMS (ClockworkSMS::SMS::Response), see [here][6]. ### Send a single SMS message ```ruby require 'clockwork' -api = Clockwork::API.new( 'API_KEY_GOES_HERE' ) +api = ClockworkSMS::API.new( 'API_KEY_GOES_HERE' ) message = api.messages.build( :to => '441234123456', :content => 'This is a test message.' ) response = message.deliver @@ -36,7 +36,7 @@ end ```ruby require 'clockwork' -api = Clockwork::API.new( 'API_KEY_GOES_HERE' ) +api = ClockworkSMS::API.new( 'API_KEY_GOES_HERE' ) message = api.messages.build message.to = '441234123456' @@ -53,7 +53,7 @@ end ### Send multiple SMS messages (with an optional client ID) -You should not use the `Clockwork::Message#deliver` method for each message, but instead use the `Clockwork::API#deliver` method to send multiple messages in the same API request. This will decrease load on the API and ensure your requests are processed significantly faster. +You should not use the `ClockworkSMS::Message#deliver` method for each message, but instead use the `ClockworkSMS::API#deliver` method to send multiple messages in the same API request. This will decrease load on the API and ensure your requests are processed significantly faster. ```ruby @@ -67,7 +67,7 @@ messages = [ ] require 'clockwork' -api = Clockwork::API.new( 'API_KEY_GOES_HERE' ) +api = ClockworkSMS::API.new( 'API_KEY_GOES_HERE' ) messages.each do |m| api.messages.build(m) end @@ -88,8 +88,8 @@ end ```ruby require 'clockwork' -api = Clockwork::API.new( 'API_KEY_GOES_HERE' ) -balance = Clockwork::API.balance +api = ClockworkSMS::API.new( 'API_KEY_GOES_HERE' ) +balance = ClockworkSMS::API.balance puts balance # => { :account_type => "PAYG", :balance => 575.23, :currency => { :code => "GBP", :symbol => "£" } } ``` diff --git a/lib/clockwork.rb b/lib/clockwork.rb deleted file mode 100644 index e7cd402..0000000 --- a/lib/clockwork.rb +++ /dev/null @@ -1,16 +0,0 @@ -lib_dir = File.expand_path(File.dirname(__FILE__)) -$LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include?(lib_dir) - -require 'net/http' -require 'net/https' -require 'openssl' -require 'nokogiri' - -require 'clockwork/error' -require 'clockwork/http' -require 'clockwork/xml/xml' - -require 'clockwork/message_collection' -require 'clockwork/api' -require 'clockwork/sms' -require 'clockwork/sms/response' \ No newline at end of file diff --git a/lib/clockwork/message_collection.rb b/lib/clockwork/message_collection.rb deleted file mode 100644 index 88d3351..0000000 --- a/lib/clockwork/message_collection.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Clockwork - - # @author James Inman - # Use an instance of Clockwork::API#messages.build to create SMS messages. - class MessageCollection - - # @!attribute api - # An instance of Clockwork::API. - # @return [Clockwork::API] - attr_accessor :api - - # @!attribute messages - # An array of Clockwork::SMS messages. - # @return [array] - attr_accessor :messages - - # @param [hash] options Hash of attributes which must include an instance of Clockwork::API - # @raise ArgumentError - if a valid instance of API is not passed as :api in options - # Create a new instance of Clockwork::MessageCollection. - def initialize options - @api = options[:api] - raise ArgumentError, "Clockwork::MessageCollection#new must include an instance of Clockwork::API as :api" unless @api.kind_of?(Clockwork::API) - - @messages = [] - end - - # @param [hash] params Hash of parameters as attributes on Clockwork::SMS - # @see Clockwork::SMS - # Create a new instance of Clockwork::SMS in this MessageCollection. - def build params = {} - sms = Clockwork::SMS.new({ :api => @api }.merge(params)) - sms.wrapper_id = @messages.count - @messages << sms - sms - end - - end - -end diff --git a/lib/clockwork_sms.rb b/lib/clockwork_sms.rb new file mode 100644 index 0000000..6dbf44d --- /dev/null +++ b/lib/clockwork_sms.rb @@ -0,0 +1,16 @@ +lib_dir = File.expand_path(File.dirname(__FILE__)) +$LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include?(lib_dir) + +require 'net/http' +require 'net/https' +require 'openssl' +require 'nokogiri' + +require 'clockwork_sms/error' +require 'clockwork_sms/http' +require 'clockwork_sms/xml/xml' + +require 'clockwork_sms/message_collection' +require 'clockwork_sms/api' +require 'clockwork_sms/sms' +require 'clockwork_sms/sms/response' \ No newline at end of file diff --git a/lib/clockwork/api.rb b/lib/clockwork_sms/api.rb similarity index 59% rename from lib/clockwork/api.rb rename to lib/clockwork_sms/api.rb index 41a6766..4b3e204 100644 --- a/lib/clockwork/api.rb +++ b/lib/clockwork_sms/api.rb @@ -1,11 +1,11 @@ -# A wrapper around the Clockwork API. -module Clockwork +# A wrapper around the ClockworkSMS API. +module ClockworkSMS # Current API wrapper version VERSION = '1.2.1' # @author James Inman - # You must create an instance of Clockwork::API to begin using the API. + # You must create an instance of ClockworkSMS::API to begin using the API. class API # URL of the SMS API send action @@ -15,40 +15,40 @@ class API # URL of the SMS API check balance action BALANCE_URL = "api.clockworksms.com/xml/balance" - # API key provided in Clockwork::API#initialize. + # API key provided in ClockworkSMS::API#initialize. # @return [string] attr_reader :api_key # This is the number of messages to concatenate (join together). # @raise ArgumentError - if a value less than 2 or more than 3 is passed # @return [symbol] One of +error+, +:replace+, +:remove+ - # @note Defaults to 3 if not set and Clockwork::API#long is set to +true+. + # @note Defaults to 3 if not set and ClockworkSMS::API#long is set to +true+. attr_reader :concat # @!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. # @return [string] - # @note This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used. + # @note This can be overriden for specific ClockworkSMS::SMS objects; if it is not set your account default will be used. attr_accessor :from - # What to do with any invalid characters in the message content. +:error+ will raise a Clockwork::InvalidCharacterException, +:replace+ will replace a small number of common invalid characters, such as the smart quotes used by Microsoft Office with a similar match, +:remove+ will remove invalid characters. + # What to do with any invalid characters in the message content. +:error+ will raise a ClockworkSMS::InvalidCharacterException, +:replace+ will replace a small number of common invalid characters, such as the smart quotes used by Microsoft Office with a similar match, +:remove+ will remove invalid characters. # @raise ArgumentError - if value is not one of +:error+, +:replace+, +:remove+ # @return [symbol] One of +error+, +:replace+, +:remove+ - # @note This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used. + # @note This can be overriden for specific ClockworkSMS::SMS objects; if it is not set your account default will be used. attr_reader :invalid_char_action # @!attribute long # Set to +true+ to enable long SMS. A standard text can contain 160 characters, a long SMS supports up to 459. Each recipient will cost up to 3 message credits. # @return [boolean] - # @note This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used. + # @note This can be overriden for specific ClockworkSMS::SMS objects; if it is not set your account default will be used. attr_accessor :long # @!attribute messages - # Returns a Clockwork::MessageCollection containing all built SMS messages. - # @return [Clockwork::MessageCollection] + # Returns a ClockworkSMS::MessageCollection containing all built SMS messages. + # @return [ClockworkSMS::MessageCollection] attr_reader :messages - # Password provided in Clockwork::API#initialize. + # Password provided in ClockworkSMS::API#initialize. # @return [string] # @deprecated Use api_key instead. attr_reader :password @@ -56,7 +56,7 @@ class API # @!attribute truncate # Set to +true+ to trim the message content to the maximum length if it is too long. # @return [boolean] - # @note This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used. + # @note This can be overriden for specific ClockworkSMS::SMS objects; if it is not set your account default will be used. attr_accessor :truncate # Whether to use SSL when connecting to the API. @@ -64,13 +64,13 @@ class API # @return [boolean] attr_accessor :use_ssl - # Username provided in Clockwork::API#initialize. + # Username provided in ClockworkSMS::API#initialize. # @return [string] # @deprecated Use api_key instead. attr_reader :username - # Alias for Clockwork::API#credit to preserve backwards compatibility with original Mediaburst API. - # @deprecated Use Clockwork::API#credit. Support for Clockwork::API#get_credit will be removed in a future version of this wrapper. + # Alias for ClockworkSMS::API#credit to preserve backwards compatibility with original Mediaburst API. + # @deprecated Use ClockworkSMS::API#credit. Support for ClockworkSMS::API#get_credit will be removed in a future version of this wrapper. def get_credit credit end @@ -93,11 +93,11 @@ def concat= number # @param [hash] options Optional hash of attributes on API # @deprecated Use an API key instead. Support for usernames and passwords will be removed in a future version of this wrapper. # @raise ArgumentError - if more than 3 parameters are passed - # @raise Clockwork::Error::InvalidAPIKey - if API key is invalid - # Clockwork::API is initialized with an API key, available from http://www.mediaburst.co.uk/api. + # @raise ClockworkSMS::Error::InvalidAPIKey - if API key is invalid + # ClockworkSMS::API is initialized with an API key, available from http://www.mediaburst.co.uk/api. def initialize *args if args.size == 1 || ( args.size == 2 && args[1].kind_of?(Hash) ) - raise Clockwork::Error::InvalidAPIKey unless args[0][/^[A-Fa-f0-9]{40}$/] + raise ClockworkSMS::Error::InvalidAPIKey unless args[0][/^[A-Fa-f0-9]{40}$/] @api_key = args[0] elsif args.size == 2 || ( args.size == 3 && args[2].kind_of?(Hash) ) raise ArgumentError, "You must pass both a username and password." if args[0].empty? || args[1].empty? @@ -111,35 +111,35 @@ def initialize *args @use_ssl = true if @use_ssl.nil? @concat = 3 if @concat.nil? && @long - @messages ||= Clockwork::MessageCollection.new( :api => self ) + @messages ||= ClockworkSMS::MessageCollection.new( :api => self ) @invalid_char_action = [nil, :error, :remove, :replace].index(@invalid_char_action) if @invalid_char_action @truncate = @truncate ? true : false unless @truncate.nil? end # Check the remaining credit for this account. - # @raise Clockwork::Error::Authentication - if API login details are incorrect + # @raise ClockworkSMS::Error::Authentication - if API login details are incorrect # @return [integer] Number of messages remaining def credit - xml = Clockwork::XML::Credit.build( self ) - response = Clockwork::HTTP.post( Clockwork::API::CREDIT_URL, xml, @use_ssl ) - credit = Clockwork::XML::Credit.parse( response ) + xml = ClockworkSMS::XML::Credit.build( self ) + response = ClockworkSMS::HTTP.post( ClockworkSMS::API::CREDIT_URL, xml, @use_ssl ) + credit = ClockworkSMS::XML::Credit.parse( response ) end # Check the remaining credit for this account. - # @raise Clockwork::Error::Authentication - if API login details are incorrect + # @raise ClockworkSMS::Error::Authentication - if API login details are incorrect # @return [integer] Number of messages remaining def balance - xml = Clockwork::XML::Balance.build( self ) - response = Clockwork::HTTP.post( Clockwork::API::BALANCE_URL, xml, @use_ssl ) - balance = Clockwork::XML::Balance.parse( response ) + xml = ClockworkSMS::XML::Balance.build( self ) + response = ClockworkSMS::HTTP.post( ClockworkSMS::API::BALANCE_URL, xml, @use_ssl ) + balance = ClockworkSMS::XML::Balance.parse( response ) end - # Deliver multiple messages created using Clockwork::API#messages.build. - # @return [array] Array of Clockwork::SMS::Response objects for messages. + # Deliver multiple messages created using ClockworkSMS::API#messages.build. + # @return [array] Array of ClockworkSMS::SMS::Response objects for messages. def deliver - xml = Clockwork::XML::SMS.build_multiple( self.messages ) - http_response = Clockwork::HTTP.post( Clockwork::API::SMS_URL, xml, @use_ssl ) - responses = Clockwork::XML::SMS.parse_multiple( self.messages, http_response ) + xml = ClockworkSMS::XML::SMS.build_multiple( self.messages ) + http_response = ClockworkSMS::HTTP.post( ClockworkSMS::API::SMS_URL, xml, @use_ssl ) + responses = ClockworkSMS::XML::SMS.parse_multiple( self.messages, http_response ) end end diff --git a/lib/clockwork/error.rb b/lib/clockwork_sms/error.rb similarity index 86% rename from lib/clockwork/error.rb rename to lib/clockwork_sms/error.rb index 6a4266f..ca6bc75 100644 --- a/lib/clockwork/error.rb +++ b/lib/clockwork_sms/error.rb @@ -1,6 +1,6 @@ -module Clockwork +module ClockworkSMS - # Module containing Clockwork::Error classes, all of which extend StandardError. + # Module containing ClockworkSMS::Error classes, all of which extend StandardError. module Error # @author James Inman diff --git a/lib/clockwork/http.rb b/lib/clockwork_sms/http.rb similarity index 90% rename from lib/clockwork/http.rb rename to lib/clockwork_sms/http.rb index d1811b4..0a4293f 100644 --- a/lib/clockwork/http.rb +++ b/lib/clockwork_sms/http.rb @@ -1,4 +1,4 @@ -module Clockwork +module ClockworkSMS # @author James Inman # Wrapper around NET/HTTP @@ -26,7 +26,7 @@ def self.post url, data = '', use_ssl = true req.content_type = "text/xml" req.body = data - req.add_field "User-Agent", "Clockwork Ruby Wrapper/#{Clockwork::VERSION}" + req.add_field "User-Agent", "ClockworkSMS Ruby Wrapper/#{ClockworkSMS::VERSION}" response = socket.start do |http| http.request( req ) diff --git a/lib/clockwork_sms/message_collection.rb b/lib/clockwork_sms/message_collection.rb new file mode 100644 index 0000000..31c1b00 --- /dev/null +++ b/lib/clockwork_sms/message_collection.rb @@ -0,0 +1,39 @@ +module ClockworkSMS + + # @author James Inman + # Use an instance of ClockworkSMS::API#messages.build to create SMS messages. + class MessageCollection + + # @!attribute api + # An instance of ClockworkSMS::API. + # @return [ClockworkSMS::API] + attr_accessor :api + + # @!attribute messages + # An array of ClockworkSMS::SMS messages. + # @return [array] + attr_accessor :messages + + # @param [hash] options Hash of attributes which must include an instance of ClockworkSMS::API + # @raise ArgumentError - if a valid instance of API is not passed as :api in options + # Create a new instance of ClockworkSMS::MessageCollection. + def initialize options + @api = options[:api] + raise ArgumentError, "ClockworkSMS::MessageCollection#new must include an instance of ClockworkSMS::API as :api" unless @api.kind_of?(ClockworkSMS::API) + + @messages = [] + end + + # @param [hash] params Hash of parameters as attributes on ClockworkSMS::SMS + # @see ClockworkSMS::SMS + # Create a new instance of ClockworkSMS::SMS in this MessageCollection. + def build params = {} + sms = ClockworkSMS::SMS.new({ :api => @api }.merge(params)) + sms.wrapper_id = @messages.count + @messages << sms + sms + end + + end + +end diff --git a/lib/clockwork/sms.rb b/lib/clockwork_sms/sms.rb similarity index 74% rename from lib/clockwork/sms.rb rename to lib/clockwork_sms/sms.rb index d75d16f..4c03ae8 100644 --- a/lib/clockwork/sms.rb +++ b/lib/clockwork_sms/sms.rb @@ -1,12 +1,12 @@ -module Clockwork +module ClockworkSMS # @author James Inman - # Create an instance of Clockwork::SMS for each SMS message you want to send. + # Create an instance of ClockworkSMS::SMS for each SMS message you want to send. class SMS # @!attribute api - # An instance of Clockwork::API. - # @return [Clockwork::API] + # An instance of ClockworkSMS::API. + # @return [ClockworkSMS::API] attr_accessor :api # @!attribute content @@ -22,25 +22,25 @@ class SMS # @!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. # @return [string] - # @note If this option is set it overrides the global option specified in Clockwork::API for this SMS, if neither option is set your account default will be used. + # @note If this option is set it overrides the global option specified in ClockworkSMS::API for this SMS, if neither option is set your account default will be used. attr_accessor :from # @!attribute long # Set to +true+ to enable long SMS. A standard text can contain 160 characters, a long SMS supports up to 459. Each recipient will cost up to 3 message credits. # @return [boolean] - # @note If this option is set it overrides the global option specified in Clockwork::API for this SMS, if neither option is set your account default will be used. + # @note If this option is set it overrides the global option specified in ClockworkSMS::API for this SMS, if neither option is set your account default will be used. attr_accessor :long # @!attribute truncate # Set to +true+ to trim the message content to the maximum length if it is too long. # @return [boolean] - # @note If this option is set it overrides the global option specified in Clockwork::API for this SMS, if neither option is set your account default will be used. + # @note If this option is set it overrides the global option specified in ClockworkSMS::API for this SMS, if neither option is set your account default will be used. attr_accessor :truncate - # What to do with any invalid characters in the message content. +:error+ will raise a Clockwork::InvalidCharacterException, +:replace+ will replace a small number of common invalid characters, such as the smart quotes used by Microsoft Office with a similar match, +:remove+ will remove invalid characters. + # What to do with any invalid characters in the message content. +:error+ will raise a ClockworkSMS::InvalidCharacterException, +:replace+ will replace a small number of common invalid characters, such as the smart quotes used by Microsoft Office with a similar match, +:remove+ will remove invalid characters. # @raise ArgumentError - if value is not one of +:error+, +:replace+, +:remove+ # @return [symbol] One of +error+, +:replace+, +:remove+ - # @note If this option is set it overrides the global option specified in Clockwork::API for this SMS, if neither option is set your account default will be used. + # @note If this option is set it overrides the global option specified in ClockworkSMS::API for this SMS, if neither option is set your account default will be used. attr_reader :invalid_char_action # @!attribute to @@ -54,7 +54,7 @@ def invalid_char_action= symbol raise( ArgumentError, "#{symbol} must be one of :error, :replace, :remove" ) unless [:error, :remove, :replace].include?(symbol.to_sym) end - # @param [hash] options Optional hash of attributes on Clockwork::SMS + # @param [hash] options Optional hash of attributes on ClockworkSMS::SMS # Create a new SMS message. def initialize options = {} options.each { |k, v| instance_variable_set "@#{k}", v } if options.kind_of?(Hash) @@ -63,14 +63,14 @@ def initialize options = {} end # Deliver the SMS message. - # @return [Clockwork::SMS::Response] An instance of Clockwork::SMS::Response + # @return [ClockworkSMS::SMS::Response] An instance of ClockworkSMS::SMS::Response def deliver - xml = Clockwork::XML::SMS.build_single( self ) - http_response = Clockwork::HTTP.post( Clockwork::API::SMS_URL, xml, @api.use_ssl ) - response = Clockwork::XML::SMS.parse_single( self, http_response ) + xml = ClockworkSMS::XML::SMS.build_single( self ) + http_response = ClockworkSMS::HTTP.post( ClockworkSMS::API::SMS_URL, xml, @api.use_ssl ) + response = ClockworkSMS::XML::SMS.parse_single( self, http_response ) end - # Translate standard variable names to those needed to make an XML request. First, checks if global variables are set in Clockwork::API then overwrites with variables set in Clockwork::SMS instance. + # Translate standard variable names to those needed to make an XML request. First, checks if global variables are set in ClockworkSMS::API then overwrites with variables set in ClockworkSMS::SMS instance. # @return [hash] Hash of XML keys and values def translated_attributes attributes = {} diff --git a/lib/clockwork/sms/response.rb b/lib/clockwork_sms/sms/response.rb similarity index 80% rename from lib/clockwork/sms/response.rb rename to lib/clockwork_sms/sms/response.rb index a0d67c6..dc32637 100644 --- a/lib/clockwork/sms/response.rb +++ b/lib/clockwork_sms/sms/response.rb @@ -1,8 +1,8 @@ -module Clockwork +module ClockworkSMS class SMS # @author James Inman - # A Clockwork::SMS::Response is returned for each SMS sent by Clockwork::SMS#deliver. + # A ClockworkSMS::SMS::Response is returned for each SMS sent by ClockworkSMS::SMS#deliver. class Response # @!attribute error_code @@ -21,8 +21,8 @@ class Response attr_accessor :message_id # @!attribute message - # The instance of Clockwork::SMS relating to this response. - # @return [Clockwork::SMS] + # The instance of ClockworkSMS::SMS relating to this response. + # @return [ClockworkSMS::SMS] attr_accessor :message # @!attribute success diff --git a/lib/clockwork/xml/balance.rb b/lib/clockwork_sms/xml/balance.rb similarity index 69% rename from lib/clockwork/xml/balance.rb rename to lib/clockwork_sms/xml/balance.rb index b0eb5e4..ae3dc7a 100644 --- a/lib/clockwork/xml/balance.rb +++ b/lib/clockwork_sms/xml/balance.rb @@ -1,4 +1,4 @@ -module Clockwork +module ClockworkSMS module XML # @author James Inman @@ -6,7 +6,7 @@ module XML class Balance # Build the XML data to check the balance from the XML API. - # @param [Clockwork::API] api Instance of Clockwork::API + # @param [ClockworkSMS::API] api Instance of ClockworkSMS::API # @return [string] XML data def self.build api builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| @@ -24,9 +24,9 @@ def self.build api # Parse the XML response. # @param [Net::HTTPResponse] response Instance of Net::HTTPResponse - # @raise Clockwork:HTTPError - if a connection to the Clockwork API cannot be made - # @raise Clockwork::Error::Generic - if the API returns an error code other than 2 - # @raise Clockwork::Error::Authentication - if API login details are incorrect + # @raise ClockworkSMS:HTTPError - if a connection to the ClockworkSMS API cannot be made + # @raise ClockworkSMS::Error::Generic - if the API returns an error code other than 2 + # @raise ClockworkSMS::Error::Authentication - if API login details are incorrect # @return [string] Number of remaining credits def self.parse response if response.code.to_i == 200 @@ -38,12 +38,12 @@ def self.parse response hsh[:currency] = { :code => doc.css('Balance_Resp').css('Currency').css('Code').inner_html, :symbol => doc.css('Balance_Resp').css('Currency').css('Symbol').inner_html } hsh elsif doc.css('ErrNo').inner_html.to_i == 2 - raise Clockwork::Error::Authentication, doc.css('ErrDesc').inner_html + raise ClockworkSMS::Error::Authentication, doc.css('ErrDesc').inner_html else - raise Clockwork::Error::Generic, doc.css('ErrDesc').inner_html + raise ClockworkSMS::Error::Generic, doc.css('ErrDesc').inner_html end else - raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to check balance." + raise ClockworkSMS::Error::HTTP, "Could not connect to the ClockworkSMS API to check balance." end end diff --git a/lib/clockwork/xml/credit.rb b/lib/clockwork_sms/xml/credit.rb similarity index 63% rename from lib/clockwork/xml/credit.rb rename to lib/clockwork_sms/xml/credit.rb index ce4ca40..a80d64a 100644 --- a/lib/clockwork/xml/credit.rb +++ b/lib/clockwork_sms/xml/credit.rb @@ -1,4 +1,4 @@ -module Clockwork +module ClockworkSMS module XML # @author James Inman @@ -6,7 +6,7 @@ module XML class Credit # Build the XML data to check the credit from the XML API. - # @param [Clockwork::API] api Instance of Clockwork::API + # @param [ClockworkSMS::API] api Instance of ClockworkSMS::API # @return [string] XML data def self.build api builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| @@ -24,9 +24,9 @@ def self.build api # Parse the XML response. # @param [Net::HTTPResponse] response Instance of Net::HTTPResponse - # @raise Clockwork:HTTPError - if a connection to the Clockwork API cannot be made - # @raise Clockwork::Error::Generic - if the API returns an error code other than 2 - # @raise Clockwork::Error::Authentication - if API login details are incorrect + # @raise ClockworkSMS:HTTPError - if a connection to the ClockworkSMS API cannot be made + # @raise ClockworkSMS::Error::Generic - if the API returns an error code other than 2 + # @raise ClockworkSMS::Error::Authentication - if API login details are incorrect # @return [string] Number of remaining credits def self.parse response if response.code.to_i == 200 @@ -34,12 +34,12 @@ def self.parse response if doc.css('ErrDesc').empty? doc.css('Credit').inner_html.to_i elsif doc.css('ErrNo').inner_html.to_i == 2 - raise Clockwork::Error::Authentication, doc.css('ErrDesc').inner_html + raise ClockworkSMS::Error::Authentication, doc.css('ErrDesc').inner_html else - raise Clockwork::Error::Generic, doc.css('ErrDesc').inner_html + raise ClockworkSMS::Error::Generic, doc.css('ErrDesc').inner_html end else - raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to check credit." + raise ClockworkSMS::Error::HTTP, "Could not connect to the ClockworkSMS API to check credit." end end diff --git a/lib/clockwork/xml/sms.rb b/lib/clockwork_sms/xml/sms.rb similarity index 76% rename from lib/clockwork/xml/sms.rb rename to lib/clockwork_sms/xml/sms.rb index 13e7cec..fc88318 100644 --- a/lib/clockwork/xml/sms.rb +++ b/lib/clockwork_sms/xml/sms.rb @@ -1,4 +1,4 @@ -module Clockwork +module ClockworkSMS module XML # @author James Inman @@ -6,7 +6,7 @@ module XML class SMS # Build the XML data to send a single SMS using the XML API. - # @param [Clockwork::SMS] sms Instance of Clockwork::SMS + # @param [ClockworkSMS::SMS] sms Instance of ClockworkSMS::SMS # @return [string] XML data def self.build_single sms builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| @@ -28,7 +28,7 @@ def self.build_single sms end # Build the XML data to send multiple SMS messages using the XML API. - # @param [Clockwork::MessageCollection] collection Instance of Clockwork::SMS + # @param [ClockworkSMS::MessageCollection] collection Instance of ClockworkSMS::SMS # @return [string] XML data def self.build_multiple collection builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| @@ -52,12 +52,12 @@ def self.build_multiple collection end # Parse the XML data from a single SMS response from the XML API. - # @param [Clockwork::SMS] sms Instance of Clockwork::SMS + # @param [ClockworkSMS::SMS] sms Instance of ClockworkSMS::SMS # @param [Net::HTTPResponse] http_response Instance of Net:HTTPResponse - # @raise Clockwork:HTTPError - if a connection to the Clockwork API cannot be made - # @return [Clockwork::SMS::Response] Instance of Clockwork::SMS::Response for the message + # @raise ClockworkSMS:HTTPError - if a connection to the ClockworkSMS API cannot be made + # @return [ClockworkSMS::SMS::Response] Instance of ClockworkSMS::SMS::Response for the message def self.parse_single sms, http_response - response = Clockwork::SMS::Response.new + response = ClockworkSMS::SMS::Response.new response.message = sms if http_response.code.to_i == 200 @@ -71,28 +71,28 @@ def self.parse_single sms, http_response response.error_description = doc.css('ErrDesc').inner_html end else - raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to send SMS." + raise ClockworkSMS::Error::HTTP, "Could not connect to the ClockworkSMS API to send SMS." end response end # Parse the XML data from a multiple SMS response from the XML API. - # @param [Clockwork::MessageCollection] collection Instance of Clockwork::SMS + # @param [ClockworkSMS::MessageCollection] collection Instance of ClockworkSMS::SMS # @param [Net::HTTPResponse] http_response Instance of Net:HTTPResponse - # @raise Clockwork:HTTPError - if a connection to the Clockwork API cannot be made - # @return [array] Array of Clockwork::SMS::Response objects relating to the messages + # @raise ClockworkSMS:HTTPError - if a connection to the ClockworkSMS API cannot be made + # @return [array] Array of ClockworkSMS::SMS::Response objects relating to the messages def self.parse_multiple collection, http_response responses = [] if http_response.code.to_i == 200 doc = Nokogiri.parse( http_response.body ) else - raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to send SMS." + raise ClockworkSMS::Error::HTTP, "Could not connect to the ClockworkSMS API to send SMS." end doc.css('SMS_Resp').each_with_index do |sms_response, i| - response = Clockwork::SMS::Response.new + response = ClockworkSMS::SMS::Response.new response.message = collection.messages[i] if sms_response.css('ErrDesc').empty? response.success = true diff --git a/lib/clockwork/xml/xml.rb b/lib/clockwork_sms/xml/xml.rb similarity index 93% rename from lib/clockwork/xml/xml.rb rename to lib/clockwork_sms/xml/xml.rb index 242ffbd..4444a2f 100644 --- a/lib/clockwork/xml/xml.rb +++ b/lib/clockwork_sms/xml/xml.rb @@ -1,6 +1,6 @@ require 'nokogiri' -module Clockwork +module ClockworkSMS # @author James Inman # Wrapper for the XML builder/parser diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 26b4d04..a7fff47 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -6,11 +6,11 @@ describe "#initialize" do - it "should raise a Clockwork::Error::InvalidAPIKey if no API key is passed" do - expect { Clockwork::API.new('') }.to raise_error Clockwork::Error::InvalidAPIKey + it "should raise a ClockworkSMS::Error::InvalidAPIKey if no API key is passed" do + expect { ClockworkSMS::API.new('') }.to raise_error ClockworkSMS::Error::InvalidAPIKey end - it "should raise a Clockwork::Error::InvalidAPIKey if an invalid format of API key is passed" do + it "should raise a ClockworkSMS::Error::InvalidAPIKey if an invalid format of API key is passed" do invalid_keys = %w{ q4353q345325325432 vsdfgihet8f7yi4u7ttf4guyi @@ -19,22 +19,22 @@ } invalid_keys.each do |k| - expect { Clockwork::API.new(k) }.to raise_error Clockwork::Error::InvalidAPIKey + expect { ClockworkSMS::API.new(k) }.to raise_error ClockworkSMS::Error::InvalidAPIKey end end - it "should return a valid instance of Clockwork::API if a valid format of API key is passed" do - api = Clockwork::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875' - expect(api).to be_a_kind_of Clockwork::API + it "should return a valid instance of ClockworkSMS::API if a valid format of API key is passed" do + api = ClockworkSMS::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875' + expect(api).to be_a_kind_of ClockworkSMS::API expect(api.api_key).to match('af7a8f7a8fa7f8a76fa876fa876a876fa875a875') end it "should raise an ArgumentError if more than three parameters are passed" do - expect { Clockwork::API.new('', '', '') }.to raise_error ArgumentError + expect { ClockworkSMS::API.new('', '', '') }.to raise_error ArgumentError end it "should set options if a parameters hash is passed along with the API key" do - api = Clockwork::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875', { :from => 'A Test', :long => true, :truncate => true, :invalid_char_action => :remove } + api = ClockworkSMS::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875', { :from => 'A Test', :long => true, :truncate => true, :invalid_char_action => :remove } expect(api.from).to match('A Test') expect(api.long).to be(true) expect(api.truncate).to be(true) @@ -46,7 +46,7 @@ describe "#invalid_char_action" do it "should raise an ArgumentError if value is not one of :error, :replace, :remove" do - api = Clockwork::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875' + api = ClockworkSMS::API.new 'af7a8f7a8fa7f8a76fa876fa876a876fa875a875' expect { api.invalid_char_action = '123' }.to raise_error ArgumentError expect { api.invalid_char_action = 'ERROR' }.to raise_error ArgumentError @@ -64,17 +64,17 @@ describe "#credit" do it "should return the number of messages remaining with an API key" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key expect(api.credit).to be > 0 end it "should raise an error with an invalid API key" do - api = Clockwork::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1' - expect { api.credit }.to raise_error Clockwork::Error::Generic + api = ClockworkSMS::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1' + expect { api.credit }.to raise_error ClockworkSMS::Error::Generic end it "should return the number of messages remaining over standard HTTP" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key api.use_ssl = false expect(api.credit).to be > 0 end @@ -84,7 +84,7 @@ describe "#balance" do it "should return the balance remaining with an API key" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key balance = api.balance expect(balance).to include(:account_type) expect(balance).to include(:balance) @@ -92,12 +92,12 @@ end it "should raise an error with an invalid API key" do - api = Clockwork::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1' - expect { api.balance }.to raise_error Clockwork::Error::Generic + api = ClockworkSMS::API.new 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1' + expect { api.balance }.to raise_error ClockworkSMS::Error::Generic end it "should return the balance over standard HTTP" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key api.use_ssl = false balance = api.balance expect(balance).to include(:account_type) diff --git a/spec/sms_spec.rb b/spec/sms_spec.rb index 7bfb865..fe0987e 100644 --- a/spec/sms_spec.rb +++ b/spec/sms_spec.rb @@ -7,7 +7,7 @@ describe "#initialize" do it "should set options if a parameters hash is passed" do - sms = Clockwork::SMS.new( :from => 'A Test', :long => true, :truncate => true, :invalid_char_action => :remove ) + sms = ClockworkSMS::SMS.new( :from => 'A Test', :long => true, :truncate => true, :invalid_char_action => :remove ) expect(sms.from).to match("A Test") expect(sms.long).to be(true) expect(sms.truncate).to be(true) @@ -19,32 +19,32 @@ describe "#deliver" do it "should accept a single phone number and a message parameter" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'This is a test message.' ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(true) expect(response.message_id).not_to be_empty end it "should not send a blank message" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => '' ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(false) expect(response.message_id).to be_nil expect(response.error_code).to be(7) end it "should accept additional parameters" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'This is a test message.', :client_id => 'message123' ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(true) expect(response.message_id).not_to be_empty expect(response.message.client_id).to match('message123') @@ -61,7 +61,7 @@ { :to => '44', :content => 'This is a test message 6.', :client_id => '7' } ] - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key messages.each do |m| api.messages.build(m) end @@ -76,53 +76,53 @@ end it "should not send huge message if truncate is false" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'a'*500, :truncate => false ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(false) expect(response.message_id).to be_nil expect(response.error_code).to be(12) end it "should send huge message if truncate is true" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'a'*500, :truncate => true ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(true) expect(response.message_id).not_to be_empty end it "should fail to send unicode snowman if invalid_char_action is error" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'snowman - ☃ - snowman', :invalid_char_action => :error ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(false) expect(response.message_id).to be_nil expect(response.error_code).to be(39) end it "should send unicode snowman if invalid_char_action is replace" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'snowman - ☃ - snowman', :invalid_char_action => :replace ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(true) expect(response.message_id).not_to be_empty end it "should send unicode snowman if invalid_char_action is remove" do - api = Clockwork::API.new test_api_key + api = ClockworkSMS::API.new test_api_key message = api.messages.build( :to => '441234123456', :content => 'snowman - ☃ - snowman', :invalid_char_action => :remove ) response = message.deliver - expect(response).to be_an_instance_of Clockwork::SMS::Response + expect(response).to be_an_instance_of ClockworkSMS::SMS::Response expect(response.success).to be(true) expect(response.message_id).not_to be_empty end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 516c784..95ae3da 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1 +1 @@ -require File.join( File.dirname(__FILE__) + "/../lib/clockwork" ) \ No newline at end of file +require File.join(File.dirname(__FILE__) + "/../lib/clockwork_sms") \ No newline at end of file