nabortu 0.0.1 → 0.1.0

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: 925c4ade6ee5063e20b733534f041092f108c80d
4
- data.tar.gz: 1c77f2c0e50c2f5bc808f3294d9baa15017d396e
3
+ metadata.gz: 6a4b650d553b36c833edcfc3b4adc0c7e821f6d2
4
+ data.tar.gz: 7d31b716632024bb083b27136bd74a2173c59dd3
5
5
  SHA512:
6
- metadata.gz: 202617eaedd8a7f215b2ec29daa5e0bab35154bee164d6ac04d0bc1cb1d3d976111eac8bbd20b3225a17e9265b0fa6c7eb23b7a6849a1274335f319d0edfad3c
7
- data.tar.gz: 345a655d675ed9d110f84851f8d7d20a397c71ca0bafb332451a833a6abc3aecd32d455503c079fcfe6bc183ba9be68fa8584be9922b1890f7918a896f0a84ca
6
+ metadata.gz: 850d80f8592e99966d908f050eadf7c6c118e0f63584999595729532a2c0b89d9b6fa66842dad52c92852570fce866652b9216b714e1337241aaced87e9375f4
7
+ data.tar.gz: 1263a5b89d5cc25d337e81b083793954914cc6c3552b83b5e7d8aea6afc523cba101ab409d72144ce1f7a5fa45594fe608dd171ce2819a283b49ac3a2693dcbe
@@ -1,5 +1,31 @@
1
- require "nabortu/version"
1
+ require 'nabortu/version'
2
+
3
+ require 'active_support'
4
+ require 'active_support/all'
5
+ require 'support/hash'
6
+
7
+ require 'nabortu/client'
8
+ require 'nabortu/errors'
9
+ require 'nabortu/configuration'
2
10
 
3
11
  module Nabortu
4
- # Your code goes here...
12
+ # Constants
13
+ STATE_RESERVE_ACTUAL = 5
14
+
15
+ # Class methods
16
+ class << self
17
+ attr_writer :config
18
+
19
+ def config
20
+ @config ||= Configuration.new
21
+ end
22
+
23
+ def configure
24
+ yield(config)
25
+ end
26
+
27
+ def client
28
+ Nabortu::Client.new
29
+ end
30
+ end
5
31
  end
@@ -0,0 +1,9 @@
1
+ module Nabortu
2
+ class Api
3
+ class << self
4
+ def call(method_name, args = {})
5
+
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ Dir[File.dirname(__FILE__) + '/methods/*.rb'].each { |file| require file }
2
+
3
+ module Nabortu
4
+ class Client
5
+ def send_payment(reserve_id)
6
+ url = URI.parse(Nabortu.config.payment_url)
7
+ http = Net::HTTP.new(url.host, url.port)
8
+ request = Net::HTTP::Post.new(url.path, {'Content-Type' =>'application/json'})
9
+ request.body = {orderId: reserve_id.to_s}.to_json
10
+
11
+ http.request(request)
12
+ end
13
+
14
+ def method_missing(name, params, args = {})
15
+ klass = ['Nabortu', 'Methods', name.to_s.camelize].join('::')
16
+
17
+ if Object.const_defined? klass
18
+ klass.constantize.do_request(name, params)
19
+ else
20
+ raise Nabortu::Errors::WrongMethod.new("Nabortu API does not have '#{name}' method or it\'s not implemented")
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module Nabortu
2
+ class Configuration
3
+ OPTIONS = [:url, :payment_url, :login, :password, :partner_id, :ns, :reserve_comment]
4
+ attr_accessor *OPTIONS
5
+
6
+ def initialize
7
+ @ns = 'http://www.nabortu.ru/'
8
+ @url = 'http://new.nabortu.ru/WebServices/Service.asmx?WSDL'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Nabortu
2
+ module Errors
3
+
4
+ # Raised when required param does not pass
5
+ class NoParam < StandardError
6
+ def initialize(name, message = '')
7
+ msg = message.empty? ? "You must set param '#{name}' for request" : message
8
+ super(msg)
9
+ end
10
+ end
11
+
12
+ # Raised when you try call non-existed API method
13
+ class WrongMethod < StandardError
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,67 @@
1
+ module Nabortu
2
+ module Methods
3
+ class Base
4
+
5
+ attr_accessor :params, :response
6
+
7
+ def self.do_request(method_name, params = {})
8
+ new(method_name, params).do_request
9
+ end
10
+
11
+ def initialize(method_name, params = {})
12
+ @method_name, @params, @response = method_name, params, nil
13
+ end
14
+
15
+ def do_request
16
+ prepare_result soap_client.call(@method_name, message: request(@params))
17
+ rescue => error # Catch all exceptions
18
+ fail_callback(error)
19
+ end
20
+
21
+ protected
22
+
23
+ def soap_client
24
+ params = {
25
+ wsdl: Nabortu.config.url,
26
+ open_timeout: 3000,
27
+ read_timeout: 3000,
28
+ element_form_default: 'unqualified',
29
+ soap_header: {
30
+ 'AuthentificationHeader' => {
31
+ 'Login' => Nabortu.config.login,
32
+ 'Password' => Nabortu.config.password,
33
+ 'PartnerId' => Nabortu.config.partner_id
34
+ },
35
+ attributes!: { 'AuthentificationHeader' => { 'xmlns' => 'http://www.nabortu.ru/'} }
36
+ },
37
+ logger: Rails.logger,
38
+ log: true,
39
+ log_level: :info
40
+ }
41
+
42
+ Savon.client(params) { convert_request_keys_to :camelcase }
43
+ end
44
+
45
+ def request(params = {})
46
+ raise StandardError.new("You try execute abstract method 'request' of Nabortu::Methods::Base")
47
+ end
48
+
49
+ def fail_callback(error)
50
+ raise error
51
+ end
52
+
53
+ def prepare_result(response)
54
+ @response = response.to_hash[:"#{@method_name}_response"][:"#{@method_name}_result"]
55
+ do_prepare
56
+ rescue
57
+ @response = false
58
+ ensure
59
+ @response
60
+ end
61
+
62
+ def do_prepare
63
+ @response
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ module Nabortu
2
+ module Methods
3
+ class CalculateVariant < Base
4
+ def request(params = {})
5
+ offer = params[:offer] || raise(Nabortu::Errors::NoParam.new :offer)
6
+ request = params[:request] || raise(Nabortu::Errors::NoParam.new :request)
7
+
8
+ {'tns:offer' => {'Item' => offer.deep_symbolize_keys.camelize_keys}, 'tns:Request' => request}
9
+ end
10
+
11
+ def do_prepare
12
+ @response[:calculation_result]
13
+ end
14
+
15
+ def fail_callback(error)
16
+ return false if error.is_a?(Savon::SOAPFault)
17
+ raise error
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module Nabortu
2
+ module Methods
3
+ class Cancel < Base
4
+ def request(params = {})
5
+ reserve_id = params[:reserve_id] || raise(Nabortu::Errors::NoParam.new :reserve_id)
6
+ {'tns:id_Reserve' => reserve_id}
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Nabortu
2
+ module Methods
3
+ class CheckEmail < Base
4
+ def request(params = {})
5
+ email = params[:email] || raise(Nabortu::Errors::NoParam.new :email)
6
+ {'tns:email' => email}
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Nabortu
2
+ module Methods
3
+ class CheckVariant < Base
4
+ def request(params = {})
5
+ offer = params[:offer] || raise(Nabortu::Errors::NoParam.new :offer)
6
+ request = params[:request] || raise(Nabortu::Errors::NoParam.new :request)
7
+
8
+ {'tns:offer' => {'Item' => offer.camelize_keys}, 'tns:Request' => request}
9
+ end
10
+
11
+ def fail_callback(error)
12
+ return false if error.is_a?(Savon::SOAPFault)
13
+ raise error
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,53 @@
1
+ module Nabortu
2
+ module Methods
3
+ class CreateAviaReserve < Base
4
+ def request(params = {})
5
+ persons = params[:persons] || raise(Nabortu::Errors::NoParam.new :persons)
6
+ request = params[:request] || raise(Nabortu::Errors::NoParam.new :request)
7
+ client_id = params[:client_id] || raise(Nabortu::Errors::NoParam.new :client_id)
8
+
9
+ persons_xml = persons.map { |p| build_person(p) }
10
+
11
+ <<-REQUEST
12
+ <tns:Request>#{request}</tns:Request>
13
+ <tns:Passengers>#{persons_xml.join('')}</tns:Passengers>
14
+ <tns:Comment>#{Nabortu.config.reserve_comment}</tns:Comment>
15
+ <tns:id_OnlinePaymentType>26</tns:id_OnlinePaymentType>
16
+ <tns:id_client>#{client_id}</tns:id_client>
17
+ <tns:PartnerMarker>nabortu</tns:PartnerMarker>
18
+ <tns:BonusProgram></tns:BonusProgram>
19
+ REQUEST
20
+ end
21
+
22
+ private
23
+
24
+ def build_person(person)
25
+ person = Nokogiri::XML::Builder.new do |xml|
26
+ xml.root('xmlns:tns' => Nabortu.config.ns){
27
+ xml[:tns].Person{
28
+ xml[:tns].id_Person person.id
29
+ xml[:tns].FirstName person.first_name
30
+ xml[:tns].LastName person.last_name
31
+ xml[:tns].Phone person.phone
32
+ xml[:tns].DateBirth person.birth_date
33
+ xml[:tns].id_Country person.id_country
34
+ xml[:tns].Gender person.gender
35
+ xml[:tns].FirstNameLatin person.first_name_latin
36
+ xml[:tns].LastNameLatin person.last_name_latin
37
+ xml[:tns].deleted 'false'
38
+ xml[:tns].ExtraBags '0'
39
+ xml[:tns].Documents{
40
+ xml[:tns].id_Document person.document.id
41
+ xml[:tns].DateGiven person.document.date
42
+ xml[:tns].DateExpire person.document.expires
43
+ xml[:tns].Number person.document.number
44
+ xml[:tns].id_DocumentType person.document.id_type
45
+ }
46
+ }
47
+ }
48
+ end
49
+ person.doc.root.children.to_xml
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module Nabortu
2
+ module Methods
3
+ class GetCitizenships < Base
4
+ def request(params = {})
5
+ {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Nabortu
2
+ module Methods
3
+ class GetDocumentTypes < Base
4
+ def request(params = {})
5
+ {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Nabortu
2
+ module Methods
3
+ class GetReserveDetails < Base
4
+ def request(params = {})
5
+ reserve_id = params[:reserve_id] || raise(Nabortu::Errors::NoParam.new :reserve_id)
6
+
7
+ {'tns:id_Reserve' => reserve_id.to_s}
8
+ end
9
+
10
+ def do_prepare
11
+ @response[:get_reserve_details_result]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,48 @@
1
+ module Nabortu
2
+ module Methods
3
+ class GetVariants < Base
4
+ def request(params = {})
5
+ date_to = params[:date_to] || raise(Nabortu::Errors::NoParam.new :date_to)
6
+ date_from = params[:date_from] || raise(Nabortu::Errors::NoParam.new :date_from)
7
+
8
+ origin = params[:origin] || 'MOW'
9
+ destination = params[:destination] || raise(Nabortu::Errors::NoParam.new :destination)
10
+
11
+ { 'tns:requestParameters' => {
12
+ 'AviaSearchRequest' => {
13
+ 'Adults' => params[:adults] || 2,
14
+ 'Children' => params[:children] || 0,
15
+ 'Infants' => params[:infants] || 0,
16
+ 'Segments' => {
17
+ 'ModifiedSegment' => [
18
+ { 'OriginCode' => params[:origin] || 'MOW',
19
+ 'DestinationCode' => destination,
20
+ 'Date' => date_to,
21
+ 'Fare' => 'Economy'
22
+ },
23
+ { 'OriginCode' => destination,
24
+ 'DestinationCode' => origin,
25
+ 'Date' => date_from,
26
+ 'Fare' => 'Economy'
27
+ }]
28
+ },
29
+ 'PartnerId' => Nabortu.config.partner_id,
30
+ 'Fare' => 'Economy',
31
+ 'FlexibleDates' => params[:flexible_dates] || 'false',
32
+ 'OnlyDirect' => params[:only_direct] || 'true',
33
+ 'IsOnePassSearch' => params[:one_pass_search] || 'true',
34
+ 'AllowLowcost' => params[:allow_lowcost] || 'false',
35
+ 'Nabortu' => params[:nabortu] || 'true'
36
+ }
37
+ }
38
+ }
39
+ end
40
+
41
+ def do_prepare
42
+ {offers: @response[:search_result][:offers][:item], request: @response[:search_result][:request]}
43
+ rescue
44
+ false
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ module Nabortu
2
+ module Methods
3
+ class Register < Base
4
+ def request(params = {})
5
+ email = params[:email] || raise(Nabortu::Errors::NoParam.new :email)
6
+ phone = params[:phone] || raise(Nabortu::Errors::NoParam.new :phone)
7
+ password = params[:password] || raise(Nabortu::Errors::NoParam.new :password)
8
+
9
+ {'tns:Email' => email, 'tns:Password' => password, 'tns:MobilePhone' => phone }
10
+ end
11
+
12
+ def do_prepare
13
+ @response[:auth_result]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,53 @@
1
+ module Nabortu
2
+ module Methods
3
+ class SavePerson < Base
4
+ def request(params = {})
5
+ person = params[:person] || raise(Nabortu::Errors::NoParam.new :person)
6
+ client_id = params[:client_id] || raise(Nabortu::Errors::NoParam.new :person)
7
+ build_person(person) + build_document(person.document) + "<tns:id_client>#{client_id}</tns:id_client>"
8
+ end
9
+
10
+ def do_prepare
11
+ @response[:save_person_result]
12
+ end
13
+
14
+ private
15
+
16
+ def build_person(person)
17
+ person = Nokogiri::XML::Builder.new do |xml|
18
+ xml.root("xmlns:tns" => 'http://nabortu.ru/'){
19
+ xml[:tns].p{
20
+ xml[:tns].FirstName person.first_name
21
+ xml[:tns].LastName person.last_name
22
+ xml[:tns].Phone person.phone
23
+ xml[:tns].SurName person.surname
24
+ xml[:tns].DateBirth person.birth_date
25
+ xml[:tns].id_Country person.id_country
26
+ xml[:tns].Gender person.gender
27
+ xml[:tns].FirstNameLatin person.first_name_latin
28
+ xml[:tns].LastNameLatin person.last_name_latin
29
+ xml[:tns].deleted 'false'
30
+ xml[:tns].ExtraBags '0'
31
+ }
32
+ }
33
+ end
34
+ person.doc.root.children.to_xml
35
+ end
36
+
37
+ def build_document(document)
38
+ doc = Nokogiri::XML::Builder.new do |xml|
39
+ xml.root('xmlns:tns' => 'http://www.nabortu.ru/'){
40
+ xml[:tns].Document{
41
+ xml[:tns].DateGiven document.date
42
+ xml[:tns].DateExpire document.expires
43
+ xml[:tns].Number document.number
44
+ xml[:tns].id_DocumentType document.id_type
45
+ xml[:tns].deleted 'false'
46
+ }
47
+ }
48
+ end
49
+ doc.doc.root.children.to_xml
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ module Nabortu
2
+ module Methods
3
+ class TryAuth < Base
4
+ def request(params = {})
5
+ email = params[:email] || raise(Nabortu::Errors::NoParam.new :email)
6
+ password = params[:password] || raise(Nabortu::Errors::NoParam.new :password)
7
+
8
+ { 'tns:Login' => email, 'tns:Password' => password }
9
+ end
10
+ def do_prepare
11
+ @response[:auth_result]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Nabortu
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,22 @@
1
+ class Hash
2
+ def camelize_keys
3
+ input, output = self.dup, self.dup
4
+
5
+ input.each do |key, _|
6
+ case input[key]
7
+ when String
8
+ if key.to_s.include? '@'
9
+ _key = ['@', key.to_s.gsub('@', '').camelize].join.to_sym
10
+ output.delete(key)
11
+ output[_key] = input[key]
12
+ end
13
+ when Hash
14
+ output[key] = input[key].camelize_keys
15
+ when Array
16
+ output[key] = input.delete(key).map{ |item| item.camelize_keys }
17
+ end
18
+ end
19
+
20
+ output
21
+ end
22
+ end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
+ spec.add_dependency 'activesupport', '>= 3.0'
21
22
  spec.add_development_dependency 'bundler', '~> 1.6'
22
23
  spec.add_development_dependency 'rake'
23
24
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nabortu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fc_arny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-22 00:00:00.000000000 Z
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -51,7 +65,25 @@ files:
51
65
  - README.md
52
66
  - Rakefile
53
67
  - lib/nabortu.rb
68
+ - lib/nabortu/api.rb
69
+ - lib/nabortu/client.rb
70
+ - lib/nabortu/configuration.rb
71
+ - lib/nabortu/errors.rb
72
+ - lib/nabortu/methods/base.rb
73
+ - lib/nabortu/methods/calculate_variant.rb
74
+ - lib/nabortu/methods/cancel.rb
75
+ - lib/nabortu/methods/check_email.rb
76
+ - lib/nabortu/methods/check_variant.rb
77
+ - lib/nabortu/methods/create_avia_reserve.rb
78
+ - lib/nabortu/methods/get_citizenships.rb
79
+ - lib/nabortu/methods/get_document_types.rb
80
+ - lib/nabortu/methods/get_reserve_details.rb
81
+ - lib/nabortu/methods/get_variants.rb
82
+ - lib/nabortu/methods/register.rb
83
+ - lib/nabortu/methods/save_person.rb
84
+ - lib/nabortu/methods/try_auth.rb
54
85
  - lib/nabortu/version.rb
86
+ - lib/support/hash.rb
55
87
  - nabortu.gemspec
56
88
  homepage: http://martsoft.ru/lab/nabortu
57
89
  licenses: