open_calais 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +20 -19
- data/lib/open_calais/client.rb +4 -4
- data/lib/open_calais/configuration.rb +1 -1
- data/lib/open_calais/connection.rb +6 -4
- data/lib/open_calais/response.rb +2 -0
- data/lib/open_calais/version.rb +3 -1
- data/test/test_helper.rb +2 -0
- metadata +8 -3
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# OpenCalais
|
2
2
|
|
3
|
-
Ruby gem to access the
|
4
|
-
It uses Faraday to abstract HTTP library (defaults to use excon because
|
3
|
+
Ruby gem to access the [OpenCalais API](http://www.opencalais.com/documentation/calais-web-service-api/api-invocation/rest), using the new-ish REST API, and JSON responses.
|
4
|
+
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.
|
5
5
|
|
6
|
-
It returns a parsed version of the response, but the response contains the raw response (converted from json to hashes/arrays/string/etc).
|
6
|
+
It returns a parsed version of the response, but the response also contains the raw response (converted from json to hashes/arrays/string/etc).
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -21,28 +21,29 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
OpenCalais has one main method,
|
24
|
+
OpenCalais has one main method, `enrich`, and so does the gem:
|
25
25
|
|
26
|
-
|
26
|
+
```ruby
|
27
|
+
require 'open_calais'
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
# you can configure for all calls
|
30
|
+
OpenCalais.configure do |c|
|
31
|
+
c.api_key = "this is a test key"
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
# or you can configure for a single call
|
35
|
+
open_calais = OpenCalais::Client.new(:api_key=>'an api key')
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
# it returns a OpenCalais::Response instance
|
38
|
+
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.')
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
# and has been parsed a bit to get :language, :topics, :tags, :entities, :relations, :locations
|
43
|
-
# as lists of hashes
|
44
|
-
response.tags.each{|t| puts t[:name] }
|
40
|
+
# which has the 'raw' response
|
41
|
+
response.raw
|
45
42
|
|
43
|
+
# and has been parsed a bit to get :language, :topics, :tags, :entities, :relations, :locations
|
44
|
+
# as lists of hashes
|
45
|
+
response.tags.each{|t| puts t[:name] }
|
46
|
+
```
|
46
47
|
|
47
48
|
## Contributing
|
48
49
|
|
data/lib/open_calais/client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require 'open_calais/configuration'
|
2
4
|
require 'open_calais/connection'
|
3
5
|
require 'open_calais/response'
|
@@ -35,12 +37,10 @@ module OpenCalais
|
|
35
37
|
|
36
38
|
def enrich(text, opts={})
|
37
39
|
raise 'Specify a value for the text' unless (text && text.length > 0)
|
40
|
+
options = current_options.merge(opts)
|
38
41
|
|
39
|
-
response = connection.post do |request|
|
42
|
+
response = connection(options).post do |request|
|
40
43
|
request.body = text
|
41
|
-
OpenCalais::HEADERS.each do |k,v|
|
42
|
-
request.headers[v] = opts[k] if opts.key?(k)
|
43
|
-
end
|
44
44
|
end
|
45
45
|
OpenCalais::Response.new(response)
|
46
46
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require 'faraday_middleware'
|
2
4
|
|
3
5
|
module OpenCalais
|
@@ -11,7 +13,7 @@ module OpenCalais
|
|
11
13
|
:ssl
|
12
14
|
].freeze
|
13
15
|
|
14
|
-
def
|
16
|
+
def merge_default_options(opts={})
|
15
17
|
headers = opts.delete(:headers) || {}
|
16
18
|
options = {
|
17
19
|
:headers => {
|
@@ -32,13 +34,14 @@ module OpenCalais
|
|
32
34
|
:url => endpoint
|
33
35
|
}.merge(opts)
|
34
36
|
options[:headers] = options[:headers].merge(headers)
|
37
|
+
OpenCalais::HEADERS.each{|k,v| options[:headers][v] = options.delete(k) if options.key?(k)}
|
35
38
|
options
|
36
39
|
end
|
37
40
|
|
38
41
|
def connection(options={})
|
39
|
-
opts =
|
42
|
+
opts = merge_default_options(options)
|
40
43
|
Faraday::Connection.new(opts) do |connection|
|
41
|
-
connection.request
|
44
|
+
connection.request :url_encoded
|
42
45
|
connection.response :mashify
|
43
46
|
connection.response :logger if ENV['DEBUG']
|
44
47
|
|
@@ -49,7 +52,6 @@ module OpenCalais
|
|
49
52
|
end
|
50
53
|
|
51
54
|
connection.adapter(adapter)
|
52
|
-
|
53
55
|
end
|
54
56
|
|
55
57
|
end
|
data/lib/open_calais/response.rb
CHANGED
data/lib/open_calais/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -187,12 +187,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
187
187
|
- - ! '>='
|
188
188
|
- !ruby/object:Gem::Version
|
189
189
|
version: '0'
|
190
|
+
segments:
|
191
|
+
- 0
|
192
|
+
hash: -4380033744204018911
|
190
193
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
191
194
|
none: false
|
192
195
|
requirements:
|
193
196
|
- - ! '>='
|
194
197
|
- !ruby/object:Gem::Version
|
195
198
|
version: '0'
|
199
|
+
segments:
|
200
|
+
- 0
|
201
|
+
hash: -4380033744204018911
|
196
202
|
requirements: []
|
197
203
|
rubyforge_project:
|
198
204
|
rubygems_version: 1.8.23
|
@@ -204,4 +210,3 @@ test_files:
|
|
204
210
|
- test/configuration_test.rb
|
205
211
|
- test/open_calais_test.rb
|
206
212
|
- test/test_helper.rb
|
207
|
-
has_rdoc:
|