diff --git a/.travis.yml b/.travis.yml index 095ba4a..8882743 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index 560cd4d..f59e715 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. diff --git a/lib/open_calais/client.rb b/lib/open_calais/client.rb index 2c75826..56caff1 100644 --- a/lib/open_calais/client.rb +++ b/lib/open_calais/client.rb @@ -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 diff --git a/lib/open_calais/configuration.rb b/lib/open_calais/configuration.rb index d84bf74..2fbeb78 100644 --- a/lib/open_calais/configuration.rb +++ b/lib/open_calais/configuration.rb @@ -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 diff --git a/lib/open_calais/connection.rb b/lib/open_calais/connection.rb index 6eabdbd..532b195 100644 --- a/lib/open_calais/connection.rb +++ b/lib/open_calais/connection.rb @@ -48,7 +48,7 @@ def connection(options={}) else connection.response :xml end - + connection.use OpenCalais::Errors::Handler connection.adapter(adapter) end diff --git a/lib/open_calais/errors.rb b/lib/open_calais/errors.rb new file mode 100644 index 0000000..4486774 --- /dev/null +++ b/lib/open_calais/errors.rb @@ -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 \ No newline at end of file diff --git a/lib/open_calais/response.rb b/lib/open_calais/response.rb index 2e0a764..12a1913 100644 --- a/lib/open_calais/response.rb +++ b/lib/open_calais/response.rb @@ -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 diff --git a/open_calais.gemspec b/open_calais.gemspec index c6a2828..8b9f21b 100644 --- a/open_calais.gemspec +++ b/open_calais.gemspec @@ -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') @@ -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 diff --git a/test/client_test.rb b/test/client_test.rb index 2f4c9ad..0928d1d 100644 --- a/test/client_test.rb +++ b/test/client_test.rb @@ -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 @@ -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 diff --git a/test/error_test.rb b/test/error_test.rb new file mode 100644 index 0000000..45beead --- /dev/null +++ b/test/error_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 23e7c6e..387276b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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!