Skip to content

Commit

Permalink
paged user collection. fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
lanej committed Jun 17, 2012
1 parent 6b3efeb commit 43979e5
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 68 deletions.
36 changes: 29 additions & 7 deletions lib/zendesk/client.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'cistern'

require 'addressable/uri'
require 'faraday'
require 'faraday_middleware'
require 'excon'

require 'time'
require 'formatador'


class Zendesk::Client < Cistern::Service
Expand Down Expand Up @@ -47,10 +48,11 @@ def initialize(options={})
@path = URI.parse(url).path

logger = options[:logger]
adapter = options[:adapter] || :excon
adapter = options[:adapter] || :net_http
connection_options = options[:connection_options] || {ssl: {verify: false}}
@username, @password = options[:username], options[:password]

@connection = Faraday.new(url: @url) do |builder|
@connection = Faraday.new({url: @url}.merge(connection_options)) do |builder|
# response
builder.use Faraday::Request::BasicAuthentication, @username, @password
builder.use Faraday::Response::RaiseError
Expand Down Expand Up @@ -83,7 +85,7 @@ def request(options={})
end

class Mock

attr_reader :username, :url

def self.data
Expand Down Expand Up @@ -125,10 +127,30 @@ def initialize(options={})
@current_user_id = self.class.new_id

self.data[:users][@current_user_id]= {
"id" => @current_user_id,
"email" => @username,
"name" => "Mock Agent",
"url" => File.join(@url, "/users/#{@current_user_id}.json"),
"name" => "Mock Agent",
"url" => File.join(@url, "/users/#{@current_user_id}.json"),
}
end

def response(options={})
method = options[:method] || :get
status = options[:status] || 200
path = options[:path]
body = options[:body]

url = File.join(@url, path)

Faraday::Response.new(
:method => method,
:status => status,
:url => url,
:body => body,
:request_headers => {
"Content-Type" => "application/json"
},
)
end
end
end
4 changes: 4 additions & 0 deletions lib/zendesk/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ def destroy
data = connection.destroy_user("id" => self.identity).body["user"]
merge_attributes(data)
end

def destroyed?
!self.active
end
end
29 changes: 23 additions & 6 deletions lib/zendesk/models/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ class Zendesk::Client::Users < Cistern::Collection

attribute :count
attribute :next_page_link, :aliases => "next_page"
attribute :prev_page_link, :aliases => "prev_page"
attribute :previous_page_link, :aliases => "previous_page"

def current
data = connection.get_current_user.body["user"]
new(data)
new(connection.get_current_user.body["user"])
end

# usernames and email address
def search(term)
body = connection.search_user("query" => term).body
data = body["results"]
load(data)
merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "previous_page"))
end

def all(params={})
body = connection.get_users(params).body

load(body["users"])
merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "prev_page"))
merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "previous_page"))
end

def get(id)
data = connection.get_user("id" => id).body["user"]
new(data) if data
if data = connection.get_user("id" => id).body["user"]
new(data)
end
end

def next_page
all("url" => next_page_link) if next_page_link
end

def previous_page
all("url" => previous_page_link) if previous_page_link
end
end
63 changes: 46 additions & 17 deletions lib/zendesk/requests/get_users.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
class Zendesk::Client
class Real
def get_users(params={})
query = if url = params["url"]
uri = Addressable::URI.parse(url)
uri.query_values
else
Cistern::Hash.slice(params, "page", "per_page")
end

request(
:params => params,
:method => :get,
:path => "/users.json"
:params => query,
:method => :get,
:path => "/users.json",
)
end
end
class Mock
def get_users(params={})
page_size = (params["per_page"] || 50).to_i
page_index = (params["page"] || 1).to_i
count = self.data[:users].size
page_params = if url = params.delete("url")
uri = Addressable::URI.parse(url)
uri.query_values
else
Cistern::Hash.slice(params, "page", "per_page")
end
params.merge!(page_params)

page_size = (params["per_page"] || 50).to_i
page_index = (params["page"] || 1).to_i
count = self.data[:users].size
offset = (page_index - 1) * page_size
total_pages = (count / page_size) + 1


next_page = if page_index < total_pages
File.join(@url, "users.json?page=#{page_index + 1}&per_page=#{page_size}")
else
nil
end
previous_page = if page_index > 1
File.join(@url, "users.json?page=#{page_index - 1}&per_page=#{page_size}")
else
nil
end

user_page = self.data[:users].values.slice(offset, page_size)

url = File.join(@url, "/users.json")
offset = (page_index - 1) * page_size
body = self.data[:users].values.slice(offset, page_size)
body = {
"users" => user_page,
"count" => count,
"next_page" => next_page,
"previous_page" => previous_page,
}

Faraday::Response.new(
:method => :get,
:status => 200,
:url => url,
:body => {"users" => body},
:request_headers => {
"Content-Type" => "application/json"
},
response(
body: body,
path: "/users.json"
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zendesk/requests/update_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Mock
def update_user(params={})
id = params.delete("id")
url = File.join(@url, "/users/#{id}.json")
body = self.data[:users].merge!(params)
body = self.data[:users][id].merge!(params)

Faraday::Response.new(
:method => :delete,
Expand Down
3 changes: 1 addition & 2 deletions spec/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

describe "accounts" do
let(:client) { Zendesk::Client.new }
it_should_behave_like "a collection", :accounts
it_should_behave_like "a model", :account
#it_should_behave_like "a resource"
end
30 changes: 0 additions & 30 deletions spec/shared/collection.rb

This file was deleted.

49 changes: 46 additions & 3 deletions spec/shared/resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
shared_examples "a resource" do |_collection, _params, _update_params|
let(:collection) { client.send(_collection) }
let(:params) { instance_exec(&_params) || {} }
let(:update_params) { instance_exec(&_update_params) }

it "by creating a record" do
record = collection.create(params)
record.identity.should_not be_nil
end

context "that is paged" do
before(:each) do
2.times.each { collection.create(instance_exec(&_params)) }
end
it "by retrieving the first page" do
collection.all("per_page" => "1").size.should == 1
end

shared_examples "a model" do
it "by updating a record"
it "by destroy a record"
it "by retrieving the next page" do
first_page = collection.all("per_page" => "1").to_a
second_page = collection.all("per_page" => 1).next_page.to_a
second_page.should_not == first_page
end

it "by retrieving the previous page" do
first_page = collection.all("per_page" => "1").to_a
previous_to_second_page = collection.all("per_page" => 1).next_page.previous_page.to_a
previous_to_second_page.should == first_page
end
end

it "by fetching a specific record" do
record = collection.create(params)
collection.get(record.identity).should == record
end

it "by updating a record" do
record = collection.create(params)
record.merge_attributes(update_params)
record.save
update_params.each {|k,v| record.send(k).should == v}
end

it "by destroy a record" do
record = collection.create(params)
record.identity.should_not be_nil
record.destroy
record.should be_destroyed
end
end
4 changes: 2 additions & 2 deletions zendesk2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Zendesk2::VERSION

gem.add_dependency "cistern"
gem.add_dependency "cistern", "~> 0.0.2"
gem.add_dependency "faraday"
gem.add_dependency "faraday_middleware"
gem.add_dependency "excon"
gem.add_dependency "addressable"
end

0 comments on commit 43979e5

Please sign in to comment.