Skip to content

Commit

Permalink
Compatibility with CivciCRM API v4 (#36)
Browse files Browse the repository at this point in the history
* Compatibility with CivciCRM API v4

* Change API version variable

* Show display name instead of email in contact displaoy name

* Added name and nickname parsers

* Added tests for API v4

* Pinning chrome version to v119

* Improved parsers methods and requires ordered

* Added test for oauth response parsers

* Fix namespace

---------

Co-authored-by: Francisco Bolívar <francisco.bolivar@nazaries.com>
  • Loading branch information
alexrlpz and fblupi authored Jul 29, 2024
1 parent fcfe186 commit a62627b
Show file tree
Hide file tree
Showing 110 changed files with 2,265 additions and 431 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ jobs:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true

- uses: nanasess/setup-chromedriver@v2
with:
chromedriver-version: 119.0.6045.105

- name: List Chrome
shell: "bash"
run: apt list --installed | grep chrome

- name: Remove Chrome
shell: "bash"
run: sudo apt remove google-chrome-stable

- uses: browser-actions/setup-chrome@v1
with:
chrome-version: 119.0.6045.105

- name: Setup Database
run: bundle exec rake test_app

Expand Down
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
inherit_from: https://raw.githubusercontent.com/decidim/decidim/release/0.27-stable/.rubocop.yml

RSpec/AnyInstance:
Exclude:
- 'spec/omni_auth/strategies/civicrm_spec.rb'
53 changes: 46 additions & 7 deletions lib/decidim/civicrm/api.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
# frozen_string_literal: true

require_relative "api/base/base_query"
require_relative "api/base/find_query"
require_relative "api/base/list_query"
require_relative "api/base/request"
require_relative "api/base/v3/base_query"
require_relative "api/base/v3/find_query"
require_relative "api/base/v3/list_query"
require_relative "api/base/v3/request"
require_relative "api/base/v4/base_query"
require_relative "api/base/v4/find_query"
require_relative "api/base/v4/list_query"
require_relative "api/base/v4/request"
require_relative "api/find_contact"
require_relative "api/find_group"
require_relative "api/find_user"
require_relative "api/find_event"
require_relative "api/find_group"
require_relative "api/find_participant"
require_relative "api/participants_in_event"
require_relative "api/find_user"
require_relative "api/list_contact_groups"
require_relative "api/list_contacts_in_group"
require_relative "api/list_contact_memberships"
require_relative "api/list_contacts_in_group"
require_relative "api/list_groups"
require_relative "api/list_membership_types"
require_relative "api/participants_in_event"
require_relative "api/request"
require_relative "api/v3/find_contact"
require_relative "api/v3/find_event"
require_relative "api/v3/find_group"
require_relative "api/v3/find_participant"
require_relative "api/v3/find_user"
require_relative "api/v3/list_contact_groups"
require_relative "api/v3/list_contact_memberships"
require_relative "api/v3/list_contacts_in_group"
require_relative "api/v3/list_groups"
require_relative "api/v3/list_membership_types"
require_relative "api/v3/participants_in_event"
require_relative "api/v4/find_contact"
require_relative "api/v4/find_event"
require_relative "api/v4/find_group"
require_relative "api/v4/find_participant"
require_relative "api/v4/find_user"
require_relative "api/v4/list_contact_groups"
require_relative "api/v4/list_contact_memberships"
require_relative "api/v4/list_contacts_in_group"
require_relative "api/v4/list_groups"
require_relative "api/v4/list_membership_types"
require_relative "api/v4/participants_in_event"

module Decidim
module Civicrm
Expand All @@ -34,6 +62,17 @@ def self.credentials
def self.url
config[:url]
end

def self.version
config[:version]
end

def self.available_versions
@available_versions ||= {
v3: "V3",
v4: "V4"
}
end
end
end
end
13 changes: 0 additions & 13 deletions lib/decidim/civicrm/api/base/base_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,13 @@ def response

protected

def json_params(params)
params.merge(
sequential: 1
).to_json
end

def parsed_response
raise NotImplementedError
end

def default_query
raise NotImplementedError
end

def store_result
return unless success?

@result = parsed_response
@result = @result.deep_symbolize_keys if @result.is_a? Hash
end
end
end
end
Expand Down
23 changes: 0 additions & 23 deletions lib/decidim/civicrm/api/base/find_query.rb

This file was deleted.

24 changes: 0 additions & 24 deletions lib/decidim/civicrm/api/base/list_query.rb

This file was deleted.

53 changes: 0 additions & 53 deletions lib/decidim/civicrm/api/base/request.rb

This file was deleted.

30 changes: 30 additions & 0 deletions lib/decidim/civicrm/api/base/v3/base_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Decidim
module Civicrm
module Api
module Base
module V3
class BaseQuery < Base::BaseQuery
attr_reader :result, :request

protected

def json_params(params)
params.merge(
sequential: 1
).to_json
end

def store_result
return unless success?

@result = parsed_response
@result = @result.deep_symbolize_keys if @result.is_a? Hash
end
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/decidim/civicrm/api/base/v3/find_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Decidim
module Civicrm
module Api
module Base
module V3
class FindQuery < BaseQuery
protected

def json_params(params)
params.deep_merge(
sequential: 1
).to_json
end

def parsed_response
self.class.parse_item(response["values"].first)
end
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/decidim/civicrm/api/base/v3/list_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Decidim
module Civicrm
module Api
module Base
module V3
class ListQuery < BaseQuery
protected

def json_params(params)
params.deep_merge(
sequential: 1,
options: { limit: 0 }
).to_json
end

def parsed_response
response["values"].map { |item| self.class.parse_item(item) }
end
end
end
end
end
end
end
54 changes: 54 additions & 0 deletions lib/decidim/civicrm/api/base/v3/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module Decidim
module Civicrm
module Api
module Base
module V3
class Request
def initialize(verify_ssl: true)
@verify_ssl = verify_ssl
end

attr_accessor :response

def self.get(params, verify_ssl: true)
instance = Request.new(verify_ssl: verify_ssl)
response = instance.connection.get Decidim::Civicrm::Api.url do |request|
request.params = instance.base_params.merge(params)
# puts [request.path, URI.encode_www_form(request.params.sort)].join("/?") # DEBUG, to obtain the correct URL for stub_request
end

raise Decidim::Civicrm::Error, response.reason_phrase unless response.success?

instance.response = JSON.parse(response.body).to_h
instance
end

def self.post(params, verify_ssl: true)
instance = Request.new(verify_ssl: verify_ssl)
response = instance.connection.post Decidim::Civicrm::Api.url do |request|
request.params = instance.base_params.merge(params)
end

raise Decidim::Civicrm::Error, response.reason_phrase unless response.success?

instance.response = JSON.parse(response.body).to_h
instance
end

def connection
@connection ||= Faraday.new(ssl: { verify: @verify_ssl })
end

def base_params
Decidim::Civicrm::Api.credentials.merge(
action: "Get"
)
end
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/decidim/civicrm/api/base/v4/base_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Decidim
module Civicrm
module Api
module Base
module V4
class BaseQuery < Base::BaseQuery
attr_reader :result, :request

def store_result
return unless success?

@result = parsed_response
@result[:values] = @result[:values].deep_symbolize_keys if @result[:values].is_a? Hash
end
end
end
end
end
end
end
Loading

0 comments on commit a62627b

Please sign in to comment.