Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions lib/tm4b/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,17 @@ def check_balance
# @option options [String] :custom_data Search option using custom data
#
def check_status(options)
check = StatusCheck.new
check = StatusCheck.new(options)

if not options[:sms_id] and not options[:custom_data]
raise "one of :sms_id or :custom_data are required"
end
raise ArgumentError, "either :sms_id or :custom_data is required" if !check.valid?

check.sms_id = options[:sms_id]
check.custom = options[:custom_data]
response = request(check.parameters)

response = request(check.parameters)
raise_if_service_error(response.body)

raise_if_service_error(response.body)
check.raw_response = response.body

check.raw_response = response.body

check
check
end

private
Expand All @@ -125,4 +120,4 @@ def raise_if_service_error(body)
raise TM4B::ServiceError.new(code, message)
end
end
end
end
28 changes: 15 additions & 13 deletions lib/tm4b/status_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@

module TM4B
class StatusCheck

VERSION = "2.1"
TYPE = "check_status"

attr_accessor :sms_id, :custom, :status, :timestamp

def initialize(args)
@sms_id = args[:sms_id]
@custom = args[:custom_data]
end

def parameters
params = {
"version" => "2.1",
"type" => "check_status"
}

if sms_id
params["smsid"] = sms_id
else
params["custom"] = custom
end

params
{"version" => VERSION, "type" => TYPE, "smsid" => sms_id, "custom" => custom}.delete_if { |k, v| !v }
end

def raw_response=(body)
Expand All @@ -34,5 +32,9 @@ def raw_response=(body)
def to_s
"TM4B::StatusCheck\n" + %w(sms_id custom status timestamp).map {|x| "\t#{x}: #{send(x)}" }.join("\n")
end

def valid?
!!(sms_id || custom)
end
end
end
end
4 changes: 2 additions & 2 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
it "should raise an error when neither :sms_id or :custom_data are provided" do
lambda do
@client.check_status({})
end.should raise_error "one of :sms_id or :custom_data are required"
end.should raise_error(ArgumentError, "either :sms_id or :custom_data is required")
end

it "should assign the sms_id and custom_data parameters" do
Expand All @@ -160,4 +160,4 @@
response.timestamp.should == DateTime.civil(2011, 03, 07, 07, 15)
end
end
end
end
15 changes: 11 additions & 4 deletions spec/status_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

describe TM4B::StatusCheck do
before do
@check = TM4B::StatusCheck.new
@check.sms_id = "FooBar"
@check.custom = "Baz"
@check = TM4B::StatusCheck.new({:sms_id => "FooBar"})
end

it "should define parameters for the api" do
Expand Down Expand Up @@ -36,4 +34,13 @@
@check.status.should == "SUBMITD"
@check.timestamp.should == nil
end
end

it "should not be valid if neither :sms_id or :custom_data are provided" do
TM4B::StatusCheck.new({}).valid?.should be_false
end

it "should be valid if either :sms_id or :custom_data is provided" do
TM4B::StatusCheck.new(:sms_id => 123).valid?.should be_true
TM4B::StatusCheck.new(:custom_data => "data").valid?.should be_true
end
end