Class: Mediaburst::API
- Inherits:
-
Object
- Object
- Mediaburst::API
- Defined in:
- lib/mediaburst/api.rb
Instance Method Summary collapse
-
#create_xml(numbers, content, options) ⇒ Object
Get the xml for the request.
-
#get_credit ⇒ Object
Returns the about of credit left on a user account.
-
#initialize(u, p) ⇒ API
constructor
A new instance of API.
-
#process_response(response) ⇒ Object
Process the received response.
-
#send_message(numbers, content, options = {}) ⇒ Object
Takes a number or array of numbers and a content string and sends to the mediaburst SMS API endpoint.
-
#send_request(request_body) ⇒ Object
Send a request to the endpoint.
Constructor Details
#initialize(u, p) ⇒ API
Returns a new instance of API.
19 20 21 |
# File 'lib/mediaburst/api.rb', line 19 def initialize(u, p) @auth = {:username => u, :password => p} end |
Instance Method Details
#create_xml(numbers, content, options) ⇒ Object
Get the xml for the request
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mediaburst/api.rb', line 60 def create_xml(numbers, content, ) # Note that the username and password should be the first elements passed builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| xml. { xml.Username @auth[:username] xml.Password @auth[:password] numbers.each do |number| xml.SMS { xml.To number xml.Content content .each do |k, v| xml.send(k.to_s, v) end } end } end builder.to_xml end |
#get_credit ⇒ Object
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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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) ⇒ Object
Process the received response
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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 = {}) ⇒ Object
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
54 55 56 57 |
# File 'lib/mediaburst/api.rb', line 54 def (numbers, content, ={}) numbers = [numbers] unless numbers.class == Array self.process_response(self.send_request(self.create_xml(numbers, content, ))) end |
#send_request(request_body) ⇒ Object
Send a request to the endpoint
83 84 85 86 87 88 89 90 91 92 |
# 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 |