Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WpApiClient::Collection: Added accessor to get total pages for a cust… #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
language: ruby
rvm:
- 2.3.0
- 2.5.3
before_install: gem install bundler -v 1.11.2
script:
- WP_VERSION=4.4 bundle exec rspec
- WP_VERSION=4.5 bundle exec rspec
- WP_VERSION=4.5.3 bundle exec rspec
- WP_VERSION=4.5.3_b13 bundle exec rspec
- WP_VERSION=4.7 bundle exec rspec
- WP_VERSION=4.7.2 bundle exec rspec
cache: bundler
3 changes: 2 additions & 1 deletion lib/wp_api_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ module WpApiClient
class Collection
include Enumerable

attr_accessor :resources, :total_available
attr_accessor :resources, :total_available, :total_pages

def initialize(resources, headers = nil)
resources = [resources] unless resources.is_a? Array
@resources = resources.map { |object| WpApiClient::Entities::Base.build(object) }
if headers
@links = parse_link_header(headers['Link'])
@total_available = headers['X-WP-TOTAL'].to_i
@total_pages = headers['X-WP-TotalPages'].to_i
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wp_api_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WpApiClient
VERSION = "0.3.0"
VERSION = "0.3.1"
end
10 changes: 10 additions & 0 deletions spec/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
expect(@collection.count).to eq 9
end

it "should respond to total_pages and total_available" do
expect(@collection).to respond_to(:total_pages)
expect(@collection).to respond_to(:total_available)
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec has the same name as the below one and I think it's redundant?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duncanjbrown yeah I will correct this.


it "should have 10 pages and 100 articles" do
expect(@collection.total_pages).to eq(10)
expect(@collection.total_available).to eq(100)
end
Copy link
Owner

@duncanjbrown duncanjbrown Jul 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right hand side is quite magical here. These values are hardcoded in the VCR cassettes: I believe X-WP-TOTAL is 100 and X-WP-TOTALPAGES is 10.

My whole testing approach here was flawed, but I think the least worst thing would be to put literals on the right hand side here, like this?

expect(@collection.total_pages).to eq(10)
expect(@collection.total_available).to eq(100)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duncanjbrown making this change.


it "throws an error when it tries to parse an error response" do
expect {
WpApiClient::Collection.new([{"code"=>"rest_forbidden", "message"=>"You don't have permission to do this.", "data"=>{"status"=>403}}])
Expand Down
2 changes: 1 addition & 1 deletion wp_api_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = '~> 2.3'
spec.required_ruby_version = '>= 2.3'

spec.add_dependency "faraday", "~> 0.9"
spec.add_dependency "faraday_middleware", "~> 0.10"
Expand Down