civicrm 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +18 -20
- data/README.md +0 -5
- data/civicrm.gemspec +1 -1
- data/lib/civicrm.rb +2 -7
- data/lib/civicrm/actions/destroy.rb +10 -2
- data/lib/civicrm/client.rb +23 -32
- data/lib/civicrm/profiling.rb +2 -0
- data/lib/civicrm/version.rb +1 -1
- data/spec/requests/base_spec.rb +1 -10
- data/spec/support/civicrm_responses.rb +1 -1
- data/spec/support/test_matchers.rb +4 -4
- metadata +4 -5
- data/lib/civicrm/xml.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b324b4218b63ab0e45d5d97f6524a4352bc34d9
|
4
|
+
data.tar.gz: db21f89583ef7ad9d9f05b25316569f11f0147d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 351a3d9c51988a98df4df75b8e40c575af6845bf6f256a3db12443a6c9ab01a617e70edde104bf04e130df601471ba87d81a37b3c63f9bbaea1f0a56af26d137
|
7
|
+
data.tar.gz: 84c42c92bbe8e32fde0730eacd101f64192635fb06b3a1c7faeda9b14d86748aadc25dec951c9da14710ddb5b9b97ab6a74453ff3a03529c254f7db0099cc98a
|
data/Gemfile.lock
CHANGED
@@ -1,46 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
civicrm (1.
|
4
|
+
civicrm (1.1.1)
|
5
5
|
activemodel
|
6
6
|
activesupport
|
7
7
|
nokogiri
|
8
|
-
rest-client (~>
|
8
|
+
rest-client (~> 2.0)
|
9
9
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activemodel (
|
14
|
-
activesupport (=
|
15
|
-
|
16
|
-
|
17
|
-
i18n (
|
18
|
-
json (~> 1.7, >= 1.7.7)
|
13
|
+
activemodel (5.2.1)
|
14
|
+
activesupport (= 5.2.1)
|
15
|
+
activesupport (5.2.1)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
19
18
|
minitest (~> 5.1)
|
20
|
-
thread_safe (~> 0.1)
|
21
19
|
tzinfo (~> 1.1)
|
22
|
-
builder (3.2.3)
|
23
20
|
byebug (10.0.2)
|
24
|
-
concurrent-ruby (1.
|
21
|
+
concurrent-ruby (1.1.4)
|
25
22
|
diff-lcs (1.3)
|
26
23
|
domain_name (0.5.20180417)
|
27
24
|
unf (>= 0.0.5, < 1.0.0)
|
28
25
|
http-cookie (1.0.3)
|
29
26
|
domain_name (~> 0.5)
|
30
|
-
i18n (
|
27
|
+
i18n (1.1.1)
|
31
28
|
concurrent-ruby (~> 1.0)
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
mime-types (3.1)
|
30
|
+
mime-types-data (~> 3.2015)
|
31
|
+
mime-types-data (3.2016.0521)
|
32
|
+
mini_portile2 (2.4.0)
|
35
33
|
minitest (5.11.3)
|
36
34
|
netrc (0.11.0)
|
37
|
-
nokogiri (1.
|
38
|
-
mini_portile2 (~> 2.
|
35
|
+
nokogiri (1.10.1)
|
36
|
+
mini_portile2 (~> 2.4.0)
|
39
37
|
rake (10.0.3)
|
40
|
-
rest-client (
|
38
|
+
rest-client (2.0.2)
|
41
39
|
http-cookie (>= 1.0.2, < 2.0)
|
42
|
-
mime-types (>= 1.16, <
|
43
|
-
netrc (~> 0.
|
40
|
+
mime-types (>= 1.16, < 4.0)
|
41
|
+
netrc (~> 0.8)
|
44
42
|
rspec (3.8.0)
|
45
43
|
rspec-core (~> 3.8.0)
|
46
44
|
rspec-expectations (~> 3.8.0)
|
data/README.md
CHANGED
@@ -12,11 +12,6 @@ $ gem install civicrm
|
|
12
12
|
# Required config
|
13
13
|
CiviCrm.api_base = "https://www.example.org/path/to/civi/codebase/"
|
14
14
|
CiviCrm.site_key = "YOUR_SITE_KEY"
|
15
|
-
|
16
|
-
# Authenticate with username/password
|
17
|
-
CiviCrm.authenticate("username", "password")
|
18
|
-
|
19
|
-
# OR, authenticate with API key
|
20
15
|
CiviCrm.api_key = "..."
|
21
16
|
```
|
22
17
|
|
data/civicrm.gemspec
CHANGED
@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s|
|
|
13
13
|
s.executables = 'civicrm'
|
14
14
|
s.require_paths = %w{lib}
|
15
15
|
|
16
|
-
s.add_dependency('rest-client', '~>
|
16
|
+
s.add_dependency('rest-client', '~> 2.0')
|
17
17
|
s.add_dependency('activesupport')
|
18
18
|
s.add_dependency('activemodel')
|
19
19
|
s.add_dependency('nokogiri')
|
data/lib/civicrm.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
require 'active_support/deprecation'
|
2
4
|
require 'active_support/core_ext/module'
|
3
5
|
require 'active_support/core_ext/hash'
|
@@ -7,7 +9,6 @@ require 'nokogiri'
|
|
7
9
|
|
8
10
|
# utils
|
9
11
|
require 'civicrm/client'
|
10
|
-
require 'civicrm/xml'
|
11
12
|
require 'civicrm/resource'
|
12
13
|
require 'civicrm/profiling'
|
13
14
|
require 'civicrm/version'
|
@@ -34,7 +35,6 @@ module CiviCrm
|
|
34
35
|
@@site_key = nil
|
35
36
|
@@api_base = 'https://www.example.org/path/to/civi/codebase'
|
36
37
|
@@api_version = 'v3'
|
37
|
-
@@user_authenticated = false
|
38
38
|
|
39
39
|
mattr_accessor :api_key, :api_base, :api_version, :site_key
|
40
40
|
|
@@ -45,11 +45,6 @@ module CiviCrm
|
|
45
45
|
base
|
46
46
|
end
|
47
47
|
|
48
|
-
def self.authenticate(name, password)
|
49
|
-
auth = Client.request(:post, q: 'civicrm/login', name: name, pass: password)
|
50
|
-
@@api_key = auth[0]['api_key']
|
51
|
-
end
|
52
|
-
|
53
48
|
def self.api_key=(key)
|
54
49
|
@@api_key = key
|
55
50
|
end
|
@@ -7,8 +7,12 @@ module CiviCrm
|
|
7
7
|
'action' => 'delete',
|
8
8
|
'id' => id
|
9
9
|
}
|
10
|
+
|
10
11
|
response = CiviCrm::Client.request(:post, params)
|
11
|
-
|
12
|
+
|
13
|
+
if response < 1
|
14
|
+
raise Error, "Couldn't delete #{entity_class_name}##{id}"
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
18
|
def delete!
|
@@ -18,8 +22,12 @@ module CiviCrm
|
|
18
22
|
'skip_undelete' => 1,
|
19
23
|
'id' => id
|
20
24
|
}
|
25
|
+
|
21
26
|
response = CiviCrm::Client.request(:post, params)
|
22
|
-
|
27
|
+
|
28
|
+
if response < 1
|
29
|
+
raise Error, "Couldn't delete #{entity_class_name}##{id}"
|
30
|
+
end
|
23
31
|
end
|
24
32
|
end
|
25
33
|
end
|
data/lib/civicrm/client.rb
CHANGED
@@ -5,27 +5,32 @@ module CiviCrm
|
|
5
5
|
# Returns parsed class inherited from CiviCrm::Resource
|
6
6
|
def request(method, params = {})
|
7
7
|
unless CiviCrm.site_key
|
8
|
-
raise CiviCrm::Errors::Unauthorized,
|
8
|
+
raise CiviCrm::Errors::Unauthorized,
|
9
|
+
"Please specify CiviCrm.site_key"
|
9
10
|
end
|
11
|
+
|
10
12
|
headers = {
|
11
13
|
:user_agent => "CiviCrm RubyClient/#{CiviCrm::VERSION}"
|
12
14
|
}
|
13
15
|
|
14
16
|
opts = {
|
15
|
-
:method =>
|
17
|
+
:method => :post,
|
16
18
|
:timeout => 80,
|
17
19
|
:headers => headers
|
18
20
|
}
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
else
|
25
|
-
opts[:payload] = stringify_params(params)
|
26
|
-
end
|
22
|
+
params = params.dup
|
23
|
+
|
24
|
+
entity = params.delete("entity")
|
25
|
+
raise ArgumentError, "params must include entity" unless entity
|
27
26
|
|
28
|
-
|
27
|
+
action = params.delete("action")
|
28
|
+
raise ArgumentError, "params must include action" unless action
|
29
|
+
|
30
|
+
method = { entity: entity, action: action }.to_query
|
31
|
+
|
32
|
+
opts[:payload] = { json: JSON.dump(params) }
|
33
|
+
opts[:url] = CiviCrm.api_url(method)
|
29
34
|
|
30
35
|
response = nil
|
31
36
|
|
@@ -33,16 +38,19 @@ module CiviCrm
|
|
33
38
|
response = execute(opts)
|
34
39
|
end
|
35
40
|
|
36
|
-
puts(JSON.dump(
|
41
|
+
puts(JSON.dump(opts)) if ENV["DEBUG_CIVICRM_REQUEST"]
|
37
42
|
puts(response) if ENV["DEBUG_CIVICRM_RESPONSE"]
|
38
43
|
|
39
44
|
body, code = response.body, response.code
|
40
45
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
46
|
+
parsed_response = JSON.parse(body)
|
47
|
+
|
48
|
+
if parsed_response["is_error"] == 1
|
49
|
+
raise Error, parsed_response["error_message"]
|
45
50
|
end
|
51
|
+
|
52
|
+
values = parsed_response.fetch("values")
|
53
|
+
values.is_a?(Hash) ? values.values : values
|
46
54
|
end
|
47
55
|
|
48
56
|
def execute(opts)
|
@@ -63,23 +71,6 @@ module CiviCrm
|
|
63
71
|
raise e
|
64
72
|
end
|
65
73
|
end
|
66
|
-
|
67
|
-
def uri_escape(key)
|
68
|
-
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
69
|
-
end
|
70
|
-
|
71
|
-
def flatten_params(params, parent_key = nil)
|
72
|
-
result = []
|
73
|
-
params.each do |key, value|
|
74
|
-
flatten_key = parent_key ? "#{parent_key}[#{uri_escape(key)}]" : uri_escape(key)
|
75
|
-
result += value.is_a?(Hash) ? flatten_params(value, flatten_key) : [[flatten_key, value]]
|
76
|
-
end
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
|
-
def stringify_params(params)
|
81
|
-
flatten_params(params).collect{|key, value| "#{key}=#{uri_escape(value)}"}.join('&')
|
82
|
-
end
|
83
74
|
end
|
84
75
|
end
|
85
76
|
end
|
data/lib/civicrm/profiling.rb
CHANGED
data/lib/civicrm/version.rb
CHANGED
data/spec/requests/base_spec.rb
CHANGED
@@ -7,19 +7,10 @@ describe "API Bindings" do
|
|
7
7
|
let(:client){ authorized_civicrm_client }
|
8
8
|
|
9
9
|
it "should not fetch from network while initializing a new Resource" do
|
10
|
-
expect(client).not_to receive(:
|
10
|
+
expect(client).not_to receive(:post)
|
11
11
|
CiviCrm::Contact.new(id: "someid")
|
12
12
|
end
|
13
13
|
|
14
|
-
it "should run authentication" do
|
15
|
-
expect(client).
|
16
|
-
to receive(:post).
|
17
|
-
exactly(:once).
|
18
|
-
and_return(test_response(api_key: 'test'))
|
19
|
-
|
20
|
-
CiviCrm.authenticate("test", "test")
|
21
|
-
end
|
22
|
-
|
23
14
|
describe "exception handler" do
|
24
15
|
let(:client){ CiviCrm::Client }
|
25
16
|
|
@@ -5,7 +5,7 @@ RSpec::Matchers.define :be_listable_resource do |expected|
|
|
5
5
|
test_response_hash = send(:"test_#{subject.name.demodulize.underscore}_array")
|
6
6
|
|
7
7
|
expect(client).
|
8
|
-
to receive(:
|
8
|
+
to receive(:post).
|
9
9
|
exactly(:once).
|
10
10
|
and_return(test_response(test_response_hash))
|
11
11
|
|
@@ -19,7 +19,7 @@ RSpec::Matchers.define :be_updatable_resource do |expected|
|
|
19
19
|
subject = actual.class
|
20
20
|
|
21
21
|
expect(client).
|
22
|
-
to receive(:
|
22
|
+
to receive(:post).
|
23
23
|
exactly(:once).
|
24
24
|
and_return(test_response(test_contact({name: "foo"})))
|
25
25
|
|
@@ -43,12 +43,12 @@ RSpec::Matchers.define :be_deleteable_resource do |expected|
|
|
43
43
|
subject = actual.class
|
44
44
|
|
45
45
|
expect(client).
|
46
|
-
to receive(:
|
46
|
+
to receive(:post).
|
47
47
|
exactly(:once).
|
48
48
|
and_return(test_response(test_contact({name: "foo"})))
|
49
49
|
|
50
50
|
expect(client).
|
51
|
-
to receive(:
|
51
|
+
to receive(:post).
|
52
52
|
exactly(:once).
|
53
53
|
and_return(test_response(test_contact({name: "bar"})))
|
54
54
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: civicrm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iskander Haziev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -140,7 +140,6 @@ files:
|
|
140
140
|
- lib/civicrm/resource.rb
|
141
141
|
- lib/civicrm/resources/base.rb
|
142
142
|
- lib/civicrm/version.rb
|
143
|
-
- lib/civicrm/xml.rb
|
144
143
|
- spec/requests/base_spec.rb
|
145
144
|
- spec/resources/contact_spec.rb
|
146
145
|
- spec/spec_helper.rb
|
data/lib/civicrm/xml.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
module CiviCrm
|
2
|
-
class XML
|
3
|
-
class << self
|
4
|
-
def parse(text)
|
5
|
-
# CiviCRM <Result>s sometimes contain invalid elements
|
6
|
-
# like <preferred_communication_method><0></0></ ...
|
7
|
-
# Get rid of them before parsing.
|
8
|
-
fixed_text = text.to_s.
|
9
|
-
gsub("\n", "").
|
10
|
-
gsub(/<\d+>(.*?)<\/\d+>/, "")
|
11
|
-
|
12
|
-
doc = Nokogiri::XML.parse(fixed_text)
|
13
|
-
|
14
|
-
results = doc.xpath('//Result')
|
15
|
-
results.map do |result|
|
16
|
-
hash = {}
|
17
|
-
result.children.each do |attribute|
|
18
|
-
next unless attribute.is_a?(Nokogiri::XML::Element)
|
19
|
-
hash[attribute.name] = attribute.children[0].text rescue nil
|
20
|
-
end
|
21
|
-
hash
|
22
|
-
end
|
23
|
-
end
|
24
|
-
def encode(resources)
|
25
|
-
builder = Nokogiri::XML::Builder.new do |xml|
|
26
|
-
xml.ResultSet do
|
27
|
-
Array.wrap(resources).each do |resource|
|
28
|
-
attributes = resource.respond_to?(:attributes) ? resource.attributes : resource
|
29
|
-
xml.Result do
|
30
|
-
attributes.each do |key, value|
|
31
|
-
xml.send key.to_sym, value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
builder.to_xml
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|