class Mediaburst::API
Public Class Methods
new(u, p)
click to toggle source
# File lib/mediaburst/api.rb, line 19 def initialize(u, p) @auth = {:username => u, :password => p} end
Public Instance Methods
create_xml(numbers, content, options)
click to toggle source
Get the xml for the request
# File lib/mediaburst/api.rb, line 60 def create_xml(numbers, content, options) # Note that the username and password should be the first elements passed builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| xml.message { xml.Username @auth[:username] xml.Password @auth[:password] numbers.each do |number| xml.SMS { xml.To number xml.Content content options.each do |k, v| xml.send(k.to_s, v) end } end } end builder.to_xml end
get_credit()
click to toggle source
Returns the about of credit left on a user account
nil if there was a problem retrieving the value, and Throws an exception if the server didn’t process the request succesfully.
# File lib/mediaburst/api.rb, line 29 def get_credit uri = URI.parse(CREDIT_ENDPOINT + "?username=#{@auth[:username]}&password=#{@auth[:password]}") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) request["Content-Type"] = "text/html" response = http.request(request) case response when Net::HTTPSuccess return response.body.gsub!(/^Current Credit: ([0-9]+)/, '\1') else raise Mediaburst::ServerError, "Request failed: #{response}" end end
process_response(response)
click to toggle source
Process the received response
# File lib/mediaburst/api.rb, line 95 def process_response(response) # Make sure we get a successful response case response when Net::HTTPSuccess # Parse the response response_xml = Nokogiri::XML(response.body) if response_xml.xpath('//SMS_Resp').empty? raise Mediaburst::InvalidRequest, "ERROR: #{response_xml.xpath('//ErrDesc').inner_text}" else responses = {} response_xml.xpath('//SMS_Resp').each do |sms_resp| if sms_resp.xpath('ErrDesc').empty? responses[sms_resp.xpath('To').inner_text] = true else responses[sms_resp.xpath('To').inner_text] = sms_resp.xpath('ErrNo').inner_text.to_i end end end else raise Mediaburst::ServerError, "Request failed: #{response}" end responses end
send_message(numbers, content, options ={})
click to toggle source
Takes a number or array of numbers and a content string and sends to the mediaburst SMS API
endpoint.
numbers - a string or array of strings content - the string to send options - a hash of options to send to the API
Returns a hash in the format: “phone number” => true on success or an error number on failure
# File lib/mediaburst/api.rb, line 54 def send_message(numbers, content, options ={}) numbers = [numbers] unless numbers.class == Array self.process_response(self.send_request(self.create_xml(numbers, content, options))) end
send_request(request_body)
click to toggle source
Send a request to the endpoint
# File lib/mediaburst/api.rb, line 83 def send_request(request_body) # Create and send the request uri = URI.parse(SEND_ENDPOINT) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) request.body = request_body request["Content-Type"] = "text/xml" http.request(request) end