open_calais 0.2.2 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a2fdfafb9d1563e07adfb6bf8149e012008ffdfb
4
- data.tar.gz: 0638f12eac133c7fa41d58e116e8b9bb8af480d7
2
+ SHA256:
3
+ metadata.gz: 5124fa084d725c52838fdf7e5e18322945bf2c96401b2dbadf80ab968e7b4068
4
+ data.tar.gz: e3d1b1209d652df0e5b46e2c743ca9569ea23b74cc31a9becbefba5db181d6b6
5
5
  SHA512:
6
- metadata.gz: 2d5c0b3cd56aed6317fff624e417c49c9e1e2f964211ceaf5dcac549c47af363cfad88f8ee312e80b74db54bfa596e7afc10dca8276d89287ca4e6b5c4448928
7
- data.tar.gz: f8ec4708f9e19d2c36df508ee6c3147b5e4c78ae93b2d5a3b1b8e161e7d62f5c2e33f7cfa8d921fa6cc70958ea3e94f9affb8a24f1c9f38d5067cd3821d728a8
6
+ metadata.gz: b0f606b87803a27e740d08c5a1974abfc2c14c3e4fb468ab3ba76f7611a65113bf606be930d8e37628df6e219729ccb0412d1827121a9a85f08c50397af0508d
7
+ data.tar.gz: e22336a48b7f6467872bf4b4c530b8d8e3f29f1b5754f8391f82600d9890519c1b295db44d4b372e427df2331a653d7f65a6e398d094e22b7701fbbcd2eca780
@@ -1,6 +1,11 @@
1
+ sudo: false
1
2
  language: ruby
2
- rvm:
3
- - "2.1.6"
4
- - "2.2.1"
3
+ cache: bundler
5
4
  env:
6
5
  - TRAVIS=true
6
+ rvm:
7
+ - 2.5.8
8
+ - 2.6.6
9
+ - 2.7.1
10
+ script:
11
+ - bundle exec rake test
data/README.md CHANGED
@@ -4,15 +4,18 @@
4
4
  [![Build Status](https://travis-ci.org/PRX/open_calais.svg?branch=master)](https://travis-ci.org/PRX/open_calais)
5
5
  [![Code Climate](https://codeclimate.com/github/PRX/open_calais/badges/gpa.svg)](https://codeclimate.com/github/PRX/open_calais)
6
6
  [![Coverage Status](https://coveralls.io/repos/PRX/open_calais/badge.svg?branch=master)](https://coveralls.io/r/PRX/open_calais?branch=master)
7
- [![Dependency Status](https://gemnasium.com/PRX/open_calais.svg)](https://gemnasium.com/PRX/open_calais)
8
7
 
9
8
  _Gem Version 0.1.* supports the original OpenCalais API_
10
9
 
11
- _Gem Version 0.2.* now uses the upgraded API http://new.opencalais.com/upgrade/_
10
+ _Gem Version 0.2.* now uses the upgraded API http://www.opencalais.com/_
12
11
 
13
- 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/).
12
+ _Gem Version 0.4.* supports refinitiv API_
14
13
 
15
- 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.
14
+ Open Calais is now found at: https://developers.refinitiv.com/open-permid/intelligent-tagging-restful-api
15
+
16
+ It has a new default endpoint: https://api-eit.refinitiv.com/permid/calais
17
+
18
+ This is a ruby gem to access the (formerly OpenCalais) [Refinitiv Intelligent Tagging API](https://developers.refinitiv.com/open-permid/intelligent-tagging-restful-api), using the REST API, and JSON responses.
16
19
 
17
20
  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.
18
21
 
@@ -34,7 +37,7 @@ Or install it yourself as:
34
37
 
35
38
  ## Usage
36
39
 
37
- OpenCalais has one main method, `enrich`, and so does the gem:
40
+ Intelligent Tagging (OpenCalais) has one main method, `enrich`, and so does the gem:
38
41
 
39
42
  ```ruby
40
43
  require 'open_calais'
@@ -45,7 +48,7 @@ OpenCalais.configure do |c|
45
48
  end
46
49
 
47
50
  # or you can configure for a single call
48
- open_calais = OpenCalais::Client.new(:api_key=>'an api key')
51
+ open_calais = OpenCalais::Client.new(:api_key => 'an api key')
49
52
 
50
53
  # it returns a OpenCalais::Response instance
51
54
  response = open_calais.enrich('Ruby on Rails is a fantastic web framework. It uses MVC, and the Ruby programming language invented by Matz in Japan.')
@@ -55,7 +58,7 @@ response.raw
55
58
 
56
59
  # and has been parsed a bit to get :language, :topics, :tags, :entities, :relations, :locations
57
60
  # as lists of hashes
58
- response.tags.each{|t| puts t[:name] }
61
+ response.tags.each { |t| puts t[:name] }
59
62
  ```
60
63
 
61
64
  ## Contributing
data/Rakefile CHANGED
@@ -6,6 +6,7 @@ Rake::TestTask.new do |t|
6
6
  t.libs.push 'test'
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  t.verbose = true
9
+ t.warning = true
9
10
  end
10
11
 
11
12
  task :default => :test
@@ -9,7 +9,7 @@ module OpenCalais
9
9
 
10
10
  include Connection
11
11
 
12
- attr_reader *OpenCalais::Configuration.keys
12
+ attr_reader(*OpenCalais::Configuration.keys)
13
13
 
14
14
  attr_accessor :current_options
15
15
 
@@ -43,7 +43,7 @@ module OpenCalais
43
43
  response = connection(options).post do |request|
44
44
  request.body = text
45
45
  end
46
- OpenCalais::Response.new(response)
46
+ OpenCalais::Response.new(response, options)
47
47
  end
48
48
 
49
49
  # using analyze as a standard method name
@@ -17,12 +17,12 @@ module OpenCalais
17
17
  DEFAULT_ADAPTER = :excon
18
18
 
19
19
  # The api endpoint to get REST info from opencalais
20
- DEFAULT_ENDPOINT = 'https://api.thomsonreuters.com/permid/calais'.freeze
20
+ DEFAULT_ENDPOINT = 'https://api-eit.refinitiv.com/permid/calais'.freeze
21
21
 
22
22
  # The value sent in the http header for 'User-Agent' if none is set
23
23
  DEFAULT_USER_AGENT = "OpenCalais Ruby Gem #{OpenCalais::VERSION}".freeze
24
24
 
25
- attr_accessor *VALID_OPTIONS_KEYS
25
+ attr_accessor(*VALID_OPTIONS_KEYS)
26
26
 
27
27
  # Convenience method to allow for global setting of configuration options
28
28
  def configure
@@ -1,10 +1,15 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'faraday_middleware'
4
+ require 'hashie'
4
5
 
5
6
  module OpenCalais
6
7
  module Connection
7
8
 
9
+ class HashieWrapper < ::Hashie::Mash
10
+ disable_warnings
11
+ end
12
+
8
13
  ALLOWED_OPTIONS = [
9
14
  :headers,
10
15
  :url,
@@ -37,13 +42,15 @@ module OpenCalais
37
42
 
38
43
  def connection(options={})
39
44
  opts = merge_default_options(options)
45
+ FaradayMiddleware::Mashify.mash_class = HashieWrapper
40
46
  Faraday::Connection.new(opts) do |connection|
41
47
  connection.request :url_encoded
42
48
  connection.response :mashify
43
49
  connection.response :logger if ENV['DEBUG']
50
+ connection.response :raise_error
44
51
 
45
52
  if opts[:headers][OpenCalais::HEADERS[:output_format]] == OpenCalais::OUTPUT_FORMATS[:json]
46
- connection.response :json
53
+ connection.response :json, :content_type => /\bjson$/
47
54
  else
48
55
  connection.response :xml
49
56
  end
@@ -1,11 +1,19 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'stringex'
2
+
3
+ require 'active_support'
3
4
 
4
5
  module OpenCalais
5
6
  class Response
7
+
8
+ ALLOWED_OPTIONS = [
9
+ :ignore_literal_match
10
+ ].freeze
11
+
12
+ include ActiveSupport::Inflector
6
13
  attr_accessor :raw, :language, :topics, :tags, :entities, :relations, :locations
7
14
 
8
- def initialize(response)
15
+ def initialize(response, options={})
16
+ @options = merge_default_options(options)
9
17
  @raw = response
10
18
 
11
19
  @language = 'English'
@@ -15,22 +23,30 @@ module OpenCalais
15
23
  @relations = []
16
24
  @locations = []
17
25
 
18
- parse(response)
26
+ parse(response, @options)
27
+ end
28
+
29
+ def merge_default_options(opts={})
30
+ defaults = {
31
+ :ignore_literal_match => true
32
+ }
33
+ options = defaults.merge(opts)
34
+ options.select{ |k,v| ALLOWED_OPTIONS.include? k.to_sym }
19
35
  end
20
36
 
21
37
  def humanize_topic(topic)
22
- topic.gsub('_', ' & ').titleize.remove_formatting
38
+ transliterate(topic.gsub('_', ' & ').titleize)
23
39
  end
24
40
 
25
41
  def importance_to_score(imp)
26
- case imp
42
+ case imp.to_i
27
43
  when 1 then 0.9
28
44
  when 2 then 0.7
29
45
  else 0.5
30
46
  end
31
47
  end
32
48
 
33
- def parse(response)
49
+ def parse(response, options={})
34
50
  r = response.body
35
51
  @language = r.doc.meta.language rescue nil
36
52
  @language = nil if @language == 'InputTextTooShort'
@@ -38,38 +54,70 @@ module OpenCalais
38
54
  r.each do |k,v|
39
55
  case v._typeGroup
40
56
  when 'topics'
41
- self.topics << {:name => humanize_topic(v.name), :score => v.score.to_f, :original => v.name}
57
+ self.topics << {
58
+ :name => humanize_topic(v.name),
59
+ :score => v.score.to_f,
60
+ :original => v.name
61
+ }
42
62
  when 'socialTag'
43
- self.tags << {:name => v.name.gsub('_', ' and ').downcase, :score => importance_to_score(v.importance)}
63
+ self.tags << {
64
+ :name => v.name.gsub('_', ' and ').downcase,
65
+ :score => importance_to_score(v.importance)
66
+ }
44
67
  when 'entities'
45
- item = {:guid => k, :name => v.name, :type => v._type.remove_formatting.titleize, :score => 1.0}
46
-
47
- instances = Array(v.instances).select{|i| i.exact.downcase != item[:name].downcase }
48
- item[:matches] = instances if instances && instances.size > 0
49
-
50
- if OpenCalais::GEO_TYPES.include?(v._type)
51
- if (v.resolutions && v.resolutions.size > 0)
52
- r = v.resolutions.first
53
- item[:name] = r.shortname || r.name
54
- item[:latitude] = r.latitude
55
- item[:longitude] = r.longitude
56
- item[:country] = r.containedbycountry if r.containedbycountry
57
- item[:state] = r.containedbystate if r.containedbystate
58
- end
59
- self.locations << item
60
- else
61
- self.entities << item
62
- end
68
+ parse_entity(k, v, options)
63
69
  when 'relations'
64
- item = v.reject{|k,v| k[0] == '_' || k == 'instances'} || {}
65
- item[:type] = v._type.remove_formatting.titleize
66
- self.relations << item
70
+ parse_relation(k, v, options)
67
71
  end
68
72
  end
69
73
 
70
74
  # remove social tags which are in the topics list already
71
- topic_names = self.topics.collect{|topic| topic[:name].downcase}
72
- self.tags.delete_if{|tag| topic_names.include?(tag[:name]) }
75
+ topic_names = self.topics.collect { |topic| topic[:name].downcase }
76
+ self.tags.delete_if { |tag| topic_names.include?(tag[:name]) }
77
+ end
78
+
79
+ def parse_entity(k, v, options)
80
+ if v.name.nil?
81
+ v.name = v.instances.first[:exact]
82
+ end
83
+
84
+ item = {
85
+ :guid => k,
86
+ :name => v.name,
87
+ :type => transliterate(v._type).titleize,
88
+ :score => v.relevance
89
+ }
90
+
91
+ instances = Array(v.instances)
92
+ if options[:ignore_literal_match]
93
+ instances = instances.select { |i| i.exact.downcase != item[:name].downcase }
94
+ end
95
+ item[:matches] = instances unless instances.empty?
96
+
97
+ if OpenCalais::GEO_TYPES.include?(v._type)
98
+ item = set_location_info(item, v)
99
+ self.locations << item
100
+ else
101
+ self.entities << item
102
+ end
103
+ end
104
+
105
+ def set_location_info(item, v)
106
+ return item if v.resolutions.nil? || v.resolutions.empty?
107
+
108
+ r = v.resolutions.first
109
+ item[:name] = r.shortname || r.name
110
+ item[:latitude] = r.latitude
111
+ item[:longitude] = r.longitude
112
+ item[:country] = r.containedbycountry if r.containedbycountry
113
+ item[:state] = r.containedbystate if r.containedbystate
114
+ item
115
+ end
116
+
117
+ def parse_relation(k, v, _options)
118
+ item = v.reject { |key,val| key[0] == '_' || key == 'instances' } || {}
119
+ item[:type] = transliterate(v._type).titleize
120
+ self.relations << item
73
121
  end
74
122
  end
75
123
  end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module OpenCalais
4
- VERSION = '0.2.2'
4
+ VERSION = '0.4.2'
5
5
  end
@@ -18,14 +18,13 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_runtime_dependency('faraday', '>= 0.7.4')
22
- gem.add_runtime_dependency('faraday_middleware', '~> 0.9')
23
- gem.add_runtime_dependency('multi_json', '>= 1.0.3', '~> 1.0')
21
+ gem.add_runtime_dependency('faraday')
22
+ gem.add_runtime_dependency('faraday_middleware')
23
+ gem.add_runtime_dependency('multi_json')
24
24
  gem.add_runtime_dependency('multi_xml')
25
25
  gem.add_runtime_dependency('excon')
26
- gem.add_runtime_dependency('hashie', '>= 0.4.0')
26
+ gem.add_runtime_dependency('hashie')
27
27
  gem.add_runtime_dependency('activesupport')
28
- gem.add_runtime_dependency('stringex')
29
28
 
30
29
  gem.add_development_dependency('rake')
31
30
  gem.add_development_dependency('minitest')
@@ -4,36 +4,36 @@ describe OpenCalais::Client do
4
4
 
5
5
  it "is initialized with defaults" do
6
6
  oc = OpenCalais::Client.new
7
- oc.current_options.wont_be_nil
8
- oc.current_options.must_equal OpenCalais.options
7
+ _(oc.current_options).wont_be_nil
8
+ _(oc.current_options).must_equal OpenCalais.options
9
9
  end
10
10
 
11
11
  it "is initialized with specific values" do
12
12
  oc = OpenCalais::Client.new(:api_key => 'current')
13
- oc.current_options.wont_be_nil
14
- oc.current_options.wont_equal OpenCalais.options
15
- oc.current_options[:api_key].must_equal 'current'
16
- oc.api_key.must_equal 'current'
13
+ _(oc.current_options).wont_be_nil
14
+ _(oc.current_options).wont_equal OpenCalais.options
15
+ _(oc.current_options[:api_key]).must_equal 'current'
16
+ _(oc.api_key).must_equal 'current'
17
17
  end
18
18
 
19
19
  it "gets tags for text" do
20
20
  oc = OpenCalais::Client.new(:api_key => ENV['OPEN_CALAIS_KEY'])
21
21
  response = oc.enrich("Ruby on Rails is a fantastic web framework. It uses MVC, and the ruby programming language invented by Matz")
22
- response.wont_be_nil
23
- response.raw.wont_be_nil
22
+ _(response).wont_be_nil
23
+ _(response.raw).wont_be_nil
24
24
  end
25
25
 
26
26
  it "passes in header options in client" do
27
27
  oc = OpenCalais::Client.new(:api_key => ENV['OPEN_CALAIS_KEY'], :content_type => OpenCalais::CONTENT_TYPES[:html])
28
28
  response = oc.enrich("Ruby on Rails is a fantastic web framework. It uses MVC, and the ruby programming language invented by Matz")
29
- response.wont_be_nil
30
- response.raw.wont_be_nil
29
+ _(response).wont_be_nil
30
+ _(response.raw).wont_be_nil
31
31
  end
32
32
 
33
33
  it "passes in header optionsin enrich" do
34
34
  oc = OpenCalais::Client.new(:api_key => ENV['OPEN_CALAIS_KEY'])
35
35
  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]})
36
- response.wont_be_nil
37
- response.raw.wont_be_nil
36
+ _(response).wont_be_nil
37
+ _(response.raw).wont_be_nil
38
38
  end
39
39
  end
@@ -8,8 +8,7 @@ describe OpenCalais::Configuration do
8
8
 
9
9
  it "can be configured" do
10
10
  TestConfig.configure do |c|
11
- c.must_be_kind_of OpenCalais::Configuration
11
+ _(c).must_be_kind_of OpenCalais::Configuration
12
12
  end
13
13
  end
14
-
15
14
  end
@@ -3,22 +3,22 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
3
3
  describe OpenCalais do
4
4
 
5
5
  it "has default configuration options" do
6
- OpenCalais.options.wont_be_nil
7
- OpenCalais.options[:adapter].must_equal OpenCalais::Configuration::DEFAULT_ADAPTER
6
+ _(OpenCalais.options).wont_be_nil
7
+ _(OpenCalais.options[:adapter]).must_equal OpenCalais::Configuration::DEFAULT_ADAPTER
8
8
  end
9
9
 
10
10
  it "can be configured" do
11
- OpenCalais.must_respond_to(:configure)
11
+ _(OpenCalais).must_respond_to(:configure)
12
12
 
13
- OpenCalais.api_key.must_be_nil
13
+ _(OpenCalais.api_key).must_be_nil
14
14
 
15
15
  OpenCalais.configure do |c|
16
- c.must_equal OpenCalais
16
+ _(c).must_equal OpenCalais
17
17
  c.api_key = "this is a test key"
18
18
  end
19
19
 
20
- OpenCalais.api_key.must_equal "this is a test key"
20
+ _(OpenCalais.api_key).must_equal "this is a test key"
21
21
 
22
- OpenCalais.options[:api_key].must_equal "this is a test key"
22
+ _(OpenCalais.options[:api_key]).must_equal "this is a test key"
23
23
  end
24
24
  end
@@ -1,14 +1,13 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require 'simplecov'
4
- SimpleCov.start
5
-
6
3
  if ENV['TRAVIS']
7
4
  require 'coveralls'
8
5
  Coveralls.wear!
6
+ else
7
+ require 'simplecov'
8
+ SimpleCov.start
9
9
  end
10
10
 
11
- require 'minitest/spec'
12
11
  require 'minitest/autorun'
13
12
 
14
13
  require 'open_calais'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_calais
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-17 00:00:00.000000000 Z
11
+ date: 2020-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,48 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.7.4
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.7.4
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday_middleware
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.9'
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.9'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: multi_json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.0.3
48
- - - "~>"
49
- - !ruby/object:Gem::Version
50
- version: '1.0'
47
+ version: '0'
51
48
  type: :runtime
52
49
  prerelease: false
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
- version: 1.0.3
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '1.0'
54
+ version: '0'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: multi_xml
63
57
  requirement: !ruby/object:Gem::Requirement
@@ -88,20 +82,6 @@ dependencies:
88
82
  version: '0'
89
83
  - !ruby/object:Gem::Dependency
90
84
  name: hashie
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- version: 0.4.0
96
- type: :runtime
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: 0.4.0
103
- - !ruby/object:Gem::Dependency
104
- name: activesupport
105
85
  requirement: !ruby/object:Gem::Requirement
106
86
  requirements:
107
87
  - - ">="
@@ -115,7 +95,7 @@ dependencies:
115
95
  - !ruby/object:Gem::Version
116
96
  version: '0'
117
97
  - !ruby/object:Gem::Dependency
118
- name: stringex
98
+ name: activesupport
119
99
  requirement: !ruby/object:Gem::Requirement
120
100
  requirements:
121
101
  - - ">="
@@ -213,7 +193,7 @@ homepage: https://github.com/PRX/open_calais
213
193
  licenses:
214
194
  - MIT
215
195
  metadata: {}
216
- post_install_message:
196
+ post_install_message:
217
197
  rdoc_options: []
218
198
  require_paths:
219
199
  - lib
@@ -228,9 +208,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
208
  - !ruby/object:Gem::Version
229
209
  version: '0'
230
210
  requirements: []
231
- rubyforge_project:
232
- rubygems_version: 2.4.5
233
- signing_key:
211
+ rubygems_version: 3.1.2
212
+ signing_key:
234
213
  specification_version: 4
235
214
  summary: This is a gem to call the OpenCalais improved REST API.
236
215
  test_files: