alfa_insurance 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: 9a0def11c9547d525960f188b95da3080b19c5b0
4
+ data.tar.gz: 9caab4f5c9bb6d78abee60fa828b4d3c9c587b9f
5
+ SHA512:
6
+ metadata.gz: 414254d9203ce1fda1ab385b233596a884bd517ce5f6b3ec997bb5bccd1cb73f15b6135266d74a841dc9d21be35fae577d141b877283e6e6f3ebecb6e3fd9a16
7
+ data.tar.gz: 599d0d54a268852e10e7b26cf1a2b9297553328fd92954517ae864336f20ccc4f2b8a4204648fb64d9a1e4aff1c764f7142902d71b7796be3a76dd2b8186665c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alfa_insurance.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alexander Sviridov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # AlfaInsurance
2
+
3
+ Обертка для SOAP API предоставляемого Альфа-Страхованием
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'alfa_insurance'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install alfa_insurance
20
+
21
+ ## Usage
22
+
23
+ Пример использования:
24
+
25
+ ```ruby
26
+ client = AlfaInsurance::BusPolicy.new(operator: 'TestBusOperator', product_code: 'TEST-BUS', debug: true)
27
+ response = client.calculate(480)
28
+ response.cost
29
+ response.risk_value
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/alfa_insurance.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'alfa_insurance/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alfa_insurance"
8
+ spec.version = AlfaInsurance::VERSION
9
+ spec.authors = ["Alexander Sviridov"]
10
+ spec.email = ["alexander.sviridov@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby wrapper for ALfaInsurance SOAP API}
13
+ spec.homepage = "https://github.com/busfor/alfa-insurance/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "savon", "~> 2.1"
24
+ spec.add_dependency "nokogiri", "~> 1"
25
+ spec.add_dependency "money", "~> 6.7"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "vcr"
31
+ spec.add_development_dependency "webmock"
32
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "alfa_insurance"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require "nokogiri"
2
+ require "savon"
3
+ require "money"
4
+
5
+ require "alfa_insurance/version"
6
+ require "alfa_insurance/base_client"
7
+ require "alfa_insurance/bus_client"
8
+ require "alfa_insurance/response"
9
+ require "alfa_insurance/calculate_response"
10
+ require "alfa_insurance/confirm_response"
11
+ require "alfa_insurance/create_response"
12
+ require "alfa_insurance/find_response"
13
+ require "alfa_insurance/insurance"
14
+
15
+ module AlfaInsurance
16
+ # Your code goes here...
17
+ end
@@ -0,0 +1,80 @@
1
+ module AlfaInsurance
2
+ class BaseClient
3
+ SANDBOX_WSDL = 'https://uat-tes.alfastrah.ru/travel-ext-services/TravelExtService?wsdl'.freeze
4
+
5
+ attr_accessor :log, :log_level, :operator, :product_code, :wsdl
6
+
7
+ def initialize(debug: false, wsdl: SANDBOX_WSDL, operator:, product_code:)
8
+ if debug
9
+ @log_level = :debug
10
+ @log = true
11
+ else
12
+ @log = false
13
+ end
14
+ @wsdl = wsdl
15
+ @operator = operator
16
+ @product_code = product_code
17
+ end
18
+
19
+ def calculate(*)
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def create(*)
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def confirm(insurance_id)
28
+ response = send_soap_request(:confirm_policy) do |xml|
29
+ xml.operator { xml.code(operator) }
30
+ xml.policyId(insurance_id)
31
+ end
32
+ ConfirmResponse.new(response)
33
+ end
34
+
35
+ def cancel(insurance_id)
36
+ response = send_soap_request(:cancel_policy) do |xml|
37
+ xml.operator { xml.code(operator) }
38
+ xml.policyId(insurance_id)
39
+ end
40
+ Response.new(response)
41
+ end
42
+
43
+ def find(insurance_id)
44
+ response = send_soap_request(:get_policy) do |xml|
45
+ xml.operator { xml.code(operator) }
46
+ xml.policyId(insurance_id)
47
+ end
48
+ FindResponse.new(response)
49
+ end
50
+
51
+ private
52
+
53
+ def send_soap_request(action_name)
54
+ message = Nokogiri::XML::Builder.new do |xml|
55
+ xml.root {
56
+ yield(xml)
57
+ }
58
+ end
59
+ payload = apply_namespace(message, action_name)
60
+ soap_client.call(action_name, message: payload)
61
+ end
62
+
63
+ def soap_client
64
+ @client ||= Savon.client(wsdl: wsdl, log_level: log_level, log: log)
65
+ end
66
+
67
+ def action_namespace(action)
68
+ soap_client.wsdl.operations[action][:namespace_identifier]
69
+ end
70
+
71
+ def apply_namespace(xml_builder, action)
72
+ namespace = action_namespace(action)
73
+
74
+ xml_text = xml_builder.doc.root.inner_html
75
+ xml_text.gsub!('</', "</#{namespace}:")
76
+ xml_text.gsub!(%r{<(?!/)}, "<#{namespace}:")
77
+ xml_text
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,26 @@
1
+ module AlfaInsurance
2
+ class BusClient < BaseClient
3
+ def calculate(total_cost)
4
+ response = send_soap_request(:create_policy) do |xml|
5
+ xml.operator { xml.code(operator) }
6
+ xml.product { xml.code(product_code) }
7
+ xml.policyParameters {
8
+ xml.ticketInformation {
9
+ xml.ticketTotalValue(total_cost.to_f)
10
+ }
11
+ }
12
+ end
13
+ CalculateResponse.new(response)
14
+ end
15
+
16
+ def create(insurance_object)
17
+ raise ArgumentError, "BusInsuranceRequest is expected" unless insurance_object.is_a?(BusInsuranceRequest)
18
+ response = send_soap_request(:create_policy) do |xml|
19
+ xml.operator { xml.code(operator) }
20
+ xml.product { xml.code(product_code) }
21
+ xml.policyParameters { insurance_object.generate_xml(xml) }
22
+ end
23
+ CreateResponse.new(response)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ module AlfaInsurance
2
+ class CalculateResponse < Response
3
+ def cost
4
+ Money.new(body.dig(:calculation_result, :premium), currency)
5
+ end
6
+
7
+ def risk_value
8
+ Money.new(body.dig(:calculation_result, :risk_value_sum), currency)
9
+ end
10
+
11
+ def risk_type
12
+ body.dig(:calculation_result, :risk_value, :@risk_type)
13
+ end
14
+
15
+ private
16
+
17
+ def currency
18
+ @currency ||= body.dig(:calculation_result, :currency)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module AlfaInsurance
2
+ class ConfirmResponse < CalculateResponse
3
+ def insurance_id
4
+ body[:full_number].to_i
5
+ end
6
+
7
+ def document_url
8
+ body.dig(:policy_document, :url)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module AlfaInsurance
2
+ class CreateResponse < CalculateResponse
3
+ def insurance_id
4
+ body[:policy_id].to_i
5
+ end
6
+
7
+ def cost
8
+ Money.new(body.dig(:calculation_result, :premium), currency)
9
+ end
10
+
11
+ def risk_value
12
+ Money.new(body.dig(:calculation_result, :risk_value_sum), currency)
13
+ end
14
+
15
+ def risk_type
16
+ body.dig(:calculation_result, :risk_value, :@risk_type)
17
+ end
18
+
19
+ private
20
+
21
+ def currency
22
+ @currency ||= body.dig(:calculation_result, :currency)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ module AlfaInsurance
2
+ class FindResponse < CalculateResponse
3
+ def insurance_id
4
+ body.dig(:policy_information, :policy_id).to_i
5
+ end
6
+
7
+ def cost
8
+ Money.new(body.dig(:policy_information, :rate), currency)
9
+ end
10
+
11
+ def risk_value
12
+ Money.new(body.dig(:policy_information, :risk_value, :@value), risk_currency)
13
+ end
14
+
15
+ def risk_type
16
+ body.dig(:policy_information, :risk_value, :@risk_type)
17
+ end
18
+
19
+ def state
20
+ body.dig(:policy_information, :policy_status)
21
+ end
22
+
23
+ private
24
+
25
+ def risk_currency
26
+ @risk_currency ||= body.dig(:policy_information, :risk_currency, :@value)
27
+ end
28
+
29
+ def currency
30
+ @currency ||= body.dig(:policy_information, :currency)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,83 @@
1
+ module AlfaInsurance
2
+ class BusSegment
3
+ attr_reader :route_number,
4
+ :place_number,
5
+ :departure_station,
6
+ :departure_at,
7
+ :arrival_station,
8
+ :arrival_at,
9
+ :number
10
+
11
+ def initialize(params = {})
12
+ params.each do |attr, value|
13
+ instance_variable_set("@#{attr}", value)
14
+ end
15
+ end
16
+ end
17
+
18
+ class BusInsuranceRequest
19
+ attr_reader :insured_first_name,
20
+ :insured_last_name,
21
+ :insured_patronymic,
22
+ :insured_birth_date,
23
+ :insured_document_type,
24
+ :insured_document_number,
25
+ :bus_segments,
26
+ :total_value,
27
+ :customer_email,
28
+ :customer_phone
29
+
30
+ def initialize(params = {})
31
+ params.each do |attr, value|
32
+ instance_variable_set("@#{attr}", value)
33
+ end
34
+ end
35
+
36
+ def generate_xml(xml)
37
+ xml.insuredFirstName(insured_first_name)
38
+ xml.insuredLastName(insured_last_name)
39
+ xml.insuredPatronymic(insured_patronymic)
40
+ xml.insuredBirthDate(insured_birth_date)
41
+ xml.insuredCount(1)
42
+ # xml.insuredDocumentType(insured_document_type)
43
+ # xml.insuredDocumentNumber(insured_document_number)
44
+ bus_segments.each_with_index do |segment, index|
45
+ xml.busSegmentRouteNumber(seqNo: index) {
46
+ xml.value(segment.route_number)
47
+ }
48
+ xml.busSegmentPlaceNumber(seqNo: index) {
49
+ xml.value(segment.place_number)
50
+ }
51
+ xml.busSegmentDepartureStation(seqNo: index) {
52
+ xml.value(segment.departure_station)
53
+ }
54
+ xml.busSegmentDepartureDate(seqNo: index) {
55
+ xml.value(segment.departure_at.to_date.iso8601)
56
+ }
57
+ xml.busSegmentDepartureTime(seqNo: index) {
58
+ xml.value(segment.departure_at.strftime("%H:%M:%S"))
59
+ }
60
+ xml.busSegmentArrivalStation(seqNo: index) {
61
+ xml.value(segment.arrival_station)
62
+ }
63
+ xml.busSegmentArrivalDate(seqNo: index) {
64
+ xml.value(segment.arrival_at.to_date.iso8601)
65
+ }
66
+ xml.busSegmentArrivalTime(seqNo: index) {
67
+ xml.value(segment.arrival_at.strftime("%H:%M:%S"))
68
+ }
69
+ xml.busSegmentNumber(seqNo: index) {
70
+ xml.value(segment.number)
71
+ }
72
+ end
73
+ xml.busSegmentsCount(bus_segments.size)
74
+ xml.ticketInformation {
75
+ xml.ticketTotalValue(total_value.to_f)
76
+ }
77
+ xml.customerPhoneType 'MOBILE'
78
+ xml.customerPhone customer_phone
79
+ xml.customerEmail customer_email
80
+ xml
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,23 @@
1
+ module AlfaInsurance
2
+ class Response
3
+ def initialize(soap_response)
4
+ @raw_response = soap_response
5
+ end
6
+
7
+ def success?
8
+ body.dig(:return_code, :code) == 'OK'
9
+ end
10
+
11
+ def error_code
12
+ body.dig(:return_code, :code) unless success?
13
+ end
14
+
15
+ def error_description
16
+ body.dig(:return_code, :error_message) unless success?
17
+ end
18
+
19
+ def body
20
+ @body ||= @raw_response.body.values.first
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module AlfaInsurance
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alfa_insurance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Sviridov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: money
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '6.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.7'
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.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - alexander.sviridov@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - alfa_insurance.gemspec
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/alfa_insurance.rb
142
+ - lib/alfa_insurance/base_client.rb
143
+ - lib/alfa_insurance/bus_client.rb
144
+ - lib/alfa_insurance/calculate_response.rb
145
+ - lib/alfa_insurance/confirm_response.rb
146
+ - lib/alfa_insurance/create_response.rb
147
+ - lib/alfa_insurance/find_response.rb
148
+ - lib/alfa_insurance/insurance.rb
149
+ - lib/alfa_insurance/response.rb
150
+ - lib/alfa_insurance/version.rb
151
+ homepage: https://github.com/busfor/alfa-insurance/
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.5.2
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Ruby wrapper for ALfaInsurance SOAP API
175
+ test_files: []