Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ability to request the users current credit

  • Loading branch information...
commit 4e41c436c1855954fe78c8672c39247ab4e5ce12 1 parent fc4c690
@MattHall authored
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm 1.8.7@mediaburst_gem
View
7 Gemfile
@@ -0,0 +1,7 @@
+source :rubygems
+
+gem "rake", "0.8.7"
+gem "rspec"
+gem "mocha"
+gem "webmock", "~> 1.6.4"
+gem "nokogiri"
View
30 Gemfile.lock
@@ -0,0 +1,30 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.6)
+ crack (0.1.8)
+ diff-lcs (1.1.2)
+ mocha (0.9.12)
+ nokogiri (1.5.0)
+ rake (0.8.7)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ webmock (1.6.4)
+ addressable (~> 2.2, > 2.2.5)
+ crack (>= 0.1.7)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mocha
+ nokogiri
+ rake (= 0.8.7)
+ rspec
+ webmock (~> 1.6.4)
View
12 README.md
@@ -52,6 +52,18 @@ Check the response for errors:
On error, the value of the key referenced by the number will equal the [error code][5] returned from Mediaburst.
+### Checking the Account's Credit
+
+Create an instance of the Mediaburst client:
+
+ client = Mediaburst::API.new('username', 'password')
+
+Request the credit amount
+
+ client.get_credit
+ => "150"
+
+Given invalid credentials, this method will return nil. If the server responds with anything other than a 200, a Mediaburst::ServerError exception is raised.
## Development
View
6 Rakefile
@@ -5,7 +5,7 @@ require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the maptastic plugin.'
+desc 'Test the mediaburst plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
@@ -13,10 +13,10 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end
-desc 'Generate documentation for the maptastic plugin.'
+desc 'Generate documentation for the mediaburst plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'Maptastic'
+ rdoc.title = 'Mediaburst'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
View
27 lib/mediaburst/api.rb
@@ -2,7 +2,8 @@
require 'net/http'
module Mediaburst
- ENDPOINT = 'http://sms.message-platform.com/xml/send.aspx'
+ SEND_ENDPOINT = 'http://sms.message-platform.com/xml/send.aspx'
+ CREDIT_ENDPOINT = 'http://sms.message-platform.com/http/credit.aspx'
# Thrown when an invalid request is made,
# e.g invalid credentials were used
@@ -18,6 +19,28 @@ class API
def initialize(u, p)
@auth = {:username => u, :password => p}
end
+
+
+ # 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.
+ 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
# Takes a number or array of numbers and a content string
# and sends to the mediaburst SMS API endpoint.
@@ -59,7 +82,7 @@ def create_xml(numbers, content, options)
# Send a request to the endpoint
def send_request(request_body)
# Create and send the request
- uri = URI.parse(ENDPOINT)
+ 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
View
45 spec/api_spec.rb
@@ -12,7 +12,7 @@ module Mediaburst
describe "if the server has gone away" do
it "should throw a ServerError exception" do
client = Mediaburst::API.new('username', 'password')
- stub_request(:post, Mediaburst::ENDPOINT).to_return(:status => [500, "Internal Server Error"])
+ stub_request(:post, Mediaburst::SEND_ENDPOINT).to_return(:status => [500, "Internal Server Error"])
lambda do
client.send_message('1234567890', 'test')
@@ -24,7 +24,7 @@ module Mediaburst
describe "with invalid requests" do
it "should throw an InvalidRequest exception" do
client = Mediaburst::API.new('username', 'password')
- stub_request(:post, Mediaburst::ENDPOINT).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/invalid_response.xml', "r").read)
+ stub_request(:post, Mediaburst::SEND_ENDPOINT).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/invalid_response.xml', "r").read)
lambda do
client.send_message('1234567890', 'test')
@@ -70,7 +70,7 @@ module Mediaburst
describe "parsing the response" do
before(:each) do
- stub_request(:post, Mediaburst::ENDPOINT).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/mixed_response.xml', "r").read)
+ stub_request(:post, Mediaburst::SEND_ENDPOINT).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/mixed_response.xml', "r").read)
@client = Mediaburst::API.new('username', 'password')
@response = @client.send_message('1234567890', 'test')
end
@@ -84,4 +84,43 @@ module Mediaburst
end
end
+
+ describe "getting the account credit" do
+ before(:each) do
+ @request_path = "http://sms.message-platform.com/http/credit.aspx?password=password&username=username"
+ end
+
+ describe "with valid credentials" do
+ before(:each) do
+ stub_request(:get, @request_path).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/credit_response.html', "r").read)
+ @client = Mediaburst::API.new('username', 'password')
+ @response = @client.get_credit
+ end
+
+ it "should return the correct credit value" do
+ @response.should eql("90250")
+ end
+ end
+
+ describe "with invalid credentials" do
+ it "should return nil" do
+ stub_request(:get, @request_path).to_return(:status => 200, :body => File.open(File.dirname(__FILE__) + '/fixtures/invalid_credit_response.html', "r").read)
+ client = Mediaburst::API.new('username', 'password')
+
+ client.get_credit.should be_nil
+ end
+ end
+
+ describe "with the server not returning correct results" do
+ it "should raise an exception" do
+ client = Mediaburst::API.new('username', 'password')
+ stub_request(:get, @request_path).to_return(:status => [500, "Internal Server Error"])
+
+ lambda do
+ client.get_credit
+ end.should raise_error(Mediaburst::ServerError)
+ end
+ end
+ end
+
end
View
1  spec/fixtures/credit_response.html
@@ -0,0 +1 @@
+Current Credit: 90250
View
1  spec/fixtures/invalid_credit_response.html
@@ -0,0 +1 @@
+Error 2: Invalid Username Or Password
View
4 spec/spec_helper.rb
@@ -1,9 +1,9 @@
require 'rubygems'
-require 'spec'
+require 'rspec'
require 'mocha'
require 'test/unit'
require 'webmock/rspec'
-Spec::Runner.configure do |config|
+RSpec.configure do |config|
config.mock_with :mocha
end
Please sign in to comment.
Something went wrong with that request. Please try again.