Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
96af63d
Adding error handling class
cometman Sep 28, 2015
4661d10
Moving to module to hijack Faraday errors
cometman Sep 28, 2015
85483e1
Fix name
cometman Sep 28, 2015
ab88aa6
adding first 404 error
cometman Sep 28, 2015
82b771d
wip for relocation
cometman Sep 28, 2015
e067a11
adding raise error class
cometman Sep 29, 2015
79e5247
adding 401 error
cometman Sep 29, 2015
5d100f7
Adding test for 401's
cometman Sep 29, 2015
4bede03
adding more error catching
cometman Oct 12, 2015
f9829e0
resolving merge
cometman Oct 12, 2015
b7f4400
Fixing unit test
cometman Oct 12, 2015
1b1dd79
Adding last 2 errors
cometman Oct 12, 2015
c70cbc8
adding server errors
cometman Oct 13, 2015
aab1294
Adding more tests and refcatoring
cometman Oct 14, 2015
955fb04
Fix stray commit here too.
knowtheory Oct 27, 2015
d94cd8a
Adding code from refactoring
cometman Oct 30, 2015
ff41887
Forgot to add lib folder with all refactored errors
cometman Oct 30, 2015
30aba73
Merge branch 'error_handling' of https://github.com/documentcloud/ope…
cometman Oct 30, 2015
64bce2d
Consolidate errors into a single file & namespace errors under OpenCa…
knowtheory Oct 30, 2015
0e7040d
Fix reference to error class.
knowtheory Oct 30, 2015
646c1ce
Update Travis config
farski Nov 2, 2015
a39b4d1
Update .travis.yml
farski Nov 2, 2015
89e166e
Merge pull request #11 from PRX/chore/update_travis
kookster May 31, 2016
0a8ff02
Update links
kant Sep 30, 2016
253659b
Merge pull request #2 from kant/patch-1
knowtheory Oct 1, 2016
3e85314
kill circular dependency warning
knowtheory Feb 1, 2018
3e6be88
kill warning about ambiguous splats
knowtheory Feb 1, 2018
53212fb
Kill warning about variable shadowing.
knowtheory Feb 1, 2018
207633c
Merge branch 'prx_master'
knowtheory Feb 1, 2018
63fd485
Kill circular dependency warning.
knowtheory Feb 1, 2018
9592f21
Kill ambiguous splat warning.
knowtheory Feb 1, 2018
7bcbcdc
Kill variable shadowing warning.
knowtheory Feb 1, 2018
2fa5356
Merge branch 'cleanup_tests'
knowtheory Feb 1, 2018
94e5de7
Merge branch 'master' into error_handling
knowtheory Feb 2, 2018
1f968a1
Fix typo
knowtheory Feb 2, 2018
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
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
sudo: false
language: ruby
rvm:
- "2.1.6"
- "2.2.1"
cache: bundler
env:
- TRAVIS=true
rvm:
- 2.0.0-p647
- 2.1.7
- 2.2.0
- 2.2.1
- 2.2.2
- 2.2.3
script:
- bundle exec rake test
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CLA# OpenCalais
# OpenCalais

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/PRX/open_calais.svg?branch=master)](https://travis-ci.org/PRX/open_calais)
Expand All @@ -8,11 +8,11 @@ CLA# OpenCalais

_Gem Version 0.1.* supports the original OpenCalais API_

_Gem Version 0.2.* now uses the upgraded API http://new.opencalais.com/upgrade/_
_Gem Version 0.2.* now uses the upgraded API http://www.opencalais.com/_

If you are upgrading the the new version of the API, you will need a new API key. You should also review the [upgrade guide](http://new.opencalais.com/upgrade/).
If you are upgrading the the new version of the API, you will need a new API key. You should also review the [upgrade guide](http://www.opencalais.com/opencalais-api/).

This is a ruby gem to access the [OpenCalais API](http://www.opencalais.com/documentation/calais-web-service-api/api-invocation/rest), using the REST API, and JSON responses.
This is a ruby gem to access the [OpenCalais API](http://www.opencalais.com/opencalais-api/), using the REST API, and JSON responses.

It uses [Faraday](https://github.com/lostisland/faraday) to abstract HTTP library (defaults to use excon because it is excellent), and multi_json to abstract JSON parsing.

Expand Down
3 changes: 2 additions & 1 deletion lib/open_calais/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
require 'open_calais/configuration'
require 'open_calais/connection'
require 'open_calais/response'
require 'open_calais/errors'

module OpenCalais
class Client

include Connection

attr_reader *OpenCalais::Configuration.keys
attr_reader(*OpenCalais::Configuration.keys)

attr_accessor :current_options

Expand Down
2 changes: 1 addition & 1 deletion lib/open_calais/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Configuration
# The value sent in the http header for 'User-Agent' if none is set
DEFAULT_USER_AGENT = "OpenCalais Ruby Gem #{OpenCalais::VERSION}".freeze

attr_accessor *VALID_OPTIONS_KEYS
attr_accessor(*VALID_OPTIONS_KEYS)

# Convenience method to allow for global setting of configuration options
def configure
Expand Down
2 changes: 1 addition & 1 deletion lib/open_calais/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def connection(options={})
else
connection.response :xml
end

connection.use OpenCalais::Errors::Handler
connection.adapter(adapter)
end

Expand Down
57 changes: 57 additions & 0 deletions lib/open_calais/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module OpenCalais
module Errors

class AuthError < StandardError
end

class BadContent < StandardError
end

class BadMedia < StandardError
end

class InvalidFormat < StandardError
end

class LargeEntity < StandardError
end

class RateLimitExceeded < StandardError
end

class ResourceNotFound < StandardError
end

class ServerError < StandardError
end

class ServiceUnavailable < StandardError
end

class Handler < Faraday::Response::Middleware
def on_complete(env)
case env[:status]
when 503
raise Errors::ServiceUnavailable
when 500
raise Errors::ServerError
when 429
raise Errors::RateLimitExceeded
when 415
raise Errors::BadMedia
when 413
raise Errors::LargeEntity
when 406
raise Errors::InvalidFormat
when 404
raise Errors::ResourceNotFound
when 401
raise Errors::AuthError
when 400
raise Errors::BadContent
end
end
end

end
end
2 changes: 1 addition & 1 deletion lib/open_calais/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def parse(response, options={})
self.entities << item
end
when 'relations'
item = v.reject{|k,v| k[0] == '_' || k == 'instances'} || {}
item = v.reject{|key,val| key[0] == '_' || key == 'instances'} || {}
item[:type] = transliterate(v._type).titleize
self.relations << item
end
Expand Down
2 changes: 2 additions & 0 deletions open_calais.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_runtime_dependency('faraday')
gem.add_runtime_dependency('webmock')
gem.add_runtime_dependency('faraday_middleware')
gem.add_runtime_dependency('multi_json')
gem.add_runtime_dependency('multi_xml')
Expand All @@ -30,4 +31,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency('minitest')
gem.add_development_dependency('simplecov')
gem.add_development_dependency('coveralls')
gem.add_development_dependency('byebug')
end
6 changes: 5 additions & 1 deletion test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe OpenCalais::Client do

before :each do
sleep(1)
end

it "is initialized with defaults" do
oc = OpenCalais::Client.new
oc.current_options.wont_be_nil
Expand Down Expand Up @@ -30,7 +34,7 @@
response.raw.wont_be_nil
end

it "passes in header optionsin enrich" do
it "passes in header options in enrich" do
oc = OpenCalais::Client.new(:api_key => ENV['OPEN_CALAIS_KEY'])
response = oc.enrich("Ruby on Rails is a fantastic web framework. It uses MVC, and the ruby programming language invented by Matz", :headers => {:content_type => OpenCalais::CONTENT_TYPES[:html]})
response.wont_be_nil
Expand Down
65 changes: 65 additions & 0 deletions test/error_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')

describe OpenCalais::Client do

before :each do
@oc = OpenCalais::Client.new(:api_key => '')
end

it "should throw a AuthError for bad api key." do
WebMock.disable!
-> { @oc.enrich("Ruby on Rails is a fantastic web framework. It uses MVC, and the ruby programming language invented by Matz") }.must_raise OpenCalais::Errors::AuthError
end

it "should throw a ServiceUnavailable error" do
WebMock.enable!
mock_call(:status => 503)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::ServiceUnavailable
end

it "should throw a ServerError error" do
WebMock.enable!
mock_call(:status => 500)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::ServerError
end

it "should throw a RateLimitExceeded error" do
WebMock.enable!
mock_call(:status => 429)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::RateLimitExceeded
end

it "should throw a BadMedia error" do
WebMock.enable!
mock_call(:status => 415)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::BadMedia
end

it "should throw a InvalidFormat error" do
WebMock.enable!
mock_call(:status => 406)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::InvalidFormat
end

it "should throw a ResourceNotFound error" do
WebMock.enable!
mock_call(:status => 404)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::ResourceNotFound
end

it "should throw a AuthError error" do
WebMock.enable!
mock_call(:status => 401)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::AuthError
end

it "should throw a BadContent error" do
WebMock.enable!
mock_call(:status => 400)
-> { @oc.enrich("Test") }.must_raise OpenCalais::Errors::BadContent
end
end

def mock_call(options)
stub_request(:any, /.*#{CALAIS_API}.*/).to_return(options, :headers => { 'Content-Length' => 3 })
end
8 changes: 7 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
Coveralls.wear!
end

require 'minitest/spec'
require 'minitest/autorun'
require 'byebug'

require 'open_calais'

require 'webmock'
include WebMock::API
CALAIS_API = "api.thomsonreuters.com"

WebMock.disable!