radiant-booking_app-extension 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 755a20a2facb90a2af6f99852f166a4fc2fe95f4
4
+ data.tar.gz: 948f3e953b25c13a37797128ede0d1e287aabc08
5
+ SHA512:
6
+ metadata.gz: 4be283cb684cb9da9ba86c42414219b48265aa7de923bc0a95e33c752d22f6536236c37ac06c86a96111f1632807c388c30a7b5fa417b3bde85291589bece7fe
7
+ data.tar.gz: eb038bdffb8b878c4e64dc810179bf32e03e00177cb4c80ed738704fcf4cde9f71f81ef83c82416cc39afd0164d05d365bc841e03d4d958e2eaa6b6583cd777d
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Default: run specs.'
4
+ task :default => :spec
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
+ # Put spec opts in a file named .rspec in root
12
+ end
@@ -0,0 +1,136 @@
1
+ class PageMountController < SiteController
2
+ no_login_required
3
+
4
+ radiant_layout { |controller| controller.domains_layout }
5
+
6
+ before_filter :set_monitoring_meta_data, only: :proxy
7
+
8
+ # Example:
9
+ # /accommodation/kangaroo-island/ozone-12?adults=1
10
+ #
11
+ # params[:paths] => ["kangaroo-island", "ozone-12"]
12
+ # params[:adults] => 1
13
+ def proxy
14
+ response = http_proxy(booking_app_url + request.path)
15
+
16
+ # TODO: Resolve so that response doesn't return blank for cookies on 500?
17
+ # ... or determine if having blank cookies means "don't set anything buddy"
18
+ session[:proxied_cookies] = response.cookies unless response.cookies.blank?
19
+
20
+ if response.redirect?
21
+ redirect_to response.redirect_location
22
+ return
23
+ end
24
+
25
+ unless response.ok?
26
+ Rails.logger.error("Booking-app proxy failed. Response [#{response.code}] =>")
27
+ Rails.logger.error(response.body)
28
+ # TODO: Contact errbit? Can we improve this fail page...?
29
+ #
30
+ # This is rendered in booking-app: 'shared/quicktravel_unavailable'
31
+ # Probably, it should be here!
32
+ end
33
+
34
+ proxy_response = ProxyResponse.new(response)
35
+ if proxy_response.file?
36
+ send_file(proxy_response)
37
+ else
38
+ @title = proxy_response.headers[:x_title]
39
+ store_client_type_id_in_cms_session(proxy_response) if login_details_request?(request)
40
+ render text: proxy_response.body,
41
+ content_type: proxy_response.content_type,
42
+ status: proxy_response.code,
43
+ layout: true
44
+ end
45
+ end
46
+
47
+ def domains_layout
48
+ calculator.domains_layout
49
+ end
50
+
51
+ private
52
+
53
+ def store_client_type_id_in_cms_session(proxy_response)
54
+ # Special Hack so CC can show exclusive products for clients
55
+ session[:client_type_id] = JSON.parse(proxy_response.body)['client_type_id']
56
+ end
57
+
58
+ def login_details_request?(request)
59
+ request.path == '/parties/login_header_details'
60
+ end
61
+
62
+ def http_proxy(url)
63
+ RequestProxy.call(request, url, params, session[:proxied_cookies])
64
+ end
65
+
66
+ def set_monitoring_meta_data
67
+ return unless defined?(NewRelic)
68
+ controller, action = extract_controller_action(request, params)
69
+ NewRelic::Agent.set_transaction_name("page_mount:#{controller}/#{action}")
70
+ end
71
+
72
+ def extract_controller_action(request, params)
73
+ if request.path == '/cells'
74
+ params[:name].split('/', 2)
75
+ else
76
+ *parts, action = request.path[1..-1].split('/')
77
+ [parts.join('/'), action]
78
+ end
79
+ end
80
+
81
+ class_attribute :calculator
82
+
83
+ def calculator
84
+ @calculator ||= Radiant::Config['bookingapp.calculator'].constantize.new(request)
85
+ end
86
+
87
+ def booking_app_url
88
+ calculator.booking_app_url
89
+ end
90
+
91
+ # TODO: Check if we can delete this...
92
+ def protect_against_forgery?
93
+ false
94
+ end
95
+
96
+ def send_file(proxy_response)
97
+ send_data proxy_response.body,
98
+ type: proxy_response.content_type,
99
+ disposition: proxy_response.content_disposition
100
+ end
101
+
102
+ class ProxyResponse
103
+ DEFAULT_CONTENT_DISPOSITION = 'inline'
104
+
105
+ attr_reader :response
106
+
107
+ def initialize(response)
108
+ @response = response
109
+ end
110
+
111
+ FILE_CONTENT_TYPES = %w(
112
+ application/octet-stream
113
+ application/javascript
114
+ text/plain # js.map
115
+ application/json
116
+ )
117
+
118
+ def file?
119
+ headers.key?(:content_disposition) || FILE_CONTENT_TYPES.include?(content_type)
120
+ end
121
+
122
+ def content_type
123
+ headers[:content_type].split(';').first
124
+ end
125
+
126
+ def content_disposition
127
+ headers[:content_disposition] || DEFAULT_CONTENT_DISPOSITION
128
+ end
129
+
130
+ delegate :body, :content_type, :code, to: :response
131
+
132
+ def headers
133
+ @response.body.headers
134
+ end
135
+ end
136
+ end
@@ -0,0 +1 @@
1
+ <%= raw @proxied_html %>
@@ -0,0 +1,13 @@
1
+ # Uncomment this if you reference any of your controllers in activate
2
+ # require_dependency 'application_controller'
3
+ class BookingAppExtension < Radiant::Extension
4
+ version "1.0"
5
+ description "This extension is communicating with Booking App"
6
+ url 'https://github.com/sealink/radiant-booking_app-extension'
7
+
8
+ def activate
9
+ end
10
+
11
+ def deactivate
12
+ end
13
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,35 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ # Page-mounted App
3
+ map.with_options controller: 'page_mount', action: 'proxy' do |page_mount|
4
+ page_mount.connect 'search/accommodations'
5
+
6
+ # These are CMS pages:
7
+ # /accommodation/hotel-motel (for each type)
8
+ # /accommodation/kangaroo-island-accommodation/ (for landing page)
9
+ #
10
+ page_mount.connect ':region_name/:accommodation_id',
11
+ requirements: {
12
+ region_name: /[a-z-]*-accommodation/,
13
+ accommodation_id: /\d+.*/
14
+ }
15
+
16
+ page_mount.connect 'transport/*paths'
17
+ page_mount.connect 'tours/*paths'
18
+
19
+ page_mount.connect 'app_cells/*paths'
20
+
21
+ page_mount.connect 'cart/*paths'
22
+
23
+ page_mount.connect 'secure-pay'
24
+ page_mount.connect 'secure-payment'
25
+ page_mount.connect 'cart_activations/*paths'
26
+ page_mount.connect 'bookings/manage/*paths'
27
+
28
+ page_mount.connect 'parties/*paths'
29
+ page_mount.connect 'reset_password/*paths'
30
+ page_mount.connect 'documents/*paths'
31
+
32
+ page_mount.connect 'booking-app-js/*paths'
33
+ page_mount.connect 'media/*paths'
34
+ end
35
+ end
@@ -0,0 +1,90 @@
1
+ require 'rest-client'
2
+
3
+ class RequestProxy
4
+ def self.call(request, url, data, cookies)
5
+ proxy = new(request, url, data, cookies)
6
+ proxy.call
7
+ end
8
+
9
+ def initialize(request, url, data, cookies)
10
+ @method = request.method
11
+ @url = url
12
+ @data = data
13
+ @cookies = cookies
14
+ @headers = request.headers
15
+ end
16
+
17
+ def call
18
+ options = {
19
+ method: @method,
20
+ url: url,
21
+ cookies: @cookies,
22
+ headers: headers_for_booking_app,
23
+ }
24
+ options[:payload] = @data unless http_get?
25
+
26
+ RestClient::Request.execute(options) do |response, _request, _result, &_block|
27
+ WrappedResponse.new(response)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def http_get?
34
+ @method == :get
35
+ end
36
+
37
+ def url
38
+ url = @url
39
+ url += "?#{@data.to_query}" if http_get?
40
+ url
41
+ end
42
+
43
+ def headers_for_booking_app
44
+ {
45
+ accept: @headers['Accept'],
46
+ referer: @headers['HTTP_REFERER'],
47
+ 'X-CSRF-Token' => @headers['X-CSRF-Token']
48
+ }
49
+ end
50
+
51
+ class WrappedResponse
52
+ def initialize(response)
53
+ @response = response
54
+ end
55
+
56
+ def redirect_location
57
+ location_uri = URI.parse(@response.headers[:location])
58
+
59
+ location = location_uri.path
60
+ location += '?' + location_uri.query if location_uri.query.present?
61
+ location += '#' + location_uri.fragment if location_uri.fragment.present?
62
+ location
63
+ end
64
+
65
+ def code
66
+ @response.code
67
+ end
68
+
69
+ def body
70
+ @response.body
71
+ end
72
+
73
+ def cookies
74
+ @response.cookies
75
+ end
76
+
77
+ def content_type
78
+ @response.headers[:content_type]
79
+ end
80
+
81
+ def ok?
82
+ (200..207).include? @response.code
83
+ end
84
+
85
+ def redirect?
86
+ (300..399).include? @response.code
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "radiant-booking_app-extension"
3
+ spec.version = '0.1.0'
4
+ spec.authors = ["Michael Noack"]
5
+ spec.email = ["support@travellink.com.au"]
6
+ spec.description = %q{Integrate booking app with radiant}
7
+ spec.summary = %q{Integrate booking app with radiant}
8
+ spec.homepage = 'http://github.com/sealink/radiant-booking_app-extension'
9
+
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_dependency 'radius'
18
+ spec.add_dependency 'rest-client' # '1.7.2' # what kis-cms uses, 1.8 is last ruby 1.9 compatible but breaks cookies, 2.0 requires ruby 2.0
19
+ spec.add_dependency 'radiant-layouts-extension' # radiant_layout
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radiant-booking_app-extension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Noack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: radius
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: radiant-layouts-extension
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: Integrate booking app with radiant
70
+ email:
71
+ - support@travellink.com.au
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Rakefile
77
+ - app/controllers/page_mount_controller.rb
78
+ - app/views/page_mount/proxy.erb
79
+ - booking_app_extension.rb
80
+ - config/routes.rb
81
+ - lib/request_proxy.rb
82
+ - radiant-booking_app-extension.gemspec
83
+ homepage: http://github.com/sealink/radiant-booking_app-extension
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.8
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Integrate booking app with radiant
107
+ test_files: []