opencrx 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2784d3b61592f54c1152b468fff93a4642d06e7
4
+ data.tar.gz: 45d07b8b6bbc135cf3e6fe5ac30bba03ad756e30
5
+ SHA512:
6
+ metadata.gz: 7489279b7e307aba8727fd2cc77e83ae8224571a9938b813abf53e1af7f14557c6e919552fb0a176d006d3ce9a418a44a45656c0f3d2adfd271e1ffb29cac419
7
+ data.tar.gz: d1ba221c1eddf2359743f7909b64dfa200851feacabda2f67bf6464e56dc2ce2b2b3d3d1446523413efa392f03f2cbef8fe510c7638aa1714f2d85b29b7957c4
data/.rvmrc ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.0.0" > .rvmrc
9
+ environment_id="ruby-2.0.0-p195@justiz"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.20.13 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: \E[32m$GEM_HOME\E[0m" # show the user the ruby and gemset they are using in green
37
+ else printf "%b" "Using: $GEM_HOME" # don't use colors in non-interactive shells
38
+ fi
39
+ fi
40
+ else
41
+ # If the environment file has not yet been created, use the RVM CLI to select.
42
+ rvm --create "$environment_id" || {
43
+ echo "Failed to create RVM environment '${environment_id}'."
44
+ return 1
45
+ }
46
+ fi
47
+
48
+ # If you use bundler, this might be useful to you:
49
+ # if [[ -s Gemfile ]] && {
50
+ # ! builtin command -v bundle >/dev/null ||
51
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
52
+ # }
53
+ # then
54
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
55
+ # gem install bundler
56
+ # fi
57
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
58
+ # then
59
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
60
+ # fi
@@ -1,141 +1,22 @@
1
- require "opencrx/version"
2
- require 'httparty'
3
- require 'active_support/inflector'
1
+ require 'opencrx/version'
2
+ require 'opencrx/session'
3
+ require 'opencrx/model'
4
4
  require 'active_support/core_ext'
5
5
 
6
6
  module Opencrx
7
+ class << self
8
+ attr_accessor :logger, :session
7
9
 
8
- @@session = nil
9
-
10
- def self.connect(base_url, user, password)
11
- self.session = Opencrx::Session.new(base_url, user, password)
12
- end
13
-
14
- def self.session
15
- @@session
16
- end
17
-
18
- def self.session=(object)
19
- @@session = object
20
- end
21
-
22
- class Session
23
- include HTTParty
24
-
25
- #debug_output
26
- headers 'Accept' => 'text/xml', 'Content-Type' => 'text/xml'
27
- format :xml
28
-
29
- def initialize(base_url, user, password)
30
- self.class.base_uri(base_url)
31
- self.class.basic_auth(user, password)
32
- end
33
-
34
- def get(url, options = {})
35
- self.class.get(url, options)
36
- end
37
-
38
- def put(url, options = {})
39
- self.class.put(url, options)
40
- end
41
-
42
- def post(url, options = {})
43
- self.class.post(url, options)
44
- end
45
- end
46
-
47
- module Account
48
- SUFFIX = "/opencrx-rest-CRX/org.opencrx.kernel.account1/provider/CRX/segment/Standard/account"
49
- KEY = Regexp.new('^org.opencrx.kernel.account1.(.*)$')
50
- RESULTSET = 'org.openmdx.kernel.ResultSet'
51
-
52
- def self.get(id)
53
- response = session.get(SUFFIX + "/#{id}")
54
- #ap Nokogiri.XML(response.body)
55
- build_record(response)
56
- end
57
-
58
- def self.query(params = {})
59
- response = session.get(SUFFIX, query: params)
60
- key = response.keys.first
61
- if key == RESULTSET
62
- parse_resultset(response[key])
63
- else
64
- failure(response)
65
- end
66
- end
67
-
68
- def self.post(xml)
69
- build_record session.post(SUFFIX, body: xml)
10
+ def connect(base_url, user, password)
11
+ self.session = Opencrx::Session.new(base_url, user, password)
70
12
  end
71
13
 
72
- def self.put(url, xml)
73
- build_record session.put(url, body: xml)
74
- end
75
-
76
- def self.session
77
- Opencrx::session
78
- end
79
-
80
- def self.parse_resultset(hash)
81
- hash.map do |key, value|
82
- next if %w(href hasMore).include? key
83
- build_record(key => value)
84
- end.compact
85
- end
86
-
87
- def self.build_record(hash)
88
- puts "Expected a single key, got [#{hash.keys}]" unless hash.keys.length == 1
89
- key = hash.keys.first
90
- key.match(KEY)
91
- klass_name = $1
92
- if (klass = ActiveSupport::Inflector.safe_constantize("Opencrx::Account::#{klass_name}"))
93
- klass.new(hash)
94
- else
95
- puts "Dont understand record type [#{klass_name}]"
96
- end
97
- end
98
-
99
- def self.failure(response)
100
- ap response.request
101
- ap response.response
102
- ap response.parsed_response
103
- nil
104
- end
105
-
106
- class AccountRecord < SimpleDelegator
107
- BASEKEY = "org.opencrx.kernel.account1"
108
-
109
- def initialize(attributes)
110
- attributes = attributes[key] if attributes.has_key? key
111
- super attributes
112
- end
113
-
114
- def key
115
- "#{BASEKEY}.#{ActiveSupport::Inflector.demodulize self.class}"
116
- end
117
-
118
- def save
119
- if has_key?('href')
120
- Opencrx::Account.put(fetch('href'), xml)
121
- else
122
- Opencrx::Account.post(xml)
123
- end
124
- end
125
-
126
- def xml
127
- __getobj__.except('owner', 'owningUser', 'owningGroup', 'href', 'version').to_xml(root: key)
14
+ def logger
15
+ @logger ||= begin
16
+ logger = Logger.new(STDERR)
17
+ logger.level = Logger::WARN
18
+ logger
128
19
  end
129
20
  end
130
-
131
- class Contact < AccountRecord
132
- end
133
- class Group < AccountRecord
134
- end
135
- class UnspecifiedAccount < AccountRecord
136
- end
137
- class LegalEntity < AccountRecord
138
- end
139
21
  end
140
-
141
22
  end
@@ -0,0 +1,12 @@
1
+ require 'opencrx/model/map'
2
+ require 'opencrx/model/result'
3
+ require 'opencrx/model/result_set'
4
+ require 'opencrx/model/record'
5
+ require 'opencrx/model/account'
6
+ require 'opencrx/model/address'
7
+
8
+ module Opencrx
9
+ module Model
10
+ BASE_KEY = "org.opencrx.kernel.account1"
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ module Opencrx
2
+ module Model
3
+ class Account < Record
4
+ have_attributes :aliasName
5
+
6
+ def self.provider
7
+ "#{base_provider}/account"
8
+ end
9
+
10
+ # subrecords of ours include :address
11
+ def addresses
12
+ query(:address)
13
+ end
14
+ end
15
+
16
+ class FilteredAccount < Account
17
+ def self.default_query_options
18
+ query_type_option
19
+ end
20
+ end
21
+
22
+ class LegalEntity < FilteredAccount
23
+ have_attributes :name
24
+ end
25
+
26
+ class Contact < FilteredAccount; end
27
+ class Group < FilteredAccount; end
28
+ class UnspecifiedAccount < FilteredAccount; end
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ module Opencrx
2
+ module Model
3
+ class Address < Record
4
+ # usage is a code
5
+ USAGE = {
6
+ mobile: "200",
7
+ business: "500",
8
+ fax: "530",
9
+ visitor: "10500",
10
+ other: "1800",
11
+ delivery: "10200"
12
+ }
13
+ have_attributes :isMain, :disabled
14
+ have_array_attributes :usage
15
+
16
+ def self.provider
17
+ "#{base_provider}/address"
18
+ end
19
+
20
+ def assign_to(parent)
21
+ self.href = "#{parent.href}/address"
22
+ self.identity = nil
23
+ end
24
+ end
25
+
26
+ class PostalAddress < Address
27
+ # postalCountry is a code
28
+ COUNTRY = {
29
+ de: "276"
30
+ }
31
+ have_attributes :postalCode, :postalCity, :postalState, :postalCountry
32
+ have_array_attributes :postalAddressLine, :postalStreet
33
+ end
34
+
35
+ class EMailAddress < Address
36
+ have_attributes :emailAddress
37
+ end
38
+
39
+ class PhoneNumber < Address
40
+ have_attributes :phoneNumberFull, :automaticParsing
41
+ end
42
+
43
+ class WebAddress < Address
44
+ have_attributes :webUrl
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Opencrx
4
+ module Model
5
+ module Map
6
+ class << self
7
+ def opencrx_key_to_model(key)
8
+ unless key.match(/^#{BASE_KEY}\.(.*)$/)
9
+ raise "Unexpected key #{key}"
10
+ end
11
+ target_class_name = $1
12
+ ActiveSupport::Inflector.safe_constantize("::Opencrx::Model::#{target_class_name}")
13
+ end
14
+
15
+ def model_to_opencrx_key(klass)
16
+ "#{BASE_KEY}.#{demodulized_class_name(klass)}"
17
+ end
18
+
19
+ def model_to_opencrx_query(klass)
20
+ model_to_opencrx_key(klass).gsub(/\./, ':')
21
+ end
22
+
23
+ private
24
+
25
+ def demodulized_class_name(klass)
26
+ ActiveSupport::Inflector.demodulize klass
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'opencrx/model/record/attributes'
2
+ require 'opencrx/model/record/query'
3
+
4
+ module Opencrx
5
+ module Model
6
+ class Record
7
+ have_attributes :href, :identity
8
+
9
+ def save
10
+ href = attributes['href'] || self.class.query_url
11
+ response = if attributes['identity']
12
+ Opencrx::session.put(href, body: to_xml)
13
+ else
14
+ Opencrx::session.post(href, body: to_xml)
15
+ end
16
+ Result.parse(response)
17
+ end
18
+
19
+ # TODO implement delete
20
+ def destroy
21
+ href = attributes['href']
22
+ # this errors with No active unit of work
23
+ #Opencrx::session.delete(href, body: to_xml)
24
+ # this errors with Premature end of file.
25
+ #Opencrx::session.delete(href)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,82 @@
1
+ module Opencrx
2
+ module Model
3
+ class Record
4
+
5
+ def self.have_attributes(*args)
6
+ args.each do |arg|
7
+ define_method(arg) do
8
+ attribute(arg)
9
+ end
10
+ define_method("#{arg}=") do |value|
11
+ write_attribute(arg, value)
12
+ end
13
+ end
14
+ end
15
+
16
+ def self.have_array_attributes(*args)
17
+ args.each do |arg|
18
+ define_method(arg) do
19
+ Array.wrap(attribute(arg))
20
+ end
21
+ define_method("#{arg}=") do |value|
22
+ write_attribute(arg, Array.wrap(value))
23
+ end
24
+ end
25
+ end
26
+
27
+ class ItemList < Array
28
+ def to_xml(options = nil)
29
+ super(options.merge(children: '_item'))
30
+ end
31
+ end
32
+
33
+ attr_accessor :attributes
34
+
35
+ def initialize(attributes = {})
36
+ self.attributes = attributes
37
+ end
38
+
39
+ def attributes=(hash)
40
+ @attributes = {}
41
+ hash.each do |key, value|
42
+ write_attribute(key, value)
43
+ end
44
+ end
45
+
46
+ # store only simple values, or simple arrays.
47
+ # arrays from opencrx are encoded as a hash values under key '_item'
48
+ def write_attribute(key, value)
49
+ key = key.to_s
50
+ case value
51
+ # incoming from opencrx
52
+ when Hash
53
+ items = Array.wrap(value['_item'])
54
+ if items.first.is_a?(String)
55
+ @attributes[key] = ItemList.new(items)
56
+ end
57
+ # incoming from our side
58
+ when Array
59
+ @attributes[key] = ItemList.new(value)
60
+ else
61
+ @attributes[key] = value
62
+ end
63
+ @attributes[key]
64
+ end
65
+
66
+ def attribute(key)
67
+ attributes[key.to_s]
68
+ end
69
+
70
+ def compact
71
+ attributes.inject({}) do |memo, (key, value)|
72
+ memo[key] = value if value.present? && value != '0'
73
+ memo
74
+ end
75
+ end
76
+
77
+ def to_xml(options = {})
78
+ attributes.except('href', 'version').to_xml(options.merge(root: Map.model_to_opencrx_key(self.class)))
79
+ end
80
+ end
81
+ end
82
+ end