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 +4 -4
- data/lib/open_calais/client.rb +1 -1
- data/lib/open_calais/connection.rb +2 -1
- data/lib/open_calais/response.rb +34 -19
- data/lib/open_calais/version.rb +1 -1
- data/test/test_helper.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4b330acabd2eb3abb0bb859c5dadc45983a1e6fc8d51cf948ff185f83bb720e
|
4
|
+
data.tar.gz: 86abb32abf7cb2cb58e4d64475aa6d36b4e8c39b2e56c82c84f1a7c4c5351db9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5511997370864eb6e6d476b0cef3f21329c33a0437eba8ce474d95f6d22d73b4b385ede8a1ed4d74be2470b8a4f1ced863655085c833a6a2c65ea500f49c1eb
|
7
|
+
data.tar.gz: 783d84b688f6281de0403dbbf5e07e304fe67e28b618322891c0e79e873670523890416de999ab3724385bdeb072d50c9a04d678c085673fb833ddf9bbe99bc6
|
data/lib/open_calais/client.rb
CHANGED
@@ -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
|
data/lib/open_calais/response.rb
CHANGED
@@ -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)
|
79
|
-
if
|
80
|
-
item[:
|
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
|
-
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
data/lib/open_calais/version.rb
CHANGED
data/test/test_helper.rb
CHANGED