Class: Clockwork::API

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/api.rb

Overview

You must create an instance of Clockwork::API to begin using the API.

Author:

Constant Summary

SMS_URL =

URL of the SMS API send action

"api.clockworksms.com/xml/send"
CREDIT_URL =

URL of the SMS API check credit action

"api.clockworksms.com/xml/credit"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (API) initalize(api_key) - (API) initalize(username, password)

Clockwork::API is initialized with an API key, available from www.mediaburst.co.uk/api.

Overloads:

  • - (API) initalize(api_key)

    Parameters:

    • api_key (string)

      API key, 40-character hexadecimal string

    • options (hash)

      Optional hash of attributes on API

  • - (API) initalize(username, password)
    Deprecated.

    Use an API key instead. Support for usernames and passwords will be removed in a future version of this wrapper.

    Parameters:

    • username (string)

      Your API username

    • password (string)

      Your API password

    • options (hash)

      Optional hash of attributes on API

Raises:

  • ArgumentError - if more than 3 parameters are passed

  • Clockwork::Error::InvalidAPIKey - if API key is invalid



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/clockwork/api.rb', line 107

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}$/]
    @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?
    @username = args[0]
    @password = args[1]
  else
    raise ArgumentError, "You must pass either an API key OR a username and password."
  end
  
  args.last.each { |k, v| instance_variable_set "@#{k}", v } if args.last.kind_of?(Hash)
  
  @use_ssl = true if @use_ssl.nil?
  @concat = 3 if @concat.nil? && @long
  @messages ||= Clockwork::MessageCollection.new( :api => self )
end

Instance Attribute Details

- (string) api_key (readonly)

API key provided in Clockwork::API#initialize.

Returns:

  • (string)


18
19
20
# File 'lib/clockwork/api.rb', line 18

def api_key
  @api_key
end

- (string) from

Note:

This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used.

The from address displayed on a phone when the SMS is received. This can be either a 12 digit number or 11 characters long.

Returns:

  • (string)


24
25
26
# File 'lib/clockwork/api.rb', line 24

def from
  @from
end

- (symbol) invalid_char_action

Note:

This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used.

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.

Returns:

  • (symbol)

    One of error, :replace, :remove

Raises:

  • ArgumentError - if value is not one of :error, :replace, :remove



30
31
32
# File 'lib/clockwork/api.rb', line 30

def invalid_char_action
  @invalid_char_action
end

- (boolean) long

Note:

This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used.

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.

Returns:

  • (boolean)


36
37
38
# File 'lib/clockwork/api.rb', line 36

def long
  @long
end

- (Clockwork::MessageCollection) messages

Returns a Clockwork::MessageCollection containing all built SMS messages.



41
42
43
# File 'lib/clockwork/api.rb', line 41

def messages
  @messages
end

- (symbol) messages_to_concat

Note:

Defaults to 3 if not set and Clockwork::API#long is set to true.

If Clockwork::API#long is set to true, this is the number of messages to concatenate (join together).

Returns:

  • (symbol)

    One of error, :replace, :remove

Raises:

  • ArgumentError - if a value less than 2 or more than 3 is passed



47
48
49
# File 'lib/clockwork/api.rb', line 47

def messages_to_concat
  @messages_to_concat
end

- (string) password (readonly)

Deprecated.

Use api_key instead.

Password provided in Clockwork::API#initialize.

Returns:

  • (string)


52
53
54
# File 'lib/clockwork/api.rb', line 52

def password
  @password
end

- (boolean) truncate

Note:

This can be overriden for specific Clockwork::SMS objects; if it is not set your account default will be used.

Set to true to trim the message content to the maximum length if it is too long.

Returns:

  • (boolean)


58
59
60
# File 'lib/clockwork/api.rb', line 58

def truncate
  @truncate
end

- (boolean) use_ssl

Whether to use SSL when connecting to the API. Defaults to true

Returns:

  • (boolean)


63
64
65
# File 'lib/clockwork/api.rb', line 63

def use_ssl
  @use_ssl
end

- (string) username (readonly)

Deprecated.

Use api_key instead.

Username provided in Clockwork::API#initialize.

Returns:

  • (string)


68
69
70
# File 'lib/clockwork/api.rb', line 68

def username
  @username
end

Instance Method Details

- (Object) concat

Deprecated.

Use Clockwork::API#messages_to_concat instead. Support for Clockwork::API#concat will be removed in a future version of this wrapper.

Alias for Clockwork::API#messages_to_concat to preserve backwards compatibility with original Mediaburst API.



72
73
74
# File 'lib/clockwork/api.rb', line 72

def concat
  messages_to_concat
end

- (Object) concat=(val)

Deprecated.

Use Clockwork::API#messages_to_concat= instead. Support for Clockwork::API#concat= will be removed in a future version of this wrapper.

Alias for Clockwork::API#messages_to_concat= to preserve backwards compatibility with original Mediaburst API.



78
79
80
# File 'lib/clockwork/api.rb', line 78

def concat= val
  messages_to_concat val
end

- (integer) credit

Check the remaining credit for this account.

Returns:

  • (integer)

    Number of messages remaining

Raises:

  • Clockwork::Error::Authentication - if API login details are incorrect



129
130
131
132
133
# File 'lib/clockwork/api.rb', line 129

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 )
end

- (array) deliver_messages

Deliver multiple messages created using Clockwork::API#messages.build.

Returns:

  • (array)

    Array of Clockwork::SMS::Response objects for messages.



185
186
187
188
189
# File 'lib/clockwork/api.rb', line 185

def deliver_messages
  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 )
end

- (Object) get_credit

Deprecated.

Use Clockwork::API#credit. Support for Clockwork::API#get_credit will be removed in a future version of this wrapper.

Alias for Clockwork::API#credit to preserve backwards compatibility with original Mediaburst API.



84
85
86
# File 'lib/clockwork/api.rb', line 84

def get_credit
  credit
end

- (hash) send_message(number, message, options) - (hash) send_message(numbers, message, options)

Deprecated.

Use Clockwork::SMS#deliver. Support for Clockwork::API#send_message will be removed in a future version of this wrapper. Besides, you get a MUCH more useful return value using #deliver: a Clockwork::SMS::Response.

Alias for Clockwork::SMS#deliver to preserve backwards compatibility with original Mediaburst API.

Overloads:

  • - (hash) send_message(number, message, options)

    Parameters:

    • number (string)

      The phone number to send the SMS to in international number format (without a leading + or international dialling prefix such as 00, e.g. 441234567890).

    • message (string)

      The message content to send.

    • options (hash)

      Optional hash of attributes on Clockwork::SMS

  • - (hash) send_message(numbers, message, options)

    Parameters:

    • numbers (array)

      Array of string phone numbers to send the SMS to in international number format (without a leading + or international dialling prefix such as 00, e.g. 441234567890).

    • message (string)

      The message content to send.

    • options (hash)

      Optional hash of attributes on Clockwork::SMS

Returns:

  • (hash)

    Hash in the format "phone number" => true on success, or "phone number" => error_code on failure.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/clockwork/api.rb', line 146

def send_message *args
  result = {}
  
  options = {}
  options = args[2] if args[2].kind_of?(Hash)
  
  # TODO: Make this do a transactional send if we're sending multiple messages
  if args[0].kind_of?(Array)
    args[0].each do |number|        
      sms = self.messages.build( options )
      sms.to = number
      sms.content = args[1]
    end
    responses = self.deliver_messages
    responses.each do |response|
      if response.success
        result["#{response.message.to}"] = true
      else
        result["#{response.message.to}"] = response.error_code
      end
    end
    
  elsif args[0].kind_of?(String)        
    sms = self.messages.build( options )
    sms.to = args[0]
    sms.content = args[1]        
    response = sms.deliver
    if response.success
      result["#{sms.to}"] = true
    else
      result["#{sms.to}"] = response.error_code
    end
  end
  
  result      
end