zendesk2 1.4.1 → 1.4.2
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 +4 -4
- data/Gemfile +1 -1
- data/lib/zendesk2/client/collections/organizations.rb +1 -0
- data/lib/zendesk2/client/mock.rb +11 -5
- data/lib/zendesk2/client/requests/search.rb +4 -7
- data/lib/zendesk2/client/requests/search_help_center_articles.rb +4 -7
- data/lib/zendesk2/client/requests/search_organization.rb +36 -0
- data/lib/zendesk2/client/requests/search_user.rb +2 -5
- data/lib/zendesk2/paged_collection.rb +14 -9
- data/lib/zendesk2/searchable.rb +8 -5
- data/lib/zendesk2/version.rb +1 -1
- data/spec/lib/paged_collection_spec.rb +11 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a75f9d7681dd7fc05b70ffaac7cd40f4d7b0e0d2
|
4
|
+
data.tar.gz: da867a0ca54e8645a7ba70f301810474509dc640
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66a38be00688b1ee608f8898ac88cb96137f056a5eb116039f30ab143994dede0dbba3442c6bcae7503dc94d2d2f45b89a366624787fd2437c6129cc5cb3d354
|
7
|
+
data.tar.gz: 222bf38cead5cbbae73790862d893fbf7bccb1758d075980e03a7da722b7ce2b724d5c05fc8d364ab479a02a30efd9be892d30953349f8ed44be99c6fab1608c
|
data/Gemfile
CHANGED
data/lib/zendesk2/client/mock.rb
CHANGED
@@ -65,8 +65,12 @@ class Zendesk2::Client < Cistern::Service
|
|
65
65
|
File.join(@url, path.to_s)
|
66
66
|
end
|
67
67
|
|
68
|
-
def url_for(path)
|
69
|
-
|
68
|
+
def url_for(path, options={})
|
69
|
+
Addressable::URI.parse(
|
70
|
+
File.join(@url, "/api/v2", path.to_s)
|
71
|
+
).tap do |uri|
|
72
|
+
uri.query_values = options.fetch(:query, {})
|
73
|
+
end.to_s
|
70
74
|
end
|
71
75
|
|
72
76
|
def resources(collection, path, collection_root, options={})
|
@@ -86,7 +90,7 @@ class Zendesk2::Client < Cistern::Service
|
|
86
90
|
|
87
91
|
def page(params, collection, path, collection_root, options={})
|
88
92
|
page_params = Zendesk2.paging_parameters(params)
|
89
|
-
page_size = (page_params["per_page"] ||
|
93
|
+
page_size = (page_params["per_page"] || 100).to_i
|
90
94
|
page_index = (page_params["page"] || 1).to_i
|
91
95
|
offset = (page_index - 1) * page_size
|
92
96
|
filter = options[:filter]
|
@@ -95,11 +99,13 @@ class Zendesk2::Client < Cistern::Service
|
|
95
99
|
count = resources.size
|
96
100
|
total_pages = (count / page_size) + 1
|
97
101
|
|
102
|
+
query = options.fetch(:query, {})
|
103
|
+
|
98
104
|
next_page = if page_index < total_pages
|
99
|
-
url_for(
|
105
|
+
url_for(path, query: {page: page_index + 1, per_page: page_size}.merge(query))
|
100
106
|
end
|
101
107
|
previous_page = if page_index > 1
|
102
|
-
url_for(
|
108
|
+
url_for(path, query: {page: page_index - 1, per_page: page_size}.merge(query))
|
103
109
|
end
|
104
110
|
|
105
111
|
resource_page = resources.slice(offset, page_size)
|
@@ -1,26 +1,23 @@
|
|
1
1
|
class Zendesk2::Client
|
2
2
|
class Real
|
3
|
-
def search(query)
|
3
|
+
def search(query, params={})
|
4
4
|
request(
|
5
5
|
:method => :get,
|
6
|
-
:params => {query: query},
|
6
|
+
:params => {query: query}.merge(params),
|
7
7
|
:path => "/search.json",
|
8
8
|
)
|
9
9
|
end
|
10
10
|
end # Real
|
11
11
|
|
12
12
|
class Mock
|
13
|
-
def search(query)
|
13
|
+
def search(query, params={})
|
14
14
|
terms = Hash[query.split(" ").map { |t| t.split(":") }]
|
15
15
|
type = terms.delete("type")
|
16
16
|
collection = type.nil? ? self.data.values : self.data[pluralize(type).to_sym]
|
17
17
|
|
18
18
|
results = collection.values.select { |v| terms.all?{ |term, condition| v[term.to_s].to_s == condition.to_s } }
|
19
19
|
|
20
|
-
|
21
|
-
:path => "/search.json",
|
22
|
-
:body => {"results" => results},
|
23
|
-
)
|
20
|
+
page(params, nil, "/search.json", "results", resources: results, query: {query: query})
|
24
21
|
end
|
25
22
|
end # Mock
|
26
23
|
end
|
@@ -1,26 +1,23 @@
|
|
1
1
|
class Zendesk2::Client
|
2
2
|
class Real
|
3
|
-
def search_help_center_articles(query)
|
3
|
+
def search_help_center_articles(query, params={})
|
4
4
|
request(
|
5
5
|
:method => :get,
|
6
|
-
:params => {query: query},
|
6
|
+
:params => {query: query}.merge(params),
|
7
7
|
:path => "/help_center/articles/search.json",
|
8
8
|
)
|
9
9
|
end
|
10
10
|
end # Real
|
11
11
|
|
12
12
|
class Mock
|
13
|
-
def search_help_center_articles(query)
|
13
|
+
def search_help_center_articles(query, params={})
|
14
14
|
terms = Hash[query.split(" ").map { |t| t.split(":") }]
|
15
15
|
|
16
16
|
collection = self.data[:help_center_articles].values
|
17
17
|
|
18
18
|
results = collection.select { |v| terms.all?{ |term, condition| v[term.to_s].to_s == condition.to_s } }
|
19
19
|
|
20
|
-
|
21
|
-
:path => "/search.json",
|
22
|
-
:body => {"results" => results},
|
23
|
-
)
|
20
|
+
page(params, :help_center_articles, "/search.json", "results", resources: results, query: {query: query})
|
24
21
|
end
|
25
22
|
end # Mock
|
26
23
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Zendesk2::Client
|
2
|
+
class Real
|
3
|
+
alias search_organization search
|
4
|
+
end # Real
|
5
|
+
|
6
|
+
class Mock
|
7
|
+
def search_organization(query, params={})
|
8
|
+
terms = Hash[query.split(" ").map { |t| t.split(":") }]
|
9
|
+
terms.delete("type") # context already provided
|
10
|
+
|
11
|
+
collection = self.data[:organizations].values
|
12
|
+
|
13
|
+
# organization name is fuzzy matched
|
14
|
+
if organization_name = terms.delete("name")
|
15
|
+
terms.merge!("name" => "*#{organization_name}*")
|
16
|
+
end
|
17
|
+
|
18
|
+
compiled_terms = terms.inject({}) do |r,(term, raw_condition)|
|
19
|
+
condition = if raw_condition.include?("*")
|
20
|
+
Regexp.compile(raw_condition.gsub("*", ".*"), Regexp::IGNORECASE)
|
21
|
+
else
|
22
|
+
raw_condition
|
23
|
+
end
|
24
|
+
r.merge(term => condition)
|
25
|
+
end
|
26
|
+
|
27
|
+
results = collection.select do |v|
|
28
|
+
compiled_terms.all? do |term, condition|
|
29
|
+
condition.is_a?(Regexp) ? condition.match(v[term.to_s]) : v[term.to_s].to_s == condition.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
page(params, :organizations, "/search.json", "results", resources: results, query: {query: query})
|
34
|
+
end
|
35
|
+
end # Mock
|
36
|
+
end
|
@@ -4,7 +4,7 @@ class Zendesk2::Client
|
|
4
4
|
end # Real
|
5
5
|
|
6
6
|
class Mock
|
7
|
-
def search_user(query)
|
7
|
+
def search_user(query, params={})
|
8
8
|
terms = Hash[query.split(" ").map { |t| t.split(":") }]
|
9
9
|
terms.delete("type") # context already provided
|
10
10
|
|
@@ -46,10 +46,7 @@ class Zendesk2::Client
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
|
-
:path => "/search.json",
|
51
|
-
:body => {"results" => results},
|
52
|
-
)
|
49
|
+
page(params, :users, "/search.json", "results", resources: results, query: {query: query})
|
53
50
|
end
|
54
51
|
end # Mock
|
55
52
|
end
|
@@ -14,9 +14,8 @@ class Zendesk2::PagedCollection < Zendesk2::Collection
|
|
14
14
|
def model_root; self.class.model_root; end
|
15
15
|
|
16
16
|
def new_page
|
17
|
-
page = self.
|
18
|
-
|
19
|
-
page.records = []
|
17
|
+
page = self.class.new(connection: self.connection)
|
18
|
+
page.merge_attributes(self.class.scopes.inject({}){|r,k| r.merge(k.to_s => send(k))})
|
20
19
|
page
|
21
20
|
end
|
22
21
|
|
@@ -39,11 +38,11 @@ class Zendesk2::PagedCollection < Zendesk2::Collection
|
|
39
38
|
end
|
40
39
|
|
41
40
|
def next_page
|
42
|
-
new_page.all("url" => next_page_link) if next_page_link
|
41
|
+
new_page.all("url" => next_page_link, "filtered" => self.filtered) if next_page_link
|
43
42
|
end
|
44
43
|
|
45
44
|
def previous_page
|
46
|
-
new_page.all("url" => previous_page_link) if previous_page_link
|
45
|
+
new_page.all("url" => previous_page_link, "filtered" => self.filtered) if previous_page_link
|
47
46
|
end
|
48
47
|
|
49
48
|
# Attempt creation of resource and explode if unsuccessful
|
@@ -64,11 +63,17 @@ class Zendesk2::PagedCollection < Zendesk2::Collection
|
|
64
63
|
|
65
64
|
# Fetch a collection of resources
|
66
65
|
def all(params={})
|
67
|
-
|
68
|
-
|
66
|
+
if params["filtered"] && (url = params["url"])
|
67
|
+
uri = Addressable::URI.parse(url)
|
68
|
+
query = uri.query_values
|
69
|
+
search(query.delete("query"), query)
|
70
|
+
else
|
71
|
+
scoped_attributes = self.class.scopes.inject({}){|r,k| r.merge(k.to_s => send(k))}.merge(params)
|
72
|
+
body = connection.send(collection_method, scoped_attributes).body
|
69
73
|
|
70
|
-
|
71
|
-
|
74
|
+
self.load(body[collection_root]) # 'results' is the key for paged seraches
|
75
|
+
self.merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "previous_page"))
|
76
|
+
end
|
72
77
|
self
|
73
78
|
end
|
74
79
|
|
data/lib/zendesk2/searchable.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module Zendesk2::Searchable
|
2
2
|
def self.included(klass)
|
3
3
|
klass.send(:extend, Zendesk2::Searchable::Attributes)
|
4
|
+
# @note signal to underlying collection that a search request
|
5
|
+
# must be use when requesting associated pages
|
6
|
+
klass.send(:attribute, :filtered, type: :boolean)
|
4
7
|
end
|
5
8
|
|
6
9
|
# Search for resources of a certain type.
|
@@ -14,7 +17,7 @@ module Zendesk2::Searchable
|
|
14
17
|
#
|
15
18
|
# @param [String, Hash] seach terms. This will be converted to a qualified Zendesk search String
|
16
19
|
# @see http://developer.zendesk.com/documentation/rest_api/search.html
|
17
|
-
def search(terms)
|
20
|
+
def search(terms, params={})
|
18
21
|
query = if terms.is_a?(Hash)
|
19
22
|
terms.merge!("type" => self.class.search_type) if self.class.search_type
|
20
23
|
terms.merge(self.class.scopes.inject({}){|r,k| r.merge(k.to_s => public_send(k))}).
|
@@ -33,12 +36,12 @@ module Zendesk2::Searchable
|
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
36
|
-
body = connection.send(self.class.search_request, query).body
|
39
|
+
body = connection.send(self.class.search_request, query, params).body
|
37
40
|
|
38
41
|
if data = body.delete("results")
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
self.load(data)
|
43
|
+
self.merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "previous_page").merge("filtered" => true))
|
44
|
+
self
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
data/lib/zendesk2/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Zendesk2::PagedCollection, :mock_only do
|
4
4
|
let!(:client) { create_client }
|
5
5
|
|
6
|
-
let!(:records) { 333.times.map { client.organizations.create(name: mock_uuid) } }
|
6
|
+
let!(:records) { 333.times.each_with_index.map { |_,i| client.organizations.create(name: "#{i%3}_#{mock_uuid}") } }
|
7
7
|
|
8
8
|
context "#each_page" do
|
9
9
|
it "should enumerate pages" do
|
@@ -28,5 +28,15 @@ describe Zendesk2::PagedCollection, :mock_only do
|
|
28
28
|
|
29
29
|
expect(target).to eq(found)
|
30
30
|
end
|
31
|
+
|
32
|
+
it "should chain search paging" do
|
33
|
+
matching_records = records.select { |o| o.name.match(/\A2_/) }
|
34
|
+
expect(matching_records).not_to be_empty
|
35
|
+
|
36
|
+
found_records = client.organizations.search(name: "2_").each_entry.to_a
|
37
|
+
|
38
|
+
expect(matching_records.size).to eq(found_records.size)
|
39
|
+
expect(matching_records).to match_array(found_records)
|
40
|
+
end
|
31
41
|
end
|
32
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zendesk2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -221,6 +221,7 @@ files:
|
|
221
221
|
- lib/zendesk2/client/requests/mark_user_identity_primary.rb
|
222
222
|
- lib/zendesk2/client/requests/search.rb
|
223
223
|
- lib/zendesk2/client/requests/search_help_center_articles.rb
|
224
|
+
- lib/zendesk2/client/requests/search_organization.rb
|
224
225
|
- lib/zendesk2/client/requests/search_user.rb
|
225
226
|
- lib/zendesk2/client/requests/update_category.rb
|
226
227
|
- lib/zendesk2/client/requests/update_forum.rb
|
@@ -285,7 +286,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
285
286
|
version: '0'
|
286
287
|
requirements: []
|
287
288
|
rubyforge_project:
|
288
|
-
rubygems_version: 2.
|
289
|
+
rubygems_version: 2.2.2
|
289
290
|
signing_key:
|
290
291
|
specification_version: 4
|
291
292
|
summary: Zendesk V2 API client
|
@@ -310,3 +311,4 @@ test_files:
|
|
310
311
|
- spec/user_fields_spec.rb
|
311
312
|
- spec/user_identities_spec.rb
|
312
313
|
- spec/users_spec.rb
|
314
|
+
has_rdoc:
|