exetel 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/exetel.gemspec +1 -1
  3. data/lib/exetel/api.rb +150 -0
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/exetel.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{exetel}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["James Kong"]
data/lib/exetel/api.rb CHANGED
@@ -47,3 +47,153 @@ module Exetel
47
47
  end
48
48
  end
49
49
 
50
+
51
+
52
+ module Clickatell
53
+ # This module provides the core implementation of the Clickatell
54
+ # HTTP service.
55
+ class API
56
+ attr_accessor :auth_options
57
+
58
+ class << self
59
+ # Authenticates using the given credentials and returns an
60
+ # API instance with the authentication options set to use the
61
+ # resulting session_id.
62
+ def authenticate(api_id, username, password)
63
+ api = self.new
64
+ session_id = api.authenticate(api_id, username, password)
65
+ api.auth_options = { :session_id => session_id }
66
+ api
67
+ end
68
+
69
+ # Set to true to enable debugging (off by default)
70
+ attr_accessor :debug_mode
71
+
72
+ # Enable secure mode (SSL)
73
+ attr_accessor :secure_mode
74
+
75
+ # Allow customizing URL
76
+ attr_accessor :api_service_host
77
+
78
+ # Set to true to test message sending; this will not actually send
79
+ # messages but will collect sent messages in a testable collection.
80
+ # (off by default)
81
+ attr_accessor :test_mode
82
+ end
83
+ self.debug_mode = false
84
+ self.secure_mode = false
85
+ self.test_mode = false
86
+
87
+ # Creates a new API instance using the specified +auth options+.
88
+ # +auth_options+ is a hash containing either a :session_id or
89
+ # :username, :password and :api_key.
90
+ #
91
+ # Some API calls (authenticate, ping etc.) do not require any
92
+ # +auth_options+. +auth_options+ can be updated using the accessor methods.
93
+ def initialize(auth_options={})
94
+ @auth_options = auth_options
95
+ end
96
+
97
+ # Authenticates using the specified credentials. Returns
98
+ # a session_id if successful which can be used in subsequent
99
+ # API calls.
100
+ def authenticate(api_id, username, password)
101
+ response = execute_command('auth', 'http',
102
+ :api_id => api_id,
103
+ :user => username,
104
+ :password => password
105
+ )
106
+ parse_response(response)['OK']
107
+ end
108
+
109
+ # Pings the service with the specified session_id to keep the
110
+ # session alive.
111
+ def ping(session_id)
112
+ execute_command('ping', 'http', :session_id => session_id)
113
+ end
114
+
115
+ # Sends a message +message_text+ to +recipient+. Recipient
116
+ # number should have an international dialing prefix and
117
+ # no leading zeros (unless you have set a default prefix
118
+ # in your clickatell account centre).
119
+ #
120
+ # Additional options:
121
+ # :from - the from number/name
122
+ # :set_mobile_originated - mobile originated flag
123
+ # :client_message_id - user specified message id that can be used in place of Clickatell issued API message ID for querying message
124
+ # Returns a new message ID if successful.
125
+ def send_message(recipient, message_text, opts={})
126
+ valid_options = opts.only(:from, :mo, :callback, :climsgid)
127
+ valid_options.merge!(:req_feat => '48') if valid_options[:from]
128
+ valid_options.merge!(:mo => '1') if opts[:set_mobile_originated]
129
+ valid_options.merge!(:climsgid => opts[:client_message_id]) if opts[:client_message_id]
130
+ recipient = recipient.join(",")if recipient.is_a?(Array)
131
+ response = execute_command('sendmsg', 'http',
132
+ {:to => recipient, :text => message_text}.merge(valid_options)
133
+ )
134
+ response = parse_response(response)
135
+ response.is_a?(Array) ? response.map { |r| r['ID'] } : response['ID']
136
+ end
137
+
138
+ def send_wap_push(recipient, media_url, notification_text='', opts={})
139
+ valid_options = opts.only(:from)
140
+ valid_options.merge!(:req_feat => '48') if valid_options[:from]
141
+ response = execute_command('si_push', 'mms',
142
+ {:to => recipient, :si_url => media_url, :si_text => notification_text, :si_id => 'foo'}.merge(valid_options)
143
+ )
144
+ parse_response(response)['ID']
145
+ end
146
+
147
+ # Returns the status of a message. Use message ID returned
148
+ # from original send_message call.
149
+ def message_status(message_id)
150
+ response = execute_command('querymsg', 'http', :apimsgid => message_id)
151
+ parse_response(response)['Status']
152
+ end
153
+
154
+ # Returns the number of credits remaining as a float.
155
+ def account_balance
156
+ response = execute_command('getbalance', 'http')
157
+ parse_response(response)['Credit'].to_f
158
+ end
159
+
160
+ def sms_requests
161
+ @sms_requests ||= []
162
+ end
163
+
164
+ protected
165
+ def execute_command(command_name, service, parameters={}) #:nodoc:
166
+ executor = CommandExecutor.new(auth_hash, self.class.secure_mode, self.class.debug_mode, self.class.test_mode)
167
+ result = executor.execute(command_name, service, parameters)
168
+
169
+ (sms_requests << executor.sms_requests).flatten! if self.class.test_mode
170
+
171
+ result
172
+ end
173
+
174
+ def parse_response(raw_response) #:nodoc:
175
+ Clickatell::Response.parse(raw_response)
176
+ end
177
+
178
+ def auth_hash #:nodoc:
179
+ if @auth_options[:session_id]
180
+ { :session_id => @auth_options[:session_id] }
181
+ elsif @auth_options[:api_id]
182
+ { :user => @auth_options[:username],
183
+ :password => @auth_options[:password],
184
+ :api_id => @auth_options[:api_key] }
185
+ else
186
+ {}
187
+ end
188
+ end
189
+
190
+ end
191
+ end
192
+
193
+ %w( api/command
194
+ api/command_executor
195
+ api/error
196
+ api/message_status
197
+ ).each do |lib|
198
+ require File.join(File.dirname(__FILE__), lib)
199
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - James Kong