open_calais 0.4.0 → 0.4.1

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
2
  SHA256:
3
- metadata.gz: 34b051160ea2d22366cacafdcb786d98e7671c49cfd67798095e0563b08766bc
4
- data.tar.gz: fc056e88eb523daeaa03d14e5b70ceb5797b352e062dbf15d7807b204dc99491
3
+ metadata.gz: d4b330acabd2eb3abb0bb859c5dadc45983a1e6fc8d51cf948ff185f83bb720e
4
+ data.tar.gz: 86abb32abf7cb2cb58e4d64475aa6d36b4e8c39b2e56c82c84f1a7c4c5351db9
5
5
  SHA512:
6
- metadata.gz: 8765c48314b227bee03d5fd88c7e6e178724574a316b139c651a27da1ad8d992fd33bf54ae419d76a68d88d44bc0535df5195c47ce905e405104adc13f13b08d
7
- data.tar.gz: 01b798fb0b54312b077c1ef3cc38fa926dc86ce395323bcfeef4ae24410838bc1b944b6d363c1ac49d36282761eef0f7996f9f0e2aa3e3d2cca6f86ade3c3a21
6
+ metadata.gz: f5511997370864eb6e6d476b0cef3f21329c33a0437eba8ce474d95f6d22d73b4b385ede8a1ed4d74be2470b8a4f1ced863655085c833a6a2c65ea500f49c1eb
7
+ data.tar.gz: 783d84b688f6281de0403dbbf5e07e304fe67e28b618322891c0e79e873670523890416de999ab3724385bdeb072d50c9a04d678c085673fb833ddf9bbe99bc6
@@ -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
@@ -47,9 +47,10 @@ module OpenCalais
47
47
  connection.request :url_encoded
48
48
  connection.response :mashify
49
49
  connection.response :logger if ENV['DEBUG']
50
+ connection.response :raise_error
50
51
 
51
52
  if opts[:headers][OpenCalais::HEADERS[:output_format]] == OpenCalais::OUTPUT_FORMATS[:json]
52
- connection.response :json
53
+ connection.response :json, :content_type => /\bjson$/
53
54
  else
54
55
  connection.response :xml
55
56
  end
@@ -5,10 +5,15 @@ require 'active_support'
5
5
  module OpenCalais
6
6
  class Response
7
7
 
8
+ ALLOWED_OPTIONS = [
9
+ :ignore_literal_match
10
+ ].freeze
11
+
8
12
  include ActiveSupport::Inflector
9
13
  attr_accessor :raw, :language, :topics, :tags, :entities, :relations, :locations
10
14
 
11
- def initialize(response)
15
+ def initialize(response, options={})
16
+ @options = merge_default_options(options)
12
17
  @raw = response
13
18
 
14
19
  @language = 'English'
@@ -18,7 +23,15 @@ module OpenCalais
18
23
  @relations = []
19
24
  @locations = []
20
25
 
21
- 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 }
22
35
  end
23
36
 
24
37
  def humanize_topic(topic)
@@ -33,7 +46,7 @@ module OpenCalais
33
46
  end
34
47
  end
35
48
 
36
- def parse(response)
49
+ def parse(response, options={})
37
50
  r = response.body
38
51
  @language = r.doc.meta.language rescue nil
39
52
  @language = nil if @language == 'InputTextTooShort'
@@ -52,9 +65,9 @@ module OpenCalais
52
65
  :score => importance_to_score(v.importance)
53
66
  }
54
67
  when 'entities'
55
- parse_entity(k, v)
68
+ parse_entity(k, v, options)
56
69
  when 'relations'
57
- parse_relation(k, v)
70
+ parse_relation(k, v, options)
58
71
  end
59
72
  end
60
73
 
@@ -63,7 +76,7 @@ module OpenCalais
63
76
  self.tags.delete_if { |tag| topic_names.include?(tag[:name]) }
64
77
  end
65
78
 
66
- def parse_entity(k, v)
79
+ def parse_entity(k, v, options)
67
80
  if v.name.nil?
68
81
  v.name = v.instances.first[:exact]
69
82
  end
@@ -75,31 +88,33 @@ module OpenCalais
75
88
  :score => v.relevance
76
89
  }
77
90
 
78
- instances = Array(v.instances).select { |i| i.exact.downcase != item[:name].downcase }
79
- if instances && instances.size > 0
80
- item[:matches] = instances
91
+ instances = Array(v.instances)
92
+ if options[:ignore_literal_match]
93
+ instances = instances.select { |i| i.exact.downcase != item[:name].downcase }
81
94
  end
95
+ item[:matches] = instances unless instances.empty?
82
96
 
83
97
  if OpenCalais::GEO_TYPES.include?(v._type)
84
- self.locations << set_location_info(item, v)
98
+ item = set_location_info(item, v)
99
+ self.locations << item
85
100
  else
86
101
  self.entities << item
87
102
  end
88
103
  end
89
104
 
90
105
  def set_location_info(item, v)
91
- if (v.resolutions && v.resolutions.size > 0)
92
- r = v.resolutions.first
93
- item[:name] = r.shortname || r.name
94
- item[:latitude] = r.latitude
95
- item[:longitude] = r.longitude
96
- item[:country] = r.containedbycountry if r.containedbycountry
97
- item[:state] = r.containedbystate if r.containedbystate
98
- end
106
+ return item if 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
99
114
  item
100
115
  end
101
116
 
102
- def parse_relation(k, v)
117
+ def parse_relation(k, v, _options)
103
118
  item = v.reject { |key,val| key[0] == '_' || key == 'instances' } || {}
104
119
  item[:type] = transliterate(v._type).titleize
105
120
  self.relations << item
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module OpenCalais
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -1,11 +1,11 @@
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
11
  require 'minitest/autorun'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_calais
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz