class Sms50X::Client
Attributes
api_key[RW]
country_code[RW]
host[RW]
Public Class Methods
new(*args)
click to toggle source
# File lib/sms50X/client.rb, line 11 def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} @api_key = get_api_key(args[0]) @country_code = get_country_code(args[1]) if [@api_key, @country_code].any? { |k| k.nil? } raise ArgumentError, 'API key and country code are required' end @host = get_host end
Public Instance Methods
balance()
click to toggle source
# File lib/sms50X/client.rb, line 30 def balance response = Faraday.get("#{host}/balance/#{api_key}") response.body.to_i end
bulk_send(message, phone_numbers)
click to toggle source
# File lib/sms50X/client.rb, line 45 def bulk_send(message, phone_numbers) response = Faraday.post("#{host}/xml", build_bulk_xml(message, phone_numbers)) response.body end
get_replies(date = Date.today, output_format = :json)
click to toggle source
# File lib/sms50X/client.rb, line 40 def get_replies(date = Date.today, output_format = :json) response = Faraday.get("#{host}/smsin/#{api_key}/#{output_format.to_s}/#{date.strftime("%d-%m-%Y")}") response.body end
get_stats(month = Date.today.month)
click to toggle source
# File lib/sms50X/client.rb, line 35 def get_stats(month = Date.today.month) response = Faraday.get("#{host}/stat/#{api_key}/#{month}") response.body.to_i end
send_message(phone, message)
click to toggle source
# File lib/sms50X/client.rb, line 25 def send_message(phone, message) response = Faraday.get("#{host}/sms/#{api_key}/t=#{phone}&m=#{escape(message)}") response.body.to_i end
Private Instance Methods
build_bulk_xml(message, phone_numbers)
click to toggle source
# File lib/sms50X/client.rb, line 75 def build_bulk_xml(message, phone_numbers) root = Element.new "SMS" auth = root.add_element "authentification" # [sic] api_key_element = auth.add_element "apikey" api_key_element.text = api_key message_element = root.add_element "message" text_element = message_element.add_element "text" text_element.text = message recipients = root.add_element "recipients" phone_numbers.each do |number| gsm = recipients.add_element "gsm" gsm.text = number end root.to_s end
escape(component)
click to toggle source
# File lib/sms50X/client.rb, line 91 def escape(component) CGI.escape(component.to_s) end
get_api_key(arg)
click to toggle source
# File lib/sms50X/client.rb, line 52 def get_api_key(arg) [arg, Sms50X.api_key, ENV['SMS50X_API_KEY']].find{ |x| x } end
get_country_code(arg)
click to toggle source
# File lib/sms50X/client.rb, line 56 def get_country_code(arg) [arg, Sms50X.country_code, ENV['SMS50X_COUNTRY_CODE']].find{ |x| x } end
get_host()
click to toggle source
# File lib/sms50X/client.rb, line 60 def get_host # ISO 3166-1 alpha-3 codes countries = { 'GTM' => '502', # Guatemala 'SLV' => '503', # El Salvador 'HND' => '504', # Honduras 'CRI' => '506', # Costa Rica 'PAN' => '507' # Panama } code = countries.fetch(country_code) { countries['CRI'] } "http://api.sms#{code}.com" end