ly-zizhuyou 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91ab4bd207f012514814028e90be866e2c5a2b85
4
- data.tar.gz: 1f8e806a74fb65f7897477533665b3610d852dda
3
+ metadata.gz: d2752855bb6a36dca58b1c913dd58db508aef7b1
4
+ data.tar.gz: e7677862c55ddb4f1c22256e360d5a2de8501e5d
5
5
  SHA512:
6
- metadata.gz: 7f3ae50a23572ad56dfd5892531b19211dc280f40fa979c66531c960ac35dd2acc880d37e9444aa4f2d1accff80332c04f30b5d7081e8b0b0f77c72ec6b6ead8
7
- data.tar.gz: bcd4c484c7d87efcebda161a4059a4466f46089fb29e82587dddcaf2babcdf0860864c6510b68f24c3f75c41c97349642ee55cf5ec1ab52cb8db3c1e0cc67f3e
6
+ metadata.gz: b83c37eec006292d10b17bef5ff209067f6abb6b308c1c34656f3776d2b291005cd7ce65519944ba56530ec6d3060efc7f6d98e87262f3b52951de8cd7cd6688
7
+ data.tar.gz: c1edcd0adac299577e1fc75e279c9e1405d979867255a1affa02cc4217b8401c930e2279323a62ed2f7769963247cce3004eefb411896f30275cb6a8ba8b35ae
@@ -1,5 +1,5 @@
1
1
  module Ly
2
2
  module Zizhuyou
3
- VERSION = '0.1.7'
3
+ VERSION = '0.1.8'
4
4
  end
5
5
  end
data/lib/ly/zizhuyou.rb CHANGED
@@ -19,7 +19,7 @@ module Ly
19
19
  end
20
20
 
21
21
  def fingerprint(params)
22
- Digest::MD5.hexdigest @access_id + @security_id + Hash[params.sort].map { |k, v| "#{k}=#{v}" }.join
22
+ Digest::MD5.hexdigest @access_id + @security_id + Hash[params.sort].map {|k, v| "#{k}=#{v}"}.join
23
23
  end
24
24
 
25
25
  def request(api_name, params = {})
@@ -36,9 +36,10 @@ module Ly
36
36
  request['Content-Type'] = 'application/x-www-form-urlencoded'
37
37
  request.body = URI.encode_www_form(@req_params = params)
38
38
  puts "\nParams: \n"
39
- @req_params.each{|k,v| puts "\t#{k}: #{v}\n"}
39
+ @req_params.each {|k, v| puts "\t#{k}: #{v}\n"}
40
40
  puts "\nParams(Encode): #{URI.encode_www_form(@req_params = params)}\n"
41
41
  response = http.request(request)
42
+ puts "Redirection: #{response['location']}" if response.is_a?(Net::HTTPRedirection)
42
43
  raise RequestError, "HTTP Response: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
43
44
  @resp = response.read_body
44
45
  puts "\nResp: #{@resp}"
@@ -65,5 +66,141 @@ module Ly
65
66
  @code = code
66
67
  end
67
68
  end
69
+
70
+ def self.prices_format(arrival_date, departure_date, result, room_info = nil)
71
+ return [] if result.nil?
72
+ ret_val = []
73
+ data = {}
74
+
75
+ # 聚合房型信息
76
+ pro_infos = {}
77
+ unless room_info.nil? || room_info['ResProBaseInfos'].nil?
78
+ room_info['ResProBaseInfos'].each do |pro_info|
79
+ pro_infos[pro_info['ProductUniqueId']] = pro_info
80
+ end
81
+ end
82
+
83
+ # 聚合房型&政策(生成空日期价格
84
+ unless result['ProInfoDetailList'].nil?
85
+ result['ProInfoDetailList'].each do |pro|
86
+ pro_info = pro_infos[pro['ProductUniqueId']]
87
+ unless data.key? pro['ProId']
88
+ data[pro['ProId']] = {
89
+ 'ProId' => pro['ProId'],
90
+ 'RoomName' => pro['RoomName'],
91
+ 'ProductInfo' => pro_info,
92
+ 'Products' => {}
93
+ }
94
+ end
95
+
96
+ #生成空日期价格
97
+ pro['ProSaleInfoDetails'] = {} if pro['ProSaleInfoDetails'].nil?
98
+ pro['ProSaleInfoDetails'] = (departure_date - arrival_date).to_i.times.map {|day| [(arrival_date + day).strftime('%m/%d/%Y 00:00:00'), nil]}.to_h.merge(pro['ProSaleInfoDetails'])
99
+
100
+ data[pro['ProId']]['Products'][pro['ProductUniqueId']] = pro.merge!('ProductInfo' => pro_info) unless pro['ProSaleInfoDetails'].value?(nil)
101
+ end
102
+ end
103
+
104
+ data.each do |_, r|
105
+ next if r['Products'].empty? # 跳过无政策房型
106
+ room = {'policies' => []}
107
+ room['room_id'] = r['ProId']
108
+ if r['ProductInfo'].present?
109
+ room['room_name'] = r['ProductInfo']['ResProName']
110
+ room['room_info'] = r['ProductInfo']['ResProProps']
111
+ else
112
+ room['room_name'] = r['RoomName']
113
+ end
114
+ room['image'] = ''
115
+ r['Products'].each do |_, p|
116
+ policy = {'daily_prices' => []}
117
+ policy['policy_id'] = p['ProductUniqueId']
118
+ if p['ProductInfo'].present?
119
+ policy['policy_name'] = p['ProductInfo']['SupPriceName']
120
+ policy['bed_type'] = p['ProductInfo']['BedTypeName']
121
+ else
122
+ policy['policy_name'] = p['PolicyName']
123
+ end
124
+
125
+ prices = []
126
+ breakfast = nil
127
+ statuses = []
128
+
129
+ guarantee_arrival_time = nil
130
+
131
+ p['ProSaleInfoDetails'].each do |d, s|
132
+ prices << s['DistributionSalePrice'] unless s.nil?
133
+ breakfast = s['BreakfastName'] if breakfast.nil?
134
+ status = !(s['InventoryStats'] == 4 || (!s['OpeningSale'] && s['InventoryRemainder'].zero?))
135
+ statuses << status
136
+ policy['daily_prices'] << {
137
+ 'date' => Time.strptime(d, '%m/%d/%Y 00:00:00').strftime('%Y-%m-%d'),
138
+ 'price' => s['DistributionSalePrice'],
139
+ 'breakfast' => s['BreakfastName'],
140
+ 'status' => status
141
+ }
142
+ unless s['GuaranteeHoldTime'].nil? || s['GuaranteeHoldTime'].empty?
143
+ guarantee_hold_time = Time.strptime("1970-01-01 #{s['GuaranteeHoldTime']} UTC", '%Y-%m-%d %H:%M:%S %Z')
144
+ guarantee_arrival_time = guarantee_hold_time if guarantee_arrival_time.nil? || (guarantee_hold_time > guarantee_arrival_time)
145
+ end
146
+ end
147
+
148
+ next if prices.empty?
149
+
150
+ policy['price'] = (prices.sum / prices.size.to_f).round
151
+ policy['breakfast'] = breakfast
152
+ policy['status'] = !statuses.include?(false)
153
+
154
+ policy['payment'] = 1
155
+ policy['total_price'] = prices.sum
156
+
157
+ policy['guarantee_arrival_time'] = ((guarantee_arrival_time - Time.at(0).utc) / 3600).to_i unless guarantee_arrival_time.nil?
158
+
159
+ room['policies'] << policy
160
+ end
161
+ ret_val << room unless room['policies'].empty?
162
+ end
163
+ ret_val
164
+ end
165
+
166
+ def self.bookable_format(result, guarantee_arrival_time)
167
+ ret_val = {status: result['CanBooking'] == 1}
168
+ if ret_val[:status]
169
+ unless result['BookableMessage'].nil?
170
+ bookable_message = JSON.parse(result['BookableMessage'])
171
+ guarantee_info = bookable_message['CtripGuaranteeInfo']
172
+ unless guarantee_info.nil? || guarantee_info['CgCode'].nil?
173
+ cg_code = nil
174
+ guarantee_info['CgCode'].each do |code|
175
+ cg = code.to_i
176
+ case cg
177
+ when 4
178
+ cg_code = cg if [2, 1, 3, 5].include?(cg_code) || cg_code.nil?
179
+ when 2
180
+ cg_code = cg if [1, 3, 5].include?(cg_code) || cg_code.nil?
181
+ when 1
182
+ cg_code = cg if [3, 5].include?(cg_code) || cg_code.nil?
183
+ when 3
184
+ cg_code = cg if [5].include?(cg_code) || cg_code.nil?
185
+ when 5
186
+ cg_code = cg
187
+ end
188
+ end
189
+ if [1, 2, 3, 4].include?(cg_code)
190
+ if cg_code == 4
191
+ ret_val[:guarantee] = 1
192
+ elsif cg_code == 2
193
+ ret_val[:guarantee] = 2
194
+ else
195
+ ret_val[:guarantee_arrival_time] = guarantee_arrival_time if cg_code == 3
196
+ ret_val[:guarantee] = 3
197
+ ret_val[:guarantee_amount] = guarantee_info['AmountPercent'].sum
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+ ret_val
204
+ end
68
205
  end
69
206
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ly-zizhuyou
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Jie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-01 00:00:00.000000000 Z
11
+ date: 2017-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler