aylien_text_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +28 -0
  5. data/LICENSE +13 -0
  6. data/README.md +67 -0
  7. data/Rakefile +8 -0
  8. data/aylien_text_api.gemspec +24 -0
  9. data/config/app_config.yml.example +7 -0
  10. data/lib/aylien_text_api.rb +23 -0
  11. data/lib/aylien_text_api/client.rb +246 -0
  12. data/lib/aylien_text_api/configuration.rb +57 -0
  13. data/lib/aylien_text_api/connection.rb +71 -0
  14. data/lib/aylien_text_api/error.rb +110 -0
  15. data/lib/aylien_text_api/version.rb +17 -0
  16. data/spec/fixtures/aylien_text_api/client/classify_with_invalid_client.yml +36 -0
  17. data/spec/fixtures/aylien_text_api/client/classify_with_invalid_params.yml +40 -0
  18. data/spec/fixtures/aylien_text_api/client/classify_with_text.yml +48 -0
  19. data/spec/fixtures/aylien_text_api/client/classify_with_unauthenticated_client.yml +40 -0
  20. data/spec/fixtures/aylien_text_api/client/classify_with_valid_url.yml +98 -0
  21. data/spec/fixtures/aylien_text_api/client/classify_with_value_as_text.yml +48 -0
  22. data/spec/fixtures/aylien_text_api/client/classify_with_value_as_valid_url.yml +98 -0
  23. data/spec/fixtures/aylien_text_api/client/concepts_with_invalid_client.yml +36 -0
  24. data/spec/fixtures/aylien_text_api/client/concepts_with_invalid_params.yml +40 -0
  25. data/spec/fixtures/aylien_text_api/client/concepts_with_text.yml +44 -0
  26. data/spec/fixtures/aylien_text_api/client/concepts_with_unauthenticated_client.yml +40 -0
  27. data/spec/fixtures/aylien_text_api/client/concepts_with_valid_url.yml +63 -0
  28. data/spec/fixtures/aylien_text_api/client/concepts_with_value_as_text.yml +44 -0
  29. data/spec/fixtures/aylien_text_api/client/concepts_with_value_as_valid_url.yml +63 -0
  30. data/spec/fixtures/aylien_text_api/client/entities_with_invalid_client.yml +36 -0
  31. data/spec/fixtures/aylien_text_api/client/entities_with_invalid_params.yml +40 -0
  32. data/spec/fixtures/aylien_text_api/client/entities_with_text.yml +43 -0
  33. data/spec/fixtures/aylien_text_api/client/entities_with_unauthenticated_client.yml +40 -0
  34. data/spec/fixtures/aylien_text_api/client/entities_with_valid_url.yml +302 -0
  35. data/spec/fixtures/aylien_text_api/client/entities_with_value_as_text.yml +43 -0
  36. data/spec/fixtures/aylien_text_api/client/entities_with_value_as_valid_url.yml +302 -0
  37. data/spec/fixtures/aylien_text_api/client/extract_with_invalid_client.yml +36 -0
  38. data/spec/fixtures/aylien_text_api/client/extract_with_invalid_params.yml +40 -0
  39. data/spec/fixtures/aylien_text_api/client/extract_with_unauthenticated_client.yml +40 -0
  40. data/spec/fixtures/aylien_text_api/client/extract_with_valid_url.yml +185 -0
  41. data/spec/fixtures/aylien_text_api/client/extract_with_value_as_text.yml +40 -0
  42. data/spec/fixtures/aylien_text_api/client/extract_with_value_as_valid_url.yml +185 -0
  43. data/spec/fixtures/aylien_text_api/client/hashtags_with_invalid_client.yml +36 -0
  44. data/spec/fixtures/aylien_text_api/client/hashtags_with_invalid_params.yml +40 -0
  45. data/spec/fixtures/aylien_text_api/client/hashtags_with_text.yml +48 -0
  46. data/spec/fixtures/aylien_text_api/client/hashtags_with_unauthenticated_client.yml +40 -0
  47. data/spec/fixtures/aylien_text_api/client/hashtags_with_valid_url.yml +58 -0
  48. data/spec/fixtures/aylien_text_api/client/hashtags_with_value_as_text.yml +48 -0
  49. data/spec/fixtures/aylien_text_api/client/hashtags_with_value_as_valid_url.yml +58 -0
  50. data/spec/fixtures/aylien_text_api/client/language_with_invalid_client.yml +36 -0
  51. data/spec/fixtures/aylien_text_api/client/language_with_invalid_params.yml +40 -0
  52. data/spec/fixtures/aylien_text_api/client/language_with_unauthenticated_client.yml +40 -0
  53. data/spec/fixtures/aylien_text_api/client/language_with_valid_text.yml +41 -0
  54. data/spec/fixtures/aylien_text_api/client/language_with_valid_url.yml +57 -0
  55. data/spec/fixtures/aylien_text_api/client/language_with_value_as_text.yml +41 -0
  56. data/spec/fixtures/aylien_text_api/client/language_with_value_as_valid_url.yml +57 -0
  57. data/spec/fixtures/aylien_text_api/client/related_with_invalid_client.yml +36 -0
  58. data/spec/fixtures/aylien_text_api/client/related_with_invalid_params.yml +40 -0
  59. data/spec/fixtures/aylien_text_api/client/related_with_phrase.yml +60 -0
  60. data/spec/fixtures/aylien_text_api/client/related_with_unauthenticated_client.yml +40 -0
  61. data/spec/fixtures/aylien_text_api/client/related_with_value_as_phrase.yml +60 -0
  62. data/spec/fixtures/aylien_text_api/client/related_with_value_as_valid_url.yml +40 -0
  63. data/spec/fixtures/aylien_text_api/client/sentiment_with_invalid_client.yml +36 -0
  64. data/spec/fixtures/aylien_text_api/client/sentiment_with_invalid_params.yml +40 -0
  65. data/spec/fixtures/aylien_text_api/client/sentiment_with_text.yml +53 -0
  66. data/spec/fixtures/aylien_text_api/client/sentiment_with_unauthenticated_client.yml +40 -0
  67. data/spec/fixtures/aylien_text_api/client/sentiment_with_valid_url.yml +57 -0
  68. data/spec/fixtures/aylien_text_api/client/sentiment_with_value_as_text.yml +53 -0
  69. data/spec/fixtures/aylien_text_api/client/sentiment_with_value_as_valid_url.yml +57 -0
  70. data/spec/fixtures/aylien_text_api/client/summarize_with_invalid_client.yml +36 -0
  71. data/spec/fixtures/aylien_text_api/client/summarize_with_invalid_params.yml +41 -0
  72. data/spec/fixtures/aylien_text_api/client/summarize_with_title_and_text.yml +78 -0
  73. data/spec/fixtures/aylien_text_api/client/summarize_with_unauthenticated_client.yml +40 -0
  74. data/spec/fixtures/aylien_text_api/client/summarize_with_valid_url.yml +65 -0
  75. data/spec/fixtures/aylien_text_api/client/summarize_with_value_as_text_and_title.yml +78 -0
  76. data/spec/fixtures/aylien_text_api/client/summarize_with_value_as_text_and_without_title.yml +41 -0
  77. data/spec/fixtures/aylien_text_api/client/summarize_with_value_as_valid_url.yml +65 -0
  78. data/spec/lib/aylien_text_api/classify.rb +86 -0
  79. data/spec/lib/aylien_text_api/client_spec.rb +23 -0
  80. data/spec/lib/aylien_text_api/concepts.rb +81 -0
  81. data/spec/lib/aylien_text_api/entities.rb +81 -0
  82. data/spec/lib/aylien_text_api/extract.rb +81 -0
  83. data/spec/lib/aylien_text_api/hashtags.rb +87 -0
  84. data/spec/lib/aylien_text_api/language.rb +79 -0
  85. data/spec/lib/aylien_text_api/related.rb +80 -0
  86. data/spec/lib/aylien_text_api/sentiment.rb +91 -0
  87. data/spec/lib/aylien_text_api/summarize.rb +95 -0
  88. data/spec/spec_helper.rb +16 -0
  89. metadata +193 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 216b14bf8afcd4915cd06fd5435dd12bd6dfdb13
4
+ data.tar.gz: 7cc5feef68d09b68ed4324503be4a3a1549e95a7
5
+ SHA512:
6
+ metadata.gz: b27a6c60c851ba8c45cadd4cdf4e1d960891ec01e3f7943a1f77550519cd01cdcdc7fbff759ec3d94a9eb33eb88bc56852befca4ac700bb4d02a10cdb8364e75
7
+ data.tar.gz: 429e9b058d371f4acbdc515c90b5d4a743aa5e23c40b8893e9453dcc5d80a48fbeac690cb9c2d7c5db066dd53d92fae7483dff933305eec3702671cec7fdf3e8
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ config/app_config.yml
2
+ doc/
3
+ .yardoc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aylien_text_api (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.3.6)
10
+ crack (0.4.2)
11
+ safe_yaml (~> 1.0.0)
12
+ minitest (5.4.3)
13
+ rake (10.4.0)
14
+ safe_yaml (1.0.4)
15
+ vcr (2.9.3)
16
+ webmock (1.20.4)
17
+ addressable (>= 2.3.6)
18
+ crack (>= 0.3.2)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ aylien_text_api!
25
+ minitest (~> 5.4)
26
+ rake (~> 10.4)
27
+ vcr (~> 2.9)
28
+ webmock (~> 1.20)
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2014 Aylien, Inc. All Rights Reserved.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ About
2
+ =====
3
+
4
+ This is the Ruby client library for AYLIEN's APIs. If you haven't already done so, you will need to [sign up](https://developer.aylien.com/signup).
5
+
6
+
7
+ Installation
8
+ ============
9
+ gem install aylien_text_api
10
+
11
+ See the [Developers Guide](https://developer.aylien.com/docs) for additional documentation.
12
+
13
+ Configuration
14
+ =============
15
+ Aylien Text API needs app_id and app_key which you can get it from [Text API website](https://developer.aylien.com/signup).
16
+
17
+ You can pass configuration options as a block to AylienTextApi::Client.new.
18
+
19
+ ````ruby
20
+ require 'aylien_text_api'
21
+
22
+ client = AylienTextApi::Client.new do |config|
23
+ config.app_id = "YOUR_APP_ID"
24
+ config.app_key = "YOUR_APP_KEY"
25
+ end
26
+ ````
27
+
28
+ or pass them as parameters to AylienTextApi::Client class.
29
+
30
+ ````ruby
31
+ require 'aylien_text_api'
32
+
33
+ client = AylienTextApi::Client.new(app_id: "YOUR APP ID", app_key: "YOUR APP KEY")
34
+ ````
35
+
36
+ Examples
37
+ =====
38
+ After configuring a client, you can do the following things:
39
+
40
+ ````ruby
41
+ client.extract url: "http://techcrunch.com/2014/02/27/aylien-launches-text-analysis-api-to-help-developers-extract-meaning-from-documents/"
42
+ # => {
43
+ :title=>"Aylien Launches Text-Analysis API To Help Developers...",
44
+ :article=>"Working with text is often a messy business for...",
45
+ :image=>"", :author=>"Frederic Lardinois", :videos=>[],
46
+ :feeds=>["http://techcrunch.com/2014/02/27/aylien-...
47
+ }
48
+ ````
49
+
50
+ If any errors happen during the call, nil will be returned. If destructive methods are used, an exception corresponding to the error will be returned.
51
+
52
+ ````ruby
53
+ client.classify! url: "http://www.bbc.com/sport/0/football/25912393"
54
+ # => {:text=>"Lionel Messi: Forward is not for sale, says...,
55
+ :language=>"en",
56
+ :categories=>[{:label=>"sport - soccer", :code=>"15054000", :confidence=>1.0}]
57
+ }
58
+ ````
59
+
60
+ Third Party Libraries and Dependencies
61
+ ======================================
62
+ For development you will also need the following libraries:
63
+
64
+ * rake
65
+ * minitest
66
+ * vcr
67
+ * webmock
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.test_files = FileList['spec/lib/aylien_text_api/*_spec.rb']
5
+ t.verbose = true
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "aylien_text_api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "aylien_text_api"
7
+ s.version = AylienTextApi::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Aylien Inc.", "Hamed Ramezanian"]
10
+ s.email = ["hello@aylien.com", "hamed.r.nik@gmail.com"]
11
+ s.homepage = "https://github.com/AYLIEN/aylien_textapi_ruby"
12
+ s.summary = %q{Aylien Text API is a package of nine different Natural Language Processing, Information Retrieval and Machine Learning APIs that can be quickly and easily adapted to your processes and applications.}
13
+ s.description = %q{Aylien Text API is a package of nine different Natural Language Processing, Information Retrieval and Machine Learning APIs that can be quickly and easily adapted to your processes and applications.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency 'rake', '~> 10.4'
21
+ s.add_development_dependency 'minitest', '~> 5.4'
22
+ s.add_development_dependency 'vcr', '~> 2.9'
23
+ s.add_development_dependency 'webmock', '~> 1.20'
24
+ end
@@ -0,0 +1,7 @@
1
+ text_api:
2
+ correct_keys:
3
+ app_id: YOUR APP_ID
4
+ app_key: YOUR APP_KEY
5
+ incorrect_keys:
6
+ app_id: random_string
7
+ app_key: random_string
@@ -0,0 +1,23 @@
1
+ # Copyright 2014 Aylien, Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aylien_text_api/version'
16
+ require 'aylien_text_api/configuration'
17
+ require 'aylien_text_api/error'
18
+ require 'aylien_text_api/client'
19
+ require 'aylien_text_api/connection'
20
+
21
+ module AylienTextApi
22
+ extend Configuration
23
+ end
@@ -0,0 +1,246 @@
1
+ # Copyright 2014 Aylien, Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module AylienTextApi
16
+ # The Client class is the main class for calling Text API endpoints
17
+ class Client
18
+ attr_accessor *Configuration::VALID_CONFIG_KEYS
19
+
20
+ # Creates an Client object.
21
+ # @param [Hash] options Configuration params
22
+ # @option options [String] :app_id The APP_ID
23
+ # @option options [String] :app_key The APP_KEY
24
+ # @option options [String] :base_uri ('https://api.aylien.com/api/v1/')
25
+ # An URL that points to the Aylien Text API
26
+ # @option options [Symbol] :method (:post) Request method.
27
+ # @option options [String] :user_agent Request user-agent
28
+ def initialize(options={})
29
+ merged_options = AylienTextApi.options.merge(options)
30
+
31
+ Configuration::VALID_CONFIG_KEYS.each do |key|
32
+ send("#{key}=", merged_options[key])
33
+ end
34
+
35
+ Configuration::ENDPOINTS.each do |endpoint|
36
+ self.class.send(:define_method, "#{endpoint}!") do |value=nil, params={}|
37
+ endpoint, params, config = common_endpoint(value, params, endpoint)
38
+ Connection.new(endpoint, params, config).request!
39
+ end
40
+ end
41
+ end
42
+
43
+ # Extracts the main body of article, including embedded media such as
44
+ # images & videos from a URL and removes all the surrounding clutter.
45
+ #
46
+ # @param [String] value (nil) URL or Text
47
+ # @param [Hash] params The extract endpoint options
48
+ # @option params [String] :url The URL
49
+ # @option params [Boolean] :best_image (false)
50
+ # Whether extract the best image of the article
51
+ #
52
+ # @return [Hash, nil] A hash of result. See
53
+ # http://aylien.com/text-api-doc#extract for more information
54
+ # on the data returned.
55
+ #
56
+ def extract(value=nil, params={})
57
+ endpoint, params, config = common_endpoint(value, params, :extract)
58
+ Connection.new(endpoint, params, config).request
59
+ end
60
+
61
+ # Classifies a piece of text according to IPTC NewsCode standard.
62
+ #
63
+ # @param [String] value (nil) URL or Text
64
+ # @param [Hash] params The classify endpoint options
65
+ # @option params [String] :url The URL
66
+ # @option params [String] :text Text
67
+ # @option params [String] :language ('en') Language of text.
68
+ # Valid options are en, de, fr, es, it, pt, and auto.
69
+ # If set to auto, it'll try to detect the language.
70
+ #
71
+ # @return [Hash, nil] A hash of result. See
72
+ # http://aylien.com/text-api-doc#classify for more information
73
+ # on the data returned.
74
+ #
75
+ def classify(value=nil, params={})
76
+ endpoint, params, config = common_endpoint(value, params, :classify)
77
+ Connection.new(endpoint, params, config).request
78
+ end
79
+
80
+ # Extracts named entities mentioned in a document, disambiguates
81
+ # and cross-links them to DBPedia and Linked Data entities, along with
82
+ # their semantic types (including DBPedia and schema.org).
83
+ #
84
+ # @param [String] value (nil) URL or Text
85
+ # @param [Hash] params The concepts endpoint options
86
+ # @option params [String] :url The URL
87
+ # @option params [String] :text Text
88
+ # @option params [String] :language ('en') Language of text.
89
+ # Valid options are en, de, fr, es, it, pt, and auto.
90
+ # If set to auto, it'll try to detect the language.
91
+ #
92
+ # @return [Hash, nil] A hash of result. See
93
+ # http://aylien.com/text-api-doc#concepts for more information
94
+ # on the data returned.
95
+ #
96
+ def concepts(value=nil, params={})
97
+ endpoint, params, config = common_endpoint(value, params, :concepts)
98
+ Connection.new(endpoint, params, config).request
99
+ end
100
+
101
+ # Suggests hashtags describing the document.
102
+ #
103
+ # @param [String] value (nil) URL or Text
104
+ # @param [Hash] params The hashtags endpoint options
105
+ # @option params [String] :url The URL
106
+ # @option params [String] :text Text
107
+ # @option params [String] :language ('en') Language of text.
108
+ # Valid options are en, de, fr, es, it, pt, and auto.
109
+ # If set to auto, it'll try to detect the language.
110
+ #
111
+ # @return [Hash, nil] A hash of result. See
112
+ # http://aylien.com/text-api-doc#hashtags for more information
113
+ # on the data returned.
114
+ #
115
+ def hashtags(value=nil, params={})
116
+ endpoint, params, config = common_endpoint(value, params, :hashtags)
117
+ Connection.new(endpoint, params, config).request
118
+ end
119
+
120
+ # Extracts named entities (people, organizations and locations) and
121
+ # values (URLs, emails, telephone numbers, currency amounts and percentages)
122
+ # mentioned in a body of text.
123
+ #
124
+ # @param [String] value (nil) URL or Text
125
+ # @param [Hash] params The entities endpoint options
126
+ # @option params [String] :url The URL
127
+ # @option params [String] :text Text
128
+ #
129
+ # @return [Hash, nil] A hash of result. See
130
+ # http://aylien.com/text-api-doc#entities for more information
131
+ # on the data returned.
132
+ #
133
+ def entities(value=nil, params={})
134
+ endpoint, params, config = common_endpoint(value, params, :entities)
135
+ Connection.new(endpoint, params, config).request
136
+ end
137
+
138
+ # Detects the main language a document is written in and returns it
139
+ # in ISO 639-1 format.
140
+ #
141
+ # @param [String] value (nil) URL or Text
142
+ # @param [Hash] params The language endpoint options
143
+ # @option params [String] :url The URL
144
+ # @option params [String] :text Text
145
+ #
146
+ # @return [Hash, nil] A hash of result. See
147
+ # http://aylien.com/text-api-doc#language for more information
148
+ # on the data returned.
149
+ #
150
+ def language(value=nil, params={})
151
+ endpoint, params, config = common_endpoint(value, params, :language)
152
+ Connection.new(endpoint, params, config).request
153
+ end
154
+
155
+ # Detects sentiment of a document in terms of
156
+ # polarity ("positive" or "negative") and
157
+ # subjectivity ("subjective" or "objective").
158
+ #
159
+ # @param [String] value (nil) URL or Text
160
+ # @param [Hash] params The sentiment endpoint options
161
+ # @option params [String] :url The URL
162
+ # @option params [String] :text Text
163
+ # @option params [String] :mode ('tweet') Analyze mode.
164
+ # Valid options are tweet, and document.
165
+ #
166
+ # @return [Hash, nil] A hash of result. See
167
+ # http://aylien.com/text-api-doc#sentiment for more information
168
+ # on the data returned.
169
+ #
170
+ def sentiment(value=nil, params={})
171
+ endpoint, params, config = common_endpoint(value, params, :sentiment)
172
+ Connection.new(endpoint, params, config).request
173
+ end
174
+
175
+ # Summarizes an article into a few key sentences.
176
+ #
177
+ # @param [String] value (nil) URL or Text
178
+ # @param [Hash] params The summarize endpoint options
179
+ # @option params [String] :mode ('default') Analyze mode.
180
+ # Valid options are default and short.
181
+ # short mode produces shorter sentences.
182
+ # @option params [String] :url The URL
183
+ # @option params [String] :title Title
184
+ # @option params [String] :text Text
185
+ # @option params [Integer] :sentences_number (5) Number of sentences
186
+ # to be returned. Only in default mode (not applicable to short mode).
187
+ # Also has precedence over sentences_percentage.
188
+ # @option params [Integer] :sentences_percentage Percentage of sentences
189
+ # to be returned. Only in default mode (not applicable to short mode).
190
+ # Possible range is 1-100.
191
+ #
192
+ # @return [Hash, nil] A hash of result. See
193
+ # http://aylien.com/text-api-doc#summarize for more information
194
+ # on the data returned.
195
+ #
196
+ def summarize(value=nil, params={})
197
+ endpoint, params, config = common_endpoint(value, params, :summarize)
198
+ Connection.new(endpoint, params, config).request
199
+ end
200
+
201
+ # Returns phrases related to the provided unigram, or bigram.
202
+ #
203
+ # @param [String] value (nil) URL or Text
204
+ # @param [Hash] params The related endpoint options
205
+ # @option params [String] :phrase Phrase
206
+ # @option params [Integer] :count (20) Number of phrases to return.
207
+ # Max is 100.
208
+ #
209
+ # @return [Hash, nil] A hash of result. See
210
+ # http://aylien.com/text-api-doc#related for more information
211
+ # on the data returned.
212
+ #
213
+ def related(value=nil, params={})
214
+ endpoint, params, config = common_endpoint(value, params, :related)
215
+ Connection.new(endpoint, params, config).request
216
+ end
217
+
218
+ private
219
+
220
+ def validate_uri(value)
221
+ value =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
222
+ end
223
+
224
+ def common_endpoint(value, params, endpoint)
225
+ if value.is_a?(Hash)
226
+ params = value
227
+ value = nil
228
+ end
229
+ if value && (!params[:url] || !params[:text] && !params[:title])
230
+ value.strip!
231
+ if validate_uri(value)
232
+ params[:url] = value
233
+ elsif endpoint == :related
234
+ params[:phrase] = value
235
+ else
236
+ params[:text] = value
237
+ end
238
+ end
239
+ config = {}
240
+ Configuration::VALID_CONFIG_KEYS.each do |key|
241
+ config[key] = send(key)
242
+ end
243
+ [endpoint, params, config]
244
+ end
245
+ end # Client
246
+ end