forked from muhfajar/wp-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from DeweyLabs/detect-301
Raise Faraday errors
- Loading branch information
Showing
13 changed files
with
115 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,4 @@ source "https://rubygems.org" | |
gemspec | ||
|
||
gem "standard", "~> 1.33" | ||
|
||
gem "debug", "~> 1.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ require "rspec/core/rake_task" | |
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
module WpApiClient | ||
module Entities | ||
class User < Base | ||
alias :user :resource | ||
alias_method :user, :resource | ||
|
||
def self.represents?(json) | ||
json.dig('_links', 'collection') and json['_links']['collection'].first['href'] =~ /wp\/v2\/users/ | ||
json.dig("_links", "collection") and json["_links"]["collection"].first["href"] =~ /wp\/v2\/users/ | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module WpApiClient | ||
module Utils | ||
def self.deep_symbolize(obj) | ||
case obj | ||
when Hash | ||
obj.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = deep_symbolize(v) } | ||
when Array | ||
obj.map { |v| deep_symbolize(v) } | ||
else | ||
obj | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,55 @@ | ||
RSpec.describe WpApiClient::Collection do | ||
describe "fetching posts concurrently", vcr: {cassette_name: 'concurrency', record: :new_episodes} do | ||
RSpec.describe WpApiClient::Connection do | ||
# describe "fetching posts concurrently", vcr: {cassette_name: "concurrency", record: :new_episodes} do | ||
# it "allows simultaneous fetching of posts" do | ||
# pending "parallel faraday adapter to replace typhoeus" | ||
# resp = @api.concurrently do |api| | ||
# api.get("posts/1") | ||
# api.get("posts", page: 2) | ||
# end | ||
# expect(resp.first.title).to eq "Hello world!" | ||
# expect(resp.last.first.title).to eq "Post 90" | ||
# end | ||
# end | ||
|
||
it "allows simultaneous fetching of posts" do | ||
resp = @api.concurrently do |api| | ||
api.get("posts/1") | ||
api.get("posts", page: 2) | ||
describe "handling HTTP errors", vcr: false do | ||
let(:configuration) do | ||
config = WpApiClient::Configuration.new | ||
config.endpoint = "https://example.com/wp-json/wp/v2" | ||
config | ||
end | ||
let(:connection) { described_class.new(configuration) } | ||
|
||
context "when following redirects" do | ||
before do | ||
stub_request(:get, "https://example.com/wp-json/wp/v2/posts") | ||
.with(query: {"_embed" => "true"}) | ||
.to_return( | ||
status: 301, | ||
headers: {"Location" => "https://new-example.com/wp-json/wp/v2/posts?_embed=true"} | ||
) | ||
end | ||
|
||
it "raises an error" do | ||
expect { | ||
connection.get("posts") | ||
}.to raise_error(Faraday::FollowRedirects::RedirectLimitReached) | ||
end | ||
end | ||
|
||
context "when receiving other connection errors" do | ||
before do | ||
stub_request(:get, "https://example.com/wp-json/wp/v2/posts") | ||
.with(query: {"_embed" => "true"}) | ||
.to_return(status: 502) | ||
end | ||
|
||
it "raises a connection error with status information" do | ||
expect { | ||
connection.get("posts") | ||
}.to raise_error(Faraday::ServerError) do |error| | ||
expect(error.response[:status]).to eq(502) | ||
end | ||
end | ||
expect(resp.first.title).to eq 'Hello world!' | ||
expect(resp.last.first.title).to eq 'Post 90' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
RSpec.describe WpApiClient::Entities::Error do | ||
describe "an API access error", vcr: {cassette_name: 'single_post'} do | ||
|
||
Error_JSON = {"code"=>"rest_forbidden", "message"=>"You don't have permission to do this.", "data"=>{"status"=>403}} | ||
let(:error_json) { {"code" => "rest_forbidden", "message" => "You don't have permission to do this.", "data" => {"status" => 403}} } | ||
|
||
describe "an API access error", vcr: {cassette_name: "single_post"} do | ||
it "throws an exception" do | ||
expect { | ||
WpApiClient::Entities::Error.new(Error_JSON) | ||
WpApiClient::Entities::Error.new(error_json) | ||
}.to raise_error(WpApiClient::ErrorResponse) | ||
end | ||
|
||
it "recognises the error JSON exception" do | ||
expect(WpApiClient::Entities::Error.represents?(Error_JSON)).to be_truthy | ||
expect(WpApiClient::Entities::Error.represents?(error_json)).to be_truthy | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,37 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
lib = File.expand_path("../lib", __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'wp_api_client/version' | ||
require "wp_api_client/version" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "wp-api-client" | ||
spec.version = WpApiClient::VERSION | ||
spec.authors = ["Duncan Brown"] | ||
spec.email = ["[email protected]"] | ||
spec.name = "wp-api-client" | ||
spec.version = WpApiClient::VERSION | ||
spec.authors = ["Duncan Brown"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{A read-only client for the WordPress REST API (v2).} | ||
spec.homepage = "https://github.com/duncanjbrown/wp-api-client" | ||
spec.license = "MIT" | ||
spec.summary = "A read-only client for the WordPress REST API (v2)." | ||
spec.homepage = "https://github.com/duncanjbrown/wp-api-client" | ||
spec.license = "MIT" | ||
|
||
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or | ||
# delete this section to allow pushing this gem to any host. | ||
if spec.respond_to?(:metadata) | ||
spec.metadata['allowed_push_host'] = "https://rubygems.org" | ||
spec.metadata["allowed_push_host"] = "https://rubygems.org" | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against public gem pushes." | ||
end | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_dependency "faraday", "~> 2.7" | ||
spec.add_dependency "faraday-http-cache", "~> 2.5" | ||
spec.add_dependency "faraday-follow_redirects", "~> 0.3.0" | ||
|
||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
spec.add_development_dependency "vcr" | ||
spec.add_development_dependency "webmock" | ||
spec.add_development_dependency "debug" | ||
|
||
end |