tyreshopper_sqs2cb 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be16a91ca6be18e6cb0138bf54bcd3d48e3e25c2
4
+ data.tar.gz: 39eea999d39a96ed273f18f7548b58a89884f594
5
+ SHA512:
6
+ metadata.gz: 2a2b3ef7b917d84e4048f4334b7dcd2d46e196e34984784fce5dd5cc919d698f36b62d97c822a8ab927ea6dbdb5c37632a5401b5d4080a5d061201af20a0d75c
7
+ data.tar.gz: b0559a4206f01a1faec59e2dc2abefd67752ca5d70c4abfe8ed9d83e1adca871fc2124aaf8ae0884efb3051680f71d7357fb57c3e428ddcafbaf90b458d20f32
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ tyreshopper_sqs2cb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tyreshopper_sqs2cb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Stewart McKee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # TyreshopperSqs2cb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tyreshopper_sqs2cb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tyreshopper_sqs2cb
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/tyreshopper_sqs2cb/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ # Prepares the $LOAD_PATH by adding to it lib directories of the gem and
5
+ # its dependencies:
6
+ require 'tyreshopper_sqs2cb'
7
+
8
+ mh = TyreshopperSqs2cb::MessageHandler.new
9
+ mh.transfer_messages
@@ -0,0 +1,3 @@
1
+ module TyreshopperSqs2cb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,124 @@
1
+ require "tyreshopper_sqs2cb/version"
2
+ require 'aws-sdk-v1'
3
+ require 'json'
4
+ require 'rest_client'
5
+ require 'logger'
6
+ require 'sqs2cb'
7
+
8
+ module TyreshopperSqs2cb
9
+ class MessageHandler
10
+ def initialize
11
+
12
+ logfile = ENV['TYRESHOPPER_SQS2CB_LOGFILE_PATH'].nil? ? STDOUT : File.open(ENV['TYRESHOPPER_SQS2CB_LOGFILE_PATH'], 'a')
13
+ @logger = Logger.new logfile
14
+
15
+
16
+ @sqs2cb = ::Sqs2cb::MessageHandler.new
17
+
18
+ end
19
+
20
+ def stop
21
+ @sqs2cb.stop
22
+ end
23
+
24
+ def transfer_messages
25
+ @sqs2cb.transfer_messages do |message, options|
26
+ handle_received_message(message, options)
27
+ end
28
+ end
29
+
30
+ def handle_received_message(message, options)
31
+ @options = options
32
+ @logger.info "Tyreshopper Message received."
33
+ @logger.debug message.body
34
+ send_to_caseblocks(JSON.parse(message.body))
35
+
36
+ rescue JSON::ParserError => ex
37
+ # TODO needs some form of remote notification that an error occured.
38
+ @logger.error "Bad message format. Unable to deserialize message into JSON."
39
+ @logger.error ex
40
+ rescue Exception => ex
41
+ @logger.error "Something caused a message handling failure."
42
+ @logger.error ex
43
+ end
44
+
45
+
46
+ def send_to_caseblocks(msgHash)
47
+ @logger.info "Sending to CaseBlocks"
48
+
49
+ customer = find_or_create_customer(msgHash)
50
+ msgHash["customer_ref"] = customer["customer_ref"]
51
+ msgHash["user_signed_in"] = msgHash["signed_in"]
52
+
53
+ response = post("#{@caseBlocksAPIEndpoint}/case_blocks/customers", msgHash.to_json, :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken)
54
+
55
+ @logger.info "Received #{response.code} Location: #{response.headers[:location]}"
56
+ rescue RestClient::ExceptionWithResponse => ex
57
+ if ex.response.nil?
58
+ @logger.error "Received a bad response (null) from CaseBlocks. Re-raising."
59
+ raise ex
60
+ else
61
+ @logger.error "Received #{ex.response.code} bad response: #{ex.response.body}"
62
+ end
63
+ end
64
+
65
+ private
66
+ def find_or_create_customer(options)
67
+
68
+ # find customer case type id
69
+ customer_case_type = get("/case_blocks/people_types")["people_types"].select{|type| type["code"] == "customer" }.first
70
+
71
+ # if user was signed in then use member_id else use email
72
+ if options["signed_in"]
73
+ case_type_matches = get("/case_blocks/search", {"query" => "member_id%3A#{options["member"]["member_id"]}"}).detect{|type| type["case_type_id"] == customer_case_type["id"]}
74
+
75
+ if case_type_matches.nil?
76
+
77
+ customer = {:customer => {
78
+ :case_type_id => customer_case_type["id"],
79
+ :people_type_id => customer_case_type["id"],
80
+ :address_1 => options["member"]["address1"],
81
+ :address_2 => options["member"]["address2"],
82
+ :county => options["member"]["county"],
83
+ :prefix => options["member"]["prefix"],
84
+ :first_name => options["member"]["first_name"],
85
+ :surname => options["member"]["surname"],
86
+ :postcode => options["member"]["postcode"],
87
+ :town => options["member"]["town"]
88
+ }}
89
+
90
+ # create customer
91
+ else
92
+ return case_type_matches.first
93
+ end
94
+ else
95
+ case_type_matches = get("/case_blocks/search", {"query" => "email%3A#{options["email"]}"}).detect{|type| type["case_type_id"] == customer_case_type["id"]}
96
+
97
+ if case_type_matches.nil?
98
+ # create customer
99
+
100
+ else
101
+ return case_type_matches.first
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ def get(path, query={})
108
+ uri = Uri.parse("#{@caseBlocksAPIEndpoint}#{path}")
109
+
110
+ query.each do |k, v|
111
+ uri.query = [uri.query, "#{k}=#{v}"].compact.join('&')
112
+ end
113
+ uri.query = [uri.query, "auth_token=#{@caseBlocksAPIToken}"].compact.join('&')
114
+ return RestClient.get(uri.to_s, :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken)
115
+ end
116
+
117
+ def post(uri, data)
118
+ uri = Uri.parse("#{@caseBlocksAPIEndpoint}#{path}")
119
+
120
+ uri.query = [uri.query, "auth_token=#{@caseBlocksAPIToken}"].compact.join('&')
121
+ return RestClient.get(uri.to_s, data.to_json, :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe TyreshopperSqs2cb::MessageHandler do
4
+
5
+ it "should instantiate a new class" do
6
+ TyreshopperSqs2cb::MessageHandler.new.should be_kind_of TyreshopperSqs2cb::MessageHandler
7
+ end
8
+
9
+ context "handling message" do
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'tyreshopper_sqs2cb' # and any other gems you need
5
+
6
+ ENV['AWS_ACCESS_KEY'] = "access_key"
7
+ ENV['AWS_SECRET_ACCESS_KEY'] = "secret_key"
8
+ ENV['CB_API_ENDPOINT'] = "http://localhost:8888/"
9
+ ENV['CB_API_TOKEN'] = "token"
10
+ ENV['CB_QUEUE'] = "cb_queue"
11
+ ENV['SQS2CB_LOGFILE_PATH'] = "log/sqs2cb.log"
12
+ ENV['TYRESHOPPER_SQS2CB_LOGFILE_PATH'] = "log/tyreshopper_sqs2cb.log"
13
+
14
+ RSpec.configure do |config|
15
+ # some (optional) config here
16
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tyreshopper_sqs2cb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tyreshopper_sqs2cb"
8
+ spec.version = TyreshopperSqs2cb::VERSION
9
+ spec.authors = ["Stewart McKee"]
10
+ spec.email = ["stewart@theizone.co.uk"]
11
+ spec.summary = "Tyreshopper specific handling of data to send to CaseBlocks"
12
+ spec.description = "Tyreshopper specific handling of data to send to CaseBlocks"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sqs2cb"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tyreshopper_sqs2cb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stewart McKee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqs2cb
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Tyreshopper specific handling of data to send to CaseBlocks
70
+ email:
71
+ - stewart@theizone.co.uk
72
+ executables:
73
+ - tyreshopper_sqs2cb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-gemset"
80
+ - ".ruby-version"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/tyreshopper_sqs2cb
86
+ - lib/tyreshopper_sqs2cb.rb
87
+ - lib/tyreshopper_sqs2cb/version.rb
88
+ - spec/lib/tyreshopper_sqs2cb/message_handler_spec.rb
89
+ - spec/spec_helper.rb
90
+ - tyreshopper_sqs2cb.gemspec
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Tyreshopper specific handling of data to send to CaseBlocks
115
+ test_files:
116
+ - spec/lib/tyreshopper_sqs2cb/message_handler_spec.rb
117
+ - spec/spec_helper.rb