Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Broadcast - initializer method #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 13 additions & 5 deletions lib/tm4b/broadcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

module TM4B
class Broadcast

def initialize(opts)
opts = {
:encoding => :plain ,
:split_method => :concatenation_graceful
}.merge(opts)

opts.each do |k,v|
attribute = k.to_s + "="
self.send(attribute, v)
end
end

attr_reader :recipients
def recipients=(recipients)
if String === recipients
Expand Down Expand Up @@ -63,11 +76,6 @@ def encoded_message
# response variables
attr_accessor :broadcast_id, :recipient_count, :balance_type, :credits, :balance, :neglected

def initialize
@encoding = :plain
@split_method = :concatenation_graceful
end

def raw_response=(body)
# parse the response body into an XML document
document = REXML::Document.new(body)
Expand Down
17 changes: 7 additions & 10 deletions lib/tm4b/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ def initialize(options={})
# use when sending this message. Use either "unicode" or "plain" or nil
#
def broadcast(recipients, originator, message, options={})
broadcast = Broadcast.new
broadcast.recipients = recipients
broadcast.originator = originator
broadcast.message = message
args = {
:recipients => recipients,
:originator => originator,
:message => message
}.merge(options)


broadcast.simulated = true if options[:simulated]
broadcast.split_method = options[:split_method] if options[:split_method]
broadcast.route = options[:route] if options[:route]
broadcast.encoding = options[:encoding] if options[:encoding]
broadcast = Broadcast.new(args)

response = request(broadcast.parameters)

Expand Down Expand Up @@ -125,4 +122,4 @@ def raise_if_service_error(body)
raise TM4B::ServiceError.new(code, message)
end
end
end
end
7 changes: 2 additions & 5 deletions spec/broadcast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

describe TM4B::Broadcast do
before do
@broadcast = TM4B::Broadcast.new
@broadcast.recipients = "+1 213 555-0100"
@broadcast.originator = "tm4btest"
@broadcast.message = "hello world"
@broadcast = TM4B::Broadcast.new(:recipients => "+1 213 555-0100", :originator => "tm4btest", :message => "hello world")
end

it "should return an encoded string if 'plain' encoding if selected" do
Expand Down Expand Up @@ -94,4 +91,4 @@
@broadcast.balance.should == 5995
@broadcast.neglected.should == "-"
end
end
end