textrazor 0.0.6 → 0.0.7

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
  SHA1:
3
- metadata.gz: 7bf3035d6e71f1f909d0369bc27cd46e582b9794
4
- data.tar.gz: 8f403a7ef549ea2f740ceb3017d43ee2ddae4099
3
+ metadata.gz: 17c7396459d35be185762cdd33e48422c550382f
4
+ data.tar.gz: 5162e912ea4c29574f42a291c08945d4be062f03
5
5
  SHA512:
6
- metadata.gz: f7cfcb4275bc41a291d0df2a05d69a75d85e2502f8471b74d1c6b1ebcebb52403bf90c9ef2c3011da6ae7dd30976bc36be50b0fc3f7a1d6734627c88d795c5c5
7
- data.tar.gz: 3132e5fb601f066516aeaf6b25050a25482bd24b01cf9fcf838bc18acbc5e6dcf758f542f6d0faf5dcb0ec019f3043aede30674076672f34c252e9425671a574
6
+ metadata.gz: e77134b8e36ed76bd5c779100cb203b2542cebd86d436d7054bb64b695f06aa1509077d811b838246cf86531cd90134562e9d6f225cf83b5e7e7d9b8ab797c50
7
+ data.tar.gz: 18b6342068846a68bd645599fb2a6c55da017320a1fe2d11cf7386ea192d4998431368cdd3acf72d360632fe9518dfe3f1b5e38af7baf774975b2df3b9c626f4
data/README.md CHANGED
@@ -21,6 +21,17 @@ Or download the git repository and install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
+ ### Configuration
25
+
26
+ By default TextRazor uses the secure SSL endpoint of the Text Razor API.
27
+ You can use the HTTP endpoint by configuring TextRazor:
28
+
29
+ ```
30
+ TextRazor.configure do |config|
31
+ config.secure = false
32
+ end
33
+ ```
34
+
24
35
  ### When the client is persisted across different requests
25
36
 
26
37
  ```
@@ -31,6 +42,8 @@ response = client.analyse('text to be analysed')
31
42
 
32
43
  response.topics # Returns an array of TextRazor::Topic instances.
33
44
 
45
+ response.coarse_topics # Returns an array of TextRazor::Topic instances.
46
+
34
47
  response.entities # Returns an array of TextRazor::Entity instances.
35
48
 
36
49
  response.words # Returns an array of TextRazor::Word instances.
@@ -39,13 +52,15 @@ response.words # Returns an array of TextRazor::Word instances.
39
52
 
40
53
  ### One off requests
41
54
 
42
- For making one off request to retrieve topics, entities or words you
43
- can use the following handy method. A new client is instantiated and
55
+ For making one off request to retrieve topics, entities or words you
56
+ can use the following handy method. A new client is instantiated and
44
57
  discarded everytime you make this request.
45
58
 
46
59
  ```
47
60
  TextRazor.topics('api_key', 'text')
48
61
 
62
+ TextRazor.coarse_topics('api_key', 'text')
63
+
49
64
  TextRazor.entities('api_key', 'text')
50
65
 
51
66
  TextRazor.words('api_key', 'text')
@@ -54,7 +69,7 @@ TextRazor.words('api_key', 'text')
54
69
 
55
70
  ## Next steps
56
71
 
57
- Only implemented this for topics, entities, and words. Also, implement
72
+ Only implemented this for topics, entities, and words. Also, implement
58
73
  it for other information that we can retrieve from the public API.
59
74
 
60
75
 
@@ -1,4 +1,5 @@
1
1
  require "textrazor/version"
2
+ require "textrazor/configuration"
2
3
  require "textrazor/client"
3
4
  require "textrazor/request"
4
5
  require "textrazor/response"
@@ -20,4 +21,20 @@ module TextRazor
20
21
  Client.words(api_key, text, options)
21
22
  end
22
23
 
24
+ class << self
25
+ attr_writer :configuration
26
+ end
27
+
28
+ def self.configuration
29
+ @configuration ||= Configuration.new
30
+ end
31
+
32
+ def self.reset
33
+ @configuration = Configuration.new
34
+ end
35
+
36
+ def self.configure
37
+ yield(configuration)
38
+ end
39
+
23
40
  end
@@ -31,6 +31,12 @@ module TextRazor
31
31
  topics
32
32
  end
33
33
 
34
+ def self.coarse_topics(api_key, text, options = {})
35
+ new(api_key, options.merge(extractors: ['topics'])).
36
+ analyse(text).
37
+ coarse_topics
38
+ end
39
+
34
40
  def self.entities(api_key, text, options = {})
35
41
  new(api_key, options.merge(extractors: ['entities'])).
36
42
  analyse(text).
@@ -0,0 +1,9 @@
1
+ module TextRazor
2
+ class Configuration
3
+ attr_accessor :secure
4
+
5
+ def initialize
6
+ @secure = true
7
+ end
8
+ end
9
+ end
@@ -4,6 +4,9 @@ module TextRazor
4
4
 
5
5
  class Request
6
6
 
7
+ HTTP_URL = 'http://api.textrazor.com/'
8
+ HTTPS_URL = 'https://api.textrazor.com/'
9
+
7
10
  OPTIONS_MAPPING = {
8
11
  extractors: 'extractors',
9
12
  cleanup_html: 'cleanupHTML',
@@ -13,7 +16,11 @@ module TextRazor
13
16
  }
14
17
 
15
18
  def self.post(text, options)
16
- ::RestClient.post "http://api.textrazor.com/", build_query(text, options), accept_encoding: 'gzip'
19
+ ::RestClient.post url, build_query(text, options), accept_encoding: 'gzip'
20
+ end
21
+
22
+ def self.url
23
+ TextRazor.configuration.secure ? HTTPS_URL : HTTP_URL
17
24
  end
18
25
 
19
26
  private
@@ -22,14 +22,11 @@ module TextRazor
22
22
  end
23
23
 
24
24
  def topics
25
- raw_topics = raw_response["topics"]
26
- return nil if raw_topics.nil?
25
+ @topics ||= parse_topics(raw_response["topics"])
26
+ end
27
27
 
28
- @topics ||= begin
29
- raw_topics.map do |topic_hash|
30
- Topic.create_from_hash(topic_hash)
31
- end
32
- end
28
+ def coarse_topics
29
+ @coarse_topics ||= parse_topics(raw_response["coarseTopics"])
33
30
  end
34
31
 
35
32
  def entities
@@ -72,6 +69,14 @@ module TextRazor
72
69
  code == 413
73
70
  end
74
71
 
72
+ def parse_topics(raw_topics)
73
+ return nil if raw_topics.nil?
74
+
75
+ raw_topics.map do |topic_hash|
76
+ Topic.create_from_hash(topic_hash)
77
+ end
78
+ end
79
+
75
80
  end
76
81
 
77
82
  end
@@ -1,3 +1,3 @@
1
1
  module TextRazor
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -123,7 +123,26 @@ module TextRazor
123
123
 
124
124
  it "should make correct calls" do
125
125
  client = OpenStruct.new
126
- response = OpenStruct.new topics: ['topic1']
126
+ response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
127
+
128
+ Client.should_receive(:new).
129
+ with(api_key, {extractors: ['topics']}).
130
+ and_return(client)
131
+
132
+ client.should_receive(:analyse).
133
+ with("text").
134
+ and_return(response)
135
+
136
+ Client.topics(api_key, 'text', {})
137
+ end
138
+
139
+ end
140
+
141
+ context ".coarse_topics" do
142
+
143
+ it "should make correct calls" do
144
+ client = OpenStruct.new
145
+ response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
127
146
 
128
147
  Client.should_receive(:new).
129
148
  with(api_key, {extractors: ['topics']}).
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ module TextRazor
4
+ describe Configuration do
5
+ describe "#secure" do
6
+ it "default value is true" do
7
+ Configuration.new.secure = true
8
+ end
9
+ end
10
+
11
+ describe "#secure=" do
12
+ it "can set value" do
13
+ config = Configuration.new
14
+ config.secure = false
15
+ expect(config.secure).to eq(false)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -4,6 +4,40 @@ module TextRazor
4
4
 
5
5
  describe Request do
6
6
 
7
+ describe ".url" do
8
+ after :each do
9
+ TextRazor.reset
10
+ end
11
+
12
+ let(:url) do
13
+ TextRazor::Request.url
14
+ end
15
+
16
+ context "with the config 'secure' set to false" do
17
+ before :each do
18
+ TextRazor.configure do |config|
19
+ config.secure = false
20
+ end
21
+ end
22
+
23
+ it "returns the unsecured URL" do
24
+ expect(url).to eq 'http://api.textrazor.com/'
25
+ end
26
+ end
27
+
28
+ context "with the config 'secure' set to true" do
29
+ before :each do
30
+ TextRazor.configure do |config|
31
+ config.secure = true
32
+ end
33
+ end
34
+
35
+ it "returns the unsecured URL" do
36
+ expect(url).to eq 'https://api.textrazor.com/'
37
+ end
38
+ end
39
+ end
40
+
7
41
  context ".post" do
8
42
 
9
43
  context "default options" do
@@ -12,7 +46,7 @@ module TextRazor
12
46
  options = {api_key: 'api_key', extractors: %w(entities topics words dependency-trees relations entailments)}
13
47
 
14
48
  ::RestClient.should_receive(:post).
15
- with("http://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key',
49
+ with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key',
16
50
  "extractors" => "entities,topics,words,dependency-trees,relations,entailments" }, accept_encoding: 'gzip')
17
51
 
18
52
  Request.post('text', options)
@@ -27,7 +61,7 @@ module TextRazor
27
61
  language: 'fre', filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2)}
28
62
 
29
63
  ::RestClient.should_receive(:post).
30
- with("http://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
64
+ with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
31
65
  "cleanupHTML" => true, "languageOverride" => 'fre', "entities.filterDbpediaTypes" => "type1",
32
66
  "entities.filterFreebaseTypes" => "type2" },
33
67
  accept_encoding: 'gzip')
@@ -91,6 +91,41 @@ module TextRazor
91
91
 
92
92
  end
93
93
 
94
+ context "#coarse_topics" do
95
+
96
+ context "if there are topics returned from api" do
97
+
98
+ it "should return topics" do
99
+ body = "\n {\"time\":\"0.013219\",\"response\":{\"language\":\"eng\",\"languageIsReliable\":true,\"coarseTopics\":[{\"id\":0,\"label\":\"Airlines \",\"wikiLink\":\"http://en.wikipedia.org/Category:Airlines_by_country\",\"score\":0.199069},{\"id\":1,\"label\":\"Companies \",\"wikiLink\":\"http://en.wikipedia.org/Category:Companies_by_year_of_establishment\",\"score\":0.136068}]}} \n"
100
+
101
+ http_response = ::OpenStruct.new code: 200, body: body
102
+
103
+ response = Response.new(http_response)
104
+
105
+ topics = response.coarse_topics
106
+
107
+ expect(topics).to_not be_nil
108
+ expect(topics.size).to eq(2)
109
+ end
110
+
111
+ end
112
+
113
+ context "if there are no topics returned from api" do
114
+
115
+ it "should return nil" do
116
+ body = "{\"time\":\"0.013219\",\"response\":{\"language\":\"eng\",\"languageIsReliable\":true}}"
117
+
118
+ http_response = ::OpenStruct.new code: 200, body: body
119
+
120
+ response = Response.new(http_response)
121
+
122
+ expect(response.topics).to be_nil
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
94
129
  context "#entities" do
95
130
 
96
131
  context "if there are any entities returned" do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe TextRazor do
4
4
 
5
- context ".topics" do
5
+ describe ".topics" do
6
6
 
7
7
  it "should make correct calls" do
8
8
  TextRazor::Client.should_receive(:topics).
@@ -13,7 +13,7 @@ describe TextRazor do
13
13
 
14
14
  end
15
15
 
16
- context ".entities" do
16
+ describe ".entities" do
17
17
 
18
18
  it "should make correct calls" do
19
19
  TextRazor::Client.should_receive(:entities).
@@ -24,7 +24,7 @@ describe TextRazor do
24
24
 
25
25
  end
26
26
 
27
- context ".words" do
27
+ describe ".words" do
28
28
 
29
29
  it "should make correct calls" do
30
30
  TextRazor::Client.should_receive(:words).
@@ -35,4 +35,18 @@ describe TextRazor do
35
35
 
36
36
  end
37
37
 
38
+ describe ".reset" do
39
+ before :each do
40
+ TextRazor.configure do |config|
41
+ config.secure = false
42
+ end
43
+ end
44
+
45
+ it "resets the configuration" do
46
+ TextRazor.reset
47
+ config = TextRazor.configuration
48
+ expect(config.secure).to eq(true)
49
+ end
50
+ end
51
+
38
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textrazor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anuj Dutta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-21 00:00:00.000000000 Z
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -109,6 +109,7 @@ files:
109
109
  - Rakefile
110
110
  - lib/textrazor.rb
111
111
  - lib/textrazor/client.rb
112
+ - lib/textrazor/configuration.rb
112
113
  - lib/textrazor/entity.rb
113
114
  - lib/textrazor/request.rb
114
115
  - lib/textrazor/response.rb
@@ -116,6 +117,7 @@ files:
116
117
  - lib/textrazor/version.rb
117
118
  - lib/textrazor/word.rb
118
119
  - spec/lib/textrazor/client_spec.rb
120
+ - spec/lib/textrazor/configuration_spec.rb
119
121
  - spec/lib/textrazor/entity_spec.rb
120
122
  - spec/lib/textrazor/request_spec.rb
121
123
  - spec/lib/textrazor/response_spec.rb
@@ -150,6 +152,7 @@ specification_version: 4
150
152
  summary: An api wrapper for text razor in ruby
151
153
  test_files:
152
154
  - spec/lib/textrazor/client_spec.rb
155
+ - spec/lib/textrazor/configuration_spec.rb
153
156
  - spec/lib/textrazor/entity_spec.rb
154
157
  - spec/lib/textrazor/request_spec.rb
155
158
  - spec/lib/textrazor/response_spec.rb