Permalink
Browse files

append /api to salesking url if its not there

  • Loading branch information...
1 parent c520635 commit 44d06fc8d29c9d7955c6915efda01d14d40ca9df @schorsch schorsch committed Nov 29, 2011
Showing with 32 additions and 5 deletions.
  1. +2 −0 lib/sk_sdk/README.rdoc
  2. +9 −2 lib/sk_sdk/base.rb
  3. +1 −3 lib/sk_sdk/sync.rb
  4. +20 −0 spec/sk_sdk/base_spec.rb
View
2 lib/sk_sdk/README.rdoc
@@ -64,6 +64,8 @@ remember we only support HTTPS:
=>
https://my_company.salesking.eu/api
+For convenience we check if your url ends in /api and add if you forgot it!
+
Create a single class
require 'sk_sdk/base'
View
11 lib/sk_sdk/base.rb
@@ -39,8 +39,8 @@ def save; save_with_validation; end
# :user<String>:: if using httpBasic auth set to sk user login email
# :password<String>:: if using httpBasic sk user password
def self.set_connection(opts)
- self.site = opts[:site]
- self.format = :json # f*** xml
+ self.site = site_api_url(opts[:site])
+ self.format = :json # f*** xml
if opts[:token] #oAuth access token in header
self.headers['Authorization'] = "Bearer #{opts[:token]}"
else
@@ -60,4 +60,11 @@ def self.headers
@headers ||= {}
end
end
+
+ private
+
+ def self.site_api_url(site)
+ site = site.gsub(/\/$/, '')
+ site =~ /\/api$/ ? site : "#{site}/api"
+ end
end
View
4 lib/sk_sdk/sync.rb
@@ -64,9 +64,7 @@ def initialize(l_obj, r_obj, field_map)
# field_map<Array[Hash{}]>::
def fields=(field_map)
@fields = []
- field_map.each do |fld|
- @fields << Field.new(fld)
- end
+ field_map.each { |fld| @fields << Field.new(fld) }
@fields
end
View
20 spec/sk_sdk/base_spec.rb
@@ -16,6 +16,26 @@ module KingTester; end
c.first_name.should == 'herbert' # implicit getter
end
+ it "should set api url" do
+ opts = {:site => 'https://my.salesking.eu', :token=>'123'}
+ result = 'https://my.salesking.eu/api'
+
+ SK::SDK::Base.set_connection(opts)
+ SK::SDK::Base.site.to_s.should == result
+
+ opts[:site] = 'https://my.salesking.eu/'
+ SK::SDK::Base.set_connection(opts)
+ SK::SDK::Base.site.to_s.should == result
+
+ opts[:site] ='https://my.salesking.eu/api'
+ SK::SDK::Base.set_connection(opts)
+ SK::SDK::Base.site.to_s.should == result
+
+ opts[:site] = 'https://my.salesking.eu/api/'
+ SK::SDK::Base.set_connection(opts)
+ SK::SDK::Base.site.to_s.should == result
+ end
+
it "should have properties as attributes" do
c = Client.new :some_field => ''
c.attributes.should == {"some_field"=>""}

0 comments on commit 44d06fc

Please sign in to comment.