How to search using API client? #10

Closed
brainlid opened this Issue May 21, 2012 · 4 comments

Comments

Projects
None yet
2 participants

How do I search using the API client? The documentation shows the API supports it using a "search" action on a user (for instance). - http://developer.zendesk.com/documentation/rest_api/users.html

Specifically, I'm trying to search for a user by email.

All the following attempts return the full list of users paged in 100's.

zendesk_user = Zendesk::User.find(client, {:email => 'test@example.com'})
zendesk_user = Zendesk::User.find(client, :params => {:search => 'test@example.com'})
zendesk_user = Zendesk::User.find(client, :search => {:email => 'test@example.com'})

How do I use the API client to search for Users, Organizations, etc? I looked in the gem but didn't see any code or tests for this feature.

Contributor

steved commented May 22, 2012

User searching is available through

client.users.search(:query => "test@example.com")

Generic searching is available through

client.search(:query => "test@example.com")

The latter doesn't currently doesn't auto-detect the class of the result (just dumping them in a generic Search object), but that will hopefully come soon.
Both return collections.

Thanks for pointing me in the right direction. I had to look into the Zendesk::Collection class to figure out what to do with the created search query collection. I suggest documenting how search works on the Readme.

Here's how I understand it to work.

Create a query definition

query = client.users.search(:query => "test@example.com")

Execute the query and fetch the results

users = query.fetch
users.count  
# => 1
users.first.email
# => "test@example.com"
Contributor

steved commented May 22, 2012

The query is executed on demand. So something like

query = client.users.search(:query => "test@example.com")
query.first.email

Will execute the query and return the first user. Though calling #count currently doesn't execute the query first. That is a bug, and will be fixed.

Thanks for the answer. This resolved my issue. And yes, my initial problem was I used "count" to try and figure out if data was being returned.

brainlid closed this May 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment