justiz_sync 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: f6f1857369597b3596e2d20f889a5651daa81077
4
+ data.tar.gz: 878761187a610887fde442f878cbf80ca6f0cd43
5
+ SHA512:
6
+ metadata.gz: 490cc4f5a7203cc2980a7d4cda061ceff8998f5c75d68f49e15f483fd28bd244793fd5c4d9bae34842e6d29bd1bee94b60e1d43d50d4fe5db408edba04735158
7
+ data.tar.gz: 3444427c7b794a48e9ee259f66ceadb90793256cefba8483afd53f516c51fd5f3835498a1d9a12b97640e7518119282da42e3d270eea173bd992f85c23615795
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in justiz_sync.gemspec
4
+ gemspec
5
+
6
+ # local development only
7
+ #gem 'opencrx', path: "../opencrx"
8
+ #gem 'justiz', path: "../justiz"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Park
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
+ # JustizSync
2
+
3
+ Command line tool to synchronize contact information from justiz gem via the opencrx gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'justiz_sync'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install justiz_sync
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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 new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/justiz_sync ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'justiz_sync'
4
+
5
+ JustizSync::Cli.start
@@ -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 'justiz_sync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "justiz_sync"
8
+ spec.version = JustizSync::VERSION
9
+ spec.authors = ["Mike Park"]
10
+ spec.email = ["mikep@quake.net"]
11
+ spec.description = %q{Reads contact information from justiz gem and saves to LegalEntities using the opencrx gem }
12
+ spec.summary = %q{Command line tool to sync contacts from Justiz to openCRX}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 "justiz", "~> 0.1.0"
22
+ spec.add_dependency "opencrx", "~> 0.1.0"
23
+ spec.add_dependency "thor"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "~> 2.6"
28
+ spec.add_development_dependency "awesome_print"
29
+ spec.add_development_dependency "webmock"
30
+ spec.add_development_dependency "vcr"
31
+
32
+ end
@@ -0,0 +1,75 @@
1
+ require 'thor'
2
+ require 'awesome_print'
3
+ require 'justiz'
4
+
5
+ module JustizSync
6
+ class Cli < Thor
7
+ desc "hello NAME", "say hello to NAME"
8
+ def hello(name)
9
+ puts "Hello #{name}"
10
+ end
11
+
12
+ option :user, required: true, default: 'guest'
13
+ option :password, required: true, default: 'guest'
14
+ option :url, required: true, default: 'http://localhost:8080'
15
+ option :state
16
+ option :verbose
17
+ desc 'sync', 'sync all records from justiz to opencrx'
18
+ def sync
19
+ transfer = Transfer.new(options)
20
+ if (state = options['state'])
21
+ transfer.sync_state(state)
22
+ else
23
+ transfer.sync
24
+ end
25
+ end
26
+ end
27
+
28
+ class Transfer
29
+ attr_reader :options
30
+ def initialize(options)
31
+ @options = options
32
+ STDOUT.sync = true
33
+ open_connection
34
+ end
35
+
36
+ def sync
37
+ states = scraper.states
38
+ total = 0
39
+ states.keys.each do |state|
40
+ total += sync_state(state)
41
+ end
42
+ puts "Total #{total} synchronized"
43
+ end
44
+
45
+ def sync_state(state)
46
+ courts = scraper.contacts_for(state)
47
+ puts "Syncing #{state} #{courts.length} courts"
48
+ sync_courts(courts)
49
+ courts.length
50
+ end
51
+
52
+ def sync_courts(courts)
53
+ courts.each_with_index do |court, index|
54
+ OpencrxCourt.sync(court)
55
+ puts if verbose && ((index+1) % 20 == 0)
56
+ putc('.') if verbose
57
+ end
58
+ puts if verbose
59
+ end
60
+
61
+ private
62
+
63
+ def verbose
64
+ options.has_key?('verbose')
65
+ end
66
+
67
+ def open_connection
68
+ Opencrx::connect(options['url'], options['user'], options['password'])
69
+ end
70
+
71
+ def scraper
72
+ @scraper ||= Justiz::Scraper::Courts.new
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,126 @@
1
+ require 'opencrx'
2
+
3
+ module JustizSync
4
+
5
+ class OpencrxCourt
6
+ attr_reader :court
7
+
8
+ class << self
9
+
10
+ def sync(justiz_court)
11
+ OpencrxCourt.new(justiz_court).sync
12
+ end
13
+
14
+ def find(id)
15
+ query = id_to_query(id)
16
+ Opencrx::Model::LegalEntity.query(query).first
17
+ end
18
+
19
+ # court.id is stored in openCRX userString1
20
+ def id_to_query(id)
21
+ {query: "thereExistsUserString1().equalTo(\"#{id}\")"}
22
+ end
23
+ end
24
+
25
+
26
+ def initialize(court)
27
+ @court = court
28
+ end
29
+
30
+ def sync
31
+ entity.name = court.court
32
+ entity.aliasName = court.justiz_id
33
+ entity.save
34
+
35
+ court_address = court.location_address
36
+ process_address(Opencrx::Model::PostalAddress, usage(:visitor), court_address.city.blank?) do |address|
37
+ address.postalStreet = court_address.street
38
+ address.postalCode = court_address.plz
39
+ address.postalCity = court_address.city
40
+ address.postalCountry = country(:de)
41
+ end
42
+
43
+ court_address = court.post_address
44
+ process_address(Opencrx::Model::PostalAddress, usage(:business), court_address.city.blank?) do |address|
45
+ address.postalStreet = court_address.street
46
+ address.postalCode = court_address.plz
47
+ address.postalCity = court_address.city
48
+ address.postalCountry = country(:de)
49
+ end
50
+
51
+ process_address(Opencrx::Model::PhoneNumber, usage(:business), court.phone.blank?) do |address|
52
+ address.phoneNumberFull = court.phone
53
+ address.automaticParsing = true
54
+ end
55
+
56
+ process_address(Opencrx::Model::PhoneNumber, usage(:fax), court.fax.blank?) do |address|
57
+ address.phoneNumberFull = court.fax
58
+ address.automaticParsing = true
59
+ end
60
+
61
+ process_address(Opencrx::Model::EMailAddress, usage(:business), court.email.blank?) do |address|
62
+ address.emailAddress = court.email
63
+ end
64
+
65
+ process_address(Opencrx::Model::WebAddress, usage(:business), court.url.blank?) do |address|
66
+ address.webUrl = court.url
67
+ end
68
+ end
69
+
70
+ def addresses
71
+ @addresses ||= entity.query(:address, self.class.id_to_query(court.id))
72
+ end
73
+
74
+ def entity
75
+ @entity ||= find_entity || create_entity
76
+ end
77
+
78
+ private
79
+
80
+ def process_address(klass, usage, destroy, &block)
81
+ address = find_address(klass, usage)
82
+ if destroy
83
+ address.destroy if address
84
+ return
85
+ end
86
+ address ||= create_address(klass, usage)
87
+ update_address(address, &block)
88
+ end
89
+
90
+ def update_address(address, &block)
91
+ original_attrs = address.attributes.dup
92
+ address.isMain = true
93
+ yield address
94
+ address.save if address.attributes != original_attrs
95
+ end
96
+
97
+ def find_address(klass, usage)
98
+ addresses.find do |address|
99
+ address.usage.include?(usage.to_s) && address.class == klass
100
+ end
101
+ end
102
+
103
+ def create_address(klass, usage)
104
+ address = klass.new(usage: usage, userString1: court.id)
105
+ address.assign_to(entity)
106
+ address
107
+ end
108
+
109
+ def usage(name)
110
+ Opencrx::Model::Address::USAGE[name]
111
+ end
112
+
113
+ def country(name)
114
+ Opencrx::Model::PostalAddress::COUNTRY[name]
115
+ end
116
+
117
+ def find_entity
118
+ self.class.find(court.id)
119
+ end
120
+
121
+ def create_entity
122
+ Opencrx::Model::LegalEntity.new(name: court.court, userString1: court.id).save
123
+ end
124
+
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module JustizSync
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "justiz_sync/version"
2
+ require 'justiz_sync/opencrx_court'
3
+ require 'justiz_sync/cli'
4
+
5
+ module JustizSync
6
+ end
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe JustizSync::Cli do
4
+ let(:vcr_options) { {record: :new_episodes, match_requests_on: [:method, :path, :query]} }
5
+
6
+ it "syncs all records" do
7
+ VCR.use_cassette('cli/sync', vcr_options) do
8
+ JustizSync::Cli.start(%w(sync --verbose --state=BRD))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,75 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe JustizSync::OpencrxCourt do
4
+ before do
5
+ Opencrx::connect("http://localhost:8080", "guest", "guest")
6
+ end
7
+
8
+ context "sync" do
9
+ let(:court) { OpenStruct.new(court: 'Staatsanwaltschaft Düsseldorf',
10
+ location_address: OpenStruct.new(
11
+ street: 'Fritz-Roeber-Straße 2', plz: '40213', city: 'Düsseldorf'),
12
+ post_address: OpenStruct.new(
13
+ street: 'P.O.Box 123', plz: '40999', city: 'Düsseldorf - Post'),
14
+ phone: '0211 6025 0',
15
+ fax: '0211 6025 2929',
16
+ justiz_id: 'R1100S',
17
+ url: 'http://www.sta-duesseldorf.nrw.de',
18
+ email: 'poststelle@sta-duesseldorf.nrw.de',
19
+ id: 'Staatsanwaltschaft Düsseldorf/poststelle@sta-duesseldorf.nrw.de'
20
+ ) }
21
+
22
+ let(:vcr_options) { {match_requests_on: [:method, :path, :query, :body]} }
23
+
24
+ def find_attribute(addresses, name)
25
+ addresses.map do |address|
26
+ address.attribute(name)
27
+ end.compact
28
+ end
29
+
30
+ def match_court(court, crx)
31
+ #ap crx.compact
32
+ #ap crx.addresses.map(&:compact)
33
+
34
+ expect(crx.name).to eq(court.court)
35
+ expect(crx.aliasName).to eq(court.justiz_id)
36
+ expect(crx.attribute(:userString1)).to eq(court.id)
37
+
38
+ addresses = crx.addresses
39
+
40
+ expect(find_attribute(addresses, :phoneNumberFull)).to include(court.phone, court.fax)
41
+ expect(find_attribute(addresses, :postalCode)).to include(court.post_address.plz, court.location_address.plz)
42
+ expect(find_attribute(addresses, :webUrl)).to eq([court.url])
43
+ expect(find_attribute(addresses, :emailAddress)).to eq([court.email])
44
+ end
45
+
46
+ it "should create a court" do
47
+ VCR.use_cassette('create', vcr_options) do
48
+ JustizSync::OpencrxCourt.sync(court)
49
+ crx = JustizSync::OpencrxCourt.find(court.id)
50
+ match_court(court, crx)
51
+ end
52
+ end
53
+
54
+ it "should find court" do
55
+ VCR.use_cassette('find', vcr_options) do
56
+ crx = JustizSync::OpencrxCourt.find(court.id)
57
+ match_court(court, crx)
58
+ end
59
+ end
60
+
61
+ it "should update a court" do
62
+ VCR.use_cassette('update', vcr_options) do
63
+ court.court += ' Update'
64
+ court.post_address.plz += ' Update'
65
+ court.url += ' Update
66
+ '
67
+ JustizSync::OpencrxCourt.sync(court)
68
+ crx = JustizSync::OpencrxCourt.find(court.id)
69
+
70
+ expect(court.court).to match(/Update/)
71
+ match_court(court, crx)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require 'justiz_sync'
3
+ require 'awesome_print'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/vcr'
8
+ c.hook_into :webmock
9
+ end