tourcms 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'xml-object'
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+ xml-object (0.9.93)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.2)
20
+ rcov
21
+ shoulda
22
+ xml-object
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alex Kremer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # tourcms
2
+
3
+ A simple wrapper for connecting to [TourCMS Marketplace API](http://www.tourcms.com/support/api/mp/). This wrapper mirrors the TourCMS PHP library.
4
+
5
+ ## Install
6
+
7
+ gem install tourcms
8
+
9
+ ## Usage
10
+
11
+ Using the library is as simple as creating a **TourCMS::Connection** object:
12
+
13
+ conn = TourCMS::Connection.new(marketplace_id, private_key, result_type)
14
+
15
+ Your Marketplace ID and Private Key can be found in the TourCMS Partner Portal. The result type can be one of **obj** or **raw** where **raw** will return the raw XML from the API and **obj** will return an XMLObject using the [XML-Object Gem](https://github.com/jordi/xml-object).
16
+
17
+ ### Working with your connection in Raw mode
18
+
19
+ # Instantiate the connection
20
+ conn = TourCMS::Connection.new("12345", "mydeepsecret", "raw")
21
+ # Check we're working
22
+ conn.api_rate_limit_status
23
+ => "<?xml version="1.0" encoding="utf-8" ?><response><request>GET /api/rate_limit_status.xml</request>
24
+ <error>OK</error><remaining_hits>1999</remaining_hits><hourly_limit>2000</hourly_limit></response>"
25
+ # List the channels we have access to
26
+ conn.list_channels
27
+ => ""<?xml version="1.0" encoding="utf-8" ?><response><request>GET /p/channels/list.xml</request>
28
+ <error>OK</error><channel>(...)</channel><channel>(...)</channel><channel>(...)</channel></response>"
29
+ # Show a particular channel
30
+ conn.show_channel(1234567)
31
+ => ""<?xml version="1.0" encoding="utf-8" ?><response><request>GET /p/channels/list.xml</request>
32
+ <error>OK</error><channel>(...)</channel></response>"
33
+
34
+ ### Working with your connecting in Obj mode
35
+
36
+ # Instantiate the connection
37
+ conn = TourCMS::Connection.new("12345", "mydeepsecret", "obj")
38
+ # Check we're working
39
+ obj = conn.api_rate_limit_status
40
+
41
+ Note: XML-Object **does not support .inspect** so obj will return empty. XML-Object uses method_missing to access object properties so **you will need to know which property you're trying to access** beforehand -- Check the API docs.
42
+
43
+ obj.hourly_limit
44
+ => 2000
45
+ # List the channels we have access to
46
+ obj = conn.list_channels
47
+ obj.channel
48
+ => [Array of Channel Objects]
49
+ obj.channel.first.channel_name
50
+ => "My Adventure Tour Operator"
51
+ # Show a particular channel
52
+ obj = conn.show_channel(1234567)
53
+ obj.channel.channel_id
54
+ => "1234567"
55
+ # Search for all tours in GB
56
+ obj = conn.search_tours(:country => "GB")
57
+ obj.tour.first.tour_name
58
+ => "Canyoning"
59
+
60
+ ### Passing parameters
61
+
62
+ Many TourCMS methods accept parameters. Most methods take a hash of parameters like so:
63
+
64
+ obj = conn.search_tours({:country => "GB", :lang => "en"})
65
+
66
+ ## List of functions in TourCMS::Connection
67
+
68
+ * api\_rate\_limit\_status
69
+ * list\_channels
70
+ * show\_channel
71
+ * search\_tours
72
+ * search\_hotels\_range
73
+ * search\_hotels\_specific
74
+ * list\_tours
75
+ * list\_tour\_images
76
+ * show\_tour
77
+ * show\_tour\_departures
78
+ * show\_tour\_freesale
79
+
80
+ ## Contributing to tourcms
81
+
82
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
83
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
84
+ * Fork the project
85
+ * Start a feature/bugfix branch
86
+ * Commit and push until you are happy with your contribution
87
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
88
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
89
+
90
+ ## Copyright
91
+
92
+ Copyright (c) 2011 Flextrip. See LICENSE.txt for further details.
93
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "tourcms"
16
+ gem.homepage = "http://github.com/flextrip/tourcms"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A Ruby Library for the TourCMS API}
19
+ gem.description = %Q{A simple Ruby wrapper for interacting with the TourCMS API}
20
+ gem.email = "alex@flextrip.com"
21
+ gem.authors = ["Alex Kremer"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'xml-object'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "tourcms #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,123 @@
1
+ module TourCMS
2
+ class Connection
3
+ def initialize(marketp_id, private_key, result_type = "raw")
4
+ Integer(marketp_id) rescue raise ArgumentError, "Marketplace ID must be an Integer"
5
+ @marketp_id = marketp_id
6
+ @private_key = private_key
7
+ @result_type = result_type
8
+ @base_url = "https://api.tourcms.com"
9
+ end
10
+
11
+ def api_rate_limit_status(channel = 0)
12
+ return_result(request("/api/rate_limit_status.xml", channel))
13
+ end
14
+
15
+ def list_channels
16
+ return_result(request("/p/channels/list.xml"))
17
+ end
18
+
19
+ def show_channel(channel)
20
+ return_result(request("/c/channel/show.xml", channel))
21
+ end
22
+
23
+ def search_tours(params = {}, channel = 0)
24
+ if channel == 0
25
+ return_result(request("/p/tours/search.xml", 0, params))
26
+ else
27
+ return_result(request("/c/tours/search.xml", channel, params))
28
+ end
29
+ end
30
+
31
+ def search_hotels_range(params = {}, tour = "", channel = 0)
32
+ if channel == 0
33
+ return_result(request("/p/hotels/search_range.xml", 0, params.merge({"single_tour_id" => tour})))
34
+ else
35
+ return_result(request("/c/hotels/search_range.xml", channel, params.merge({"single_tour_id" => tour})))
36
+ end
37
+ end
38
+
39
+ def search_hotels_specific(params = {}, tour = "", channel = 0)
40
+ if channel == 0
41
+ return_result(request("/p/hotels/search-avail.xml", 0, params.merge({"single_tour_id" => tour})))
42
+ else
43
+ return_result(request("/c/hotels/search-avail.xml", channel, params.merge({"single_tour_id" => tour})))
44
+ end
45
+ end
46
+
47
+ def list_tours(channel = 0)
48
+ if channel == 0
49
+ return_result(request("/p/tours/list.xml"))
50
+ else
51
+ return_result(request("/c/tours/list.xml", channel))
52
+ end
53
+ end
54
+
55
+ def list_tour_images(channel = 0)
56
+ if channel == 0
57
+ return_result(request("/p/tours/images/list.xml"))
58
+ else
59
+ return_result(request("/c/tours/images/list.xml", channel))
60
+ end
61
+ end
62
+
63
+ def show_tour(tour, channel)
64
+ return_result(request("/c/tour/show", channel, {"id" => tour}))
65
+ end
66
+
67
+ def show_tour_departures(tour, channel)
68
+ return_result(request("/c/tour/datesprices/dep/show.xml", channel, {"id" => tour}))
69
+ end
70
+
71
+ def show_tour_freesale(tour, channel)
72
+ return_result(request("/c/tour/datesprices/freesale/show.xml", channel, {"id" => tour}))
73
+ end
74
+
75
+ private
76
+
77
+ def return_result(result)
78
+ if @result_type == "raw"
79
+ result
80
+ else
81
+ XMLObject.new(result)
82
+ end
83
+ end
84
+
85
+ def construct_params(param_hash)
86
+ if param_hash.empty?
87
+ res = ""
88
+ else
89
+ qs = param_hash.stringify_keys.reject{|k,v| v.nil? || v.empty?}.collect{|k,v|"#{CGI.escape(k)}=#{CGI.escape(v)}"}.join("&")
90
+ qs.empty? ? res = "" : res = "?#{qs}"
91
+ end
92
+ res
93
+ end
94
+
95
+ def generate_signature(path, verb, channel, outbound_time)
96
+ string_to_sign = "#{channel}/#{@marketp_id}/#{verb}/#{outbound_time}#{path}".strip
97
+
98
+ dig = OpenSSL::HMAC.digest('sha256', @private_key, string_to_sign)
99
+ b64 = Base64.encode64(dig).chomp
100
+ CGI.escape(b64).gsub("+", "%20")
101
+ end
102
+
103
+ def request(path, channel = 0, params = {}, verb = "GET")
104
+ url = @base_url + path + construct_params(params)
105
+ req_time = Time.now.utc
106
+ signature = generate_signature(path + construct_params(params), verb, channel, req_time.to_i)
107
+
108
+ headers = {"Content-type" => "text/xml", "charset" => "utf-8", "Date" => req_time.strftime("%a, %d %b %Y %H:%M:%S GMT"),
109
+ "Authorization" => "TourCMS #{channel}:#{@marketp_id}:#{signature}" }
110
+
111
+ begin
112
+ response = open(url, headers).read
113
+ rescue Exception => e
114
+ puts "Caught exception opening #{url}"
115
+ puts e.message
116
+ puts e.backtrace.inspect
117
+ rescue OpenURI::HTTPError => http_e
118
+ puts "Received HTTP Error opening #{url}"
119
+ puts http_e.io.status[0].to_s
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ def stringify_keys
3
+ inject({}) do |options, (key, value)|
4
+ options[key.to_s] = value
5
+ options
6
+ end
7
+ end
8
+ end
data/lib/tourcms.rb ADDED
@@ -0,0 +1,11 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'cgi'
6
+ require 'open-uri'
7
+ require 'xml-object'
8
+
9
+ require 'tour_cms/hash'
10
+ require 'tour_cms/connection'
11
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'tourcms'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTourcms < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tourcms
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Alex Kremer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-26 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ name: xml-object
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :development
46
+ name: shoulda
47
+ prerelease: false
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ hash: 23
56
+ segments:
57
+ - 1
58
+ - 0
59
+ - 0
60
+ version: 1.0.0
61
+ type: :development
62
+ name: bundler
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 7
72
+ segments:
73
+ - 1
74
+ - 5
75
+ - 2
76
+ version: 1.5.2
77
+ type: :development
78
+ name: jeweler
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :development
92
+ name: rcov
93
+ prerelease: false
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :runtime
106
+ name: xml-object
107
+ prerelease: false
108
+ version_requirements: *id006
109
+ description: A simple Ruby wrapper for interacting with the TourCMS API
110
+ email: alex@flextrip.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ files:
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - VERSION
125
+ - lib/tour_cms/connection.rb
126
+ - lib/tour_cms/hash.rb
127
+ - lib/tourcms.rb
128
+ - test/helper.rb
129
+ - test/test_tourcms.rb
130
+ has_rdoc: true
131
+ homepage: http://github.com/flextrip/tourcms
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ hash: 3
154
+ segments:
155
+ - 0
156
+ version: "0"
157
+ requirements: []
158
+
159
+ rubyforge_project:
160
+ rubygems_version: 1.3.7
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: A Ruby Library for the TourCMS API
164
+ test_files:
165
+ - test/helper.rb
166
+ - test/test_tourcms.rb