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
14 changes: 5 additions & 9 deletions lib/tm4b/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ module TM4B
class Client
BaseURI = URI.parse("http://www.tm4b.com/client/api/http.php").freeze

attr_accessor :username, :password, :use_ssl

def initialize(options={})
@username = options.fetch(:username, TM4B.config.username)
@password = options.fetch(:password, TM4B.config.password)
@use_ssl = options.fetch(:use_ssl, TM4B.config.use_ssl)
options.each{|key, value| TM4B.config.send("#{key}=", value)}
end

#
Expand Down Expand Up @@ -109,10 +105,10 @@ def check_status(options)
def request(params)
req = Net::HTTP::Post.new("/client/api/http.php")

req.set_form_data params.merge(:username => username, :password => password)
req.set_form_data params.merge(:username => TM4B.config.username, :password => TM4B.config.password)

conn = Net::HTTP.new("www.tm4b.com", use_ssl ? 443 : 80)
conn.use_ssl = use_ssl
conn = Net::HTTP.new("www.tm4b.com", TM4B.config.use_ssl ? 443 : 80)
conn.use_ssl = TM4B.config.use_ssl
conn.start {|http| http.request(req) }
end

Expand All @@ -125,4 +121,4 @@ def raise_if_service_error(body)
raise TM4B::ServiceError.new(code, message)
end
end
end
end
5 changes: 2 additions & 3 deletions lib/tm4b/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
module TM4B
class Configuration
attr_accessor :username, :password, :use_ssl
class Configuration < Struct.new(:username, :password, :use_ssl)
end
end
end
16 changes: 8 additions & 8 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
TM4B.config.use_ssl = true
end

it 'should accept options for an overridden username, password and ssl' do
it 'should accept options to override username, password and ssl' do
default_client = TM4B::Client.new
default_client.username.should == "foo"
default_client.password.should == "bar"
default_client.use_ssl.should == true
TM4B.config.username.should == "foo"
TM4B.config.password.should == "bar"
TM4B.config.use_ssl.should == true

overridden_client = TM4B::Client.new :username => "one", :password => "two", :use_ssl => false
overridden_client.username.should == "one"
overridden_client.password.should == "two"
overridden_client.use_ssl.should == false
TM4B.config.username.should == "one"
TM4B.config.password.should == "two"
TM4B.config.use_ssl.should == false
end

context "broadcasting a message" do
Expand Down Expand Up @@ -160,4 +160,4 @@
response.timestamp.should == DateTime.civil(2011, 03, 07, 07, 15)
end
end
end
end