metonym 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +186 -2
- data/lib/metonym/lib/gnews.rb +3 -2
- data/lib/metonym/lib/news_api.rb +65 -0
- data/lib/metonym/lib/validate.rb +85 -22
- data/lib/metonym/version.rb +1 -1
- data/lib/metonym.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a27e8f0d493acf4d9b4f731fa7c8a69559b6f693d29664c399db8c285cd1b8d
|
4
|
+
data.tar.gz: cd90c6e758930f31e110afcfdf30e3193cc506a584e872a207eb9876364a348b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddfe1458407ac402138b8a4d3e97edacb9f2a6857098c37b3370d5292f3357727bbe40499d003c0bbcea07b2ff80c4e1094d686b0e595856277a3bd69026fb5c
|
7
|
+
data.tar.gz: 5d8a2ac8eb71330aab43aaae3811fbbc7f6a402cc266c5605b14fa822267089c6d5209ef5266896f61feace244357ebc66c649745124e3113c00091a9e20e704
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Metonym
|
2
2
|
|
3
|
-
A ruby gem for [GNews API](https://gnews.io)
|
3
|
+
A ruby gem for [GNews API](https://gnews.io) and [NewsApi](https://newsapi.org/)
|
4
4
|
|
5
5
|
|
6
6
|
## Installation
|
@@ -23,7 +23,9 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
To be able to send requests to the GNews API, you first must create an account to receive a token [here](https://gnews.io/register.php).
|
25
25
|
|
26
|
-
|
26
|
+
To be able to send requests to NewsApi, you first must create an account to receive a token [here](https://newsapi.org/).
|
27
|
+
|
28
|
+
## Query Gnews API
|
27
29
|
```ruby
|
28
30
|
# Establish parameters for query
|
29
31
|
args = { "q" => "Dragon Ball Super", "country" => "MX", "mindate" => (DateTime.now - 2), 'max' => 1 }
|
@@ -99,6 +101,188 @@ MydvTAug0D-dSSDfZDG9oywTEcfQXsWXtzBHULFJzLYwJDjmnxza=-c"
|
|
99
101
|
| maxdate | Optional (Date/DateTime) | Get articles that are less recent than the max date
|
100
102
|
| in | Optional | Get articles that contains q in the specified article section
|
101
103
|
|
104
|
+
## Query NewsApi
|
105
|
+
|
106
|
+
### Top Headlines
|
107
|
+
Source: [NewsApi documentation.](https://www.newsapi.org/docs/endpoints)
|
108
|
+
|
109
|
+
> Returns breaking news headlines for a country and category, or currently running on a single or multiple sources. This is perfect for use with news tickers or anywhere you want to display live up-to-date news headlines and images.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
args = { "q" => "Brexit", "country" => "DE" }
|
113
|
+
|
114
|
+
news = NewsApi::Query.new("my-secret-key")
|
115
|
+
|
116
|
+
response = news.top_headlines(args)
|
117
|
+
|
118
|
+
# =>
|
119
|
+
```
|
120
|
+
```json
|
121
|
+
{
|
122
|
+
"status":"ok",
|
123
|
+
"totalResults":1,
|
124
|
+
"articles":[
|
125
|
+
{
|
126
|
+
"source":{
|
127
|
+
"id":null,
|
128
|
+
"name":"Faz.net"
|
129
|
+
},
|
130
|
+
"author":"Klaus-Dieter Frankenberger",
|
131
|
+
"title":"Brexit-Opfer - FAZ - Frankfurter Allgemeine Zeitung",
|
132
|
+
"description":"Das Brexit-Thema wurde May wie zuvor schon Cameron zum politischen Verhängnis – und es ist eine Last, die auch die kommende Regierung nicht einfach abschütteln kann. Die EU allerdings auch nicht.",
|
133
|
+
"url":"https://www.faz.net/aktuell/brexit/theresa-may-ist-wie-david-cameron-zum-brexit-opfer-geworden-16204205.html",
|
134
|
+
"urlToImage":"https://media1.faz.net/ppmedia/aktuell/112808907/1.6204201/facebook_teaser/theresa-may-am-freitag-bei.jpg",
|
135
|
+
"publishedAt":"2019-05-24T11:07:00Z",
|
136
|
+
"content":"David Cameron führt die Liste der Brexit-Opfer an, und nun wird auch seine Nachfolgerin einen Platz darauf finden, zunächst, am 7. Juni, als Parteichefin der Konservativen, etwas später dann auch als Premierministerin. Cameron trat zurück, weil er seine Absti… [+2771 chars]"
|
137
|
+
}
|
138
|
+
]
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
#### Search Parameters for *Top Headlines*
|
143
|
+
| Parameter | Info/Description | Additional Notes
|
144
|
+
|--- |--- |---
|
145
|
+
| q | Required |Keywords or a phrase to search for.
|
146
|
+
| country | The 2-letter ISO 3166-1 code of the country |You can't mix this param with the *sources* param.
|
147
|
+
| category | Options: *business, entertainment, general, health, science, sports, technology* |You can't mix this param with the *sources* param.
|
148
|
+
| sources | A comma-seperated string of identifiers for the news sources or blogs you want headlines from |You can't mix this param with the country or category params.
|
149
|
+
| pageSize | The number of results to return per page (request). 20 is the default, 100 is the maximum. |
|
150
|
+
| page | Use this to page through the results if the total results found is greater than the page size. |
|
151
|
+
|
152
|
+
### Everything
|
153
|
+
Source: [NewsApi documentation.](https://www.newsapi.org/docs/endpoints)
|
154
|
+
|
155
|
+
|
156
|
+
> We index every recent news and blog article published by over 30,000 different sources large and small, and you can search through them with this endpoint. This endpoint is better suited for news analysis and article discovery, but can be used to retrieve articles for display too.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
args = { "q" => "Huawei", "from" => (DateTime.now - 2)}
|
160
|
+
|
161
|
+
news = NewsApi::Query.new('my-secret-key')
|
162
|
+
|
163
|
+
response = news.everything(args)
|
164
|
+
|
165
|
+
# =>
|
166
|
+
```
|
167
|
+
|
168
|
+
```json
|
169
|
+
{
|
170
|
+
"status":"ok",
|
171
|
+
"totalResults":4599,
|
172
|
+
"articles":[
|
173
|
+
{
|
174
|
+
"source":{
|
175
|
+
"id":"techcrunch",
|
176
|
+
"name":"TechCrunch"
|
177
|
+
},
|
178
|
+
"author":"Catherine Shu",
|
179
|
+
"title":"Semiconductor startup CNEX Labs alleged Huawei’s deputy chairman conspired to steal its intellectual property",
|
180
|
+
"description":"A San Jose-based semiconductor startup being sued by Huawei for stealing trade secrets has hit back in court documents, accusing the Chinese firm’s deputy chairman of conspiring to steal its intellectual property, reports the Wall Street Journal. In court fil…",
|
181
|
+
"url":"http://techcrunch.com/2019/05/22/semiconductor-startup-cnex-labs-alleged-huaweis-deputy-chairman-conspired-to-steal-its-intellectual-property/",
|
182
|
+
"urlToImage":"https://techcrunch.com/wp-content/uploads/2019/05/GettyImages-1150756100.jpg?w=600",
|
183
|
+
"publishedAt":"2019-05-23T06:01:44Z",
|
184
|
+
"content":"A San Jose-based semiconductor startup being sued by Huawei for stealing trade secrets has hit back in court documents, accusing the Chinese firms deputy chairman of conspiring to steal its intellectual property, reports the Wall Street Journal. In court fili… [+3077 chars]"
|
185
|
+
}
|
186
|
+
]
|
187
|
+
}
|
188
|
+
```
|
189
|
+
#### Search Parameters for *Everything*
|
190
|
+
| Parameter | Info/Description | Additional Notes
|
191
|
+
|--- |--- |---
|
192
|
+
| q |Required |Keywords or a phrase to search for.
|
193
|
+
| sources |A comma-seperated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. |
|
194
|
+
| domains |A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to. |
|
195
|
+
| excludeDomains|A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results. |
|
196
|
+
| from |A date and optional time for the oldest article allowed. This should be in ISO 8601 format. |
|
197
|
+
| to |A date and optional time for the newest article allowed. This should be in ISO 8601 format |
|
198
|
+
| language |The 2-letter ISO-639-1 code of the language you want to get headlines for. Possible options: *ar de en es fr he it nl no pt ru se ud zh*. |Default: All languages returned
|
199
|
+
| sortBy |Options: *relevancy, popularity, publishedAt.* |Default: publishedAt
|
200
|
+
| pageSize |The number of results to return per page. 20 is the default, 100 is the maximum. |
|
201
|
+
| page |Use this to page through the results. |
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
### Sources
|
206
|
+
Source: [NewsApi documentation.](https://www.newsapi.org/docs/endpoints)
|
207
|
+
|
208
|
+
> Returns information (including name, description, and category) about the most notable sources we index. This list could be piped directly through to your users when showing them some of the options available.
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
news = NewsApi::Query.new("my-secret-key")
|
212
|
+
response = news.sources(args)
|
213
|
+
|
214
|
+
# =>
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
```json
|
219
|
+
{
|
220
|
+
"status":"ok",
|
221
|
+
"sources":[
|
222
|
+
{
|
223
|
+
"id":"abc-news",
|
224
|
+
"name":"ABC News",
|
225
|
+
"description":"Your trusted source for breaking news, analysis, exclusive interviews, headlines, and videos at ABCNews.com.",
|
226
|
+
"url":"https://abcnews.go.com",
|
227
|
+
"category":"general",
|
228
|
+
"language":"en",
|
229
|
+
"country":"us"
|
230
|
+
},
|
231
|
+
{
|
232
|
+
"id":"abc-news-au",
|
233
|
+
"name":"ABC News (AU)",
|
234
|
+
"description":"Australia's most trusted source of local, national and world news. Comprehensive, independent, in-depth analysis, the latest business, sport, weather and more.",
|
235
|
+
"url":"http://www.abc.net.au/news",
|
236
|
+
"category":"general",
|
237
|
+
"language":"en",
|
238
|
+
"country":"au"
|
239
|
+
},
|
240
|
+
]
|
241
|
+
}
|
242
|
+
|
243
|
+
```
|
244
|
+
|
245
|
+
#### Search Parameters for *Sources*
|
246
|
+
| Parameter | Info/Description
|
247
|
+
|--- |---
|
248
|
+
| country |Find sources that display news in a specific country
|
249
|
+
| category | Options: *business, entertainment, general, health, science, sports, technology* |
|
250
|
+
| language |The 2-letter ISO-639-1 code of the language you want to get headlines for. Possible options: *ar de en es fr he it nl no pt ru se ud zh*
|
251
|
+
|
252
|
+
## Format
|
253
|
+
Additionaly, just like with the Gnews module, you may send a second parmeter to specify the format you may want to receive the response.
|
254
|
+
|
255
|
+
Supported types: **json** and **hash**
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
args = { "q" => "Brexit", "country" => "DE" }
|
259
|
+
|
260
|
+
news = NewsApi::Query.new("my-secrety-key")
|
261
|
+
|
262
|
+
response = news.top_headlines(args, 'hash')
|
263
|
+
# =>
|
264
|
+
{
|
265
|
+
"status"=>"ok",
|
266
|
+
"totalResults"=>1,
|
267
|
+
"articles"=>[
|
268
|
+
{
|
269
|
+
"source"=> {
|
270
|
+
"id"=>nil,
|
271
|
+
"name"=>"Tagesschau.de"
|
272
|
+
},
|
273
|
+
"author"=>"tagesschau.de",
|
274
|
+
"title"=>"Mays Rücktritt: \"Sie hätte Nationalheldin werden können\" - tagesschau.de", "description"=>"Premierministerin May wollte den Brexit mit aller Kraft. Doch nun hat sie überraschend emotional aufgegeben. Ihre möglichen Nachfolger bringen sich schon in Stellung. Von Imke Köhler.",
|
275
|
+
"url"=>"https://www.tagesschau.de/ausland/may-ruecktritt-103.html",
|
276
|
+
"urlToImage"=>"https://www.tagesschau.de/multimedia/bilder/may-741~_v-videowebm.jpg",
|
277
|
+
"publishedAt"=>"2019-05-24T15:52:00Z",
|
278
|
+
"content"=>"Premierministerin May wollte den Brexit mit aller Kraft. Doch nun hat sie überraschend emotional aufgegeben. Ihre möglichen Nachfolger bringen sich schon in Stellung. \r\nVon Imke Köhler, ARD-Studio London\r\n Theresa May hat selten Gefühle gezeigt in den vergang…"
|
279
|
+
]
|
280
|
+
}
|
281
|
+
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
|
102
286
|
## Contributing
|
103
287
|
|
104
288
|
Bug reports and pull requests are welcome on GitHub at https://github.com/viccarrasco/metonym. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/metonym/lib/gnews.rb
CHANGED
@@ -7,17 +7,18 @@ module Gnews
|
|
7
7
|
def initialize(key, version = nil)
|
8
8
|
@gnews_api_key = key
|
9
9
|
@gnews_version = version.nil? ? 'v2' : version
|
10
|
-
@uri = URI(
|
10
|
+
@uri = URI("https://gnews.io/api/#{@gnews_version}/")
|
11
11
|
end
|
12
12
|
|
13
13
|
def search(args, format = 'json')
|
14
14
|
begin
|
15
|
-
v =
|
15
|
+
v = Validate::GnewsValidator.new
|
16
16
|
raise "API Key is required" if v.is_key_present?(@gnews_api_key) == false
|
17
17
|
raise "Invalid parameter sequence" if v.is_query_valid?(args) == false
|
18
18
|
raise "Invalid country sent" if v.is_country_valid?(args) == false
|
19
19
|
raise "Invalid language sent" if v.is_language_valid?(args) == false
|
20
20
|
raise "Invalid date or dates sent" if v.is_date_valid?(args) == false
|
21
|
+
raise "Invalid format sent" if v.is_format_valid?(format) == false
|
21
22
|
|
22
23
|
args = prepare_arguments(args)
|
23
24
|
@uri.query = URI.encode_www_form(args)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module NewsApi
|
2
|
+
class Query
|
3
|
+
attr_accessor :news_api_key
|
4
|
+
attr_accessor :news_api_version
|
5
|
+
attr_accessor :uri
|
6
|
+
|
7
|
+
def initialize(key, version = nil)
|
8
|
+
@news_api_key = key
|
9
|
+
@news_api_version = version.nil? ? 'v2' : version
|
10
|
+
@uri = "https://newsapi.org/#{@news_api_version}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def top_headlines(args, format = 'json')
|
14
|
+
uri = URI("#{@uri}/top-headlines")
|
15
|
+
return request(args, format, uri, 'top-headlines')
|
16
|
+
end
|
17
|
+
|
18
|
+
def everything(args, format = 'json')
|
19
|
+
uri = URI("#{@uri}/everything")
|
20
|
+
return request(args, format, uri, 'everything')
|
21
|
+
end
|
22
|
+
|
23
|
+
def sources(args, format = 'json')
|
24
|
+
uri = URI("#{@uri}/sources")
|
25
|
+
return request(args, format, uri, 'sources')
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def request(args, format, uri, endpoint)
|
30
|
+
begin
|
31
|
+
v = Validate::NewsApiValidator.new
|
32
|
+
raise "API Key is required" if v.is_key_present?(@news_api_key) == false
|
33
|
+
raise "Invalid parameter sequence" if v.is_query_valid?(args, endpoint) == false
|
34
|
+
raise "Invalid country sent" if v.is_country_valid?(args) == false
|
35
|
+
raise "Invalid language sent" if v.is_language_valid?(args) == false
|
36
|
+
raise "Invalid date or dates sent" if v.is_date_valid?(args) == false
|
37
|
+
raise "Invalid format sent" if v.is_format_valid?(format) == false
|
38
|
+
|
39
|
+
args = prepare_arguments(args)
|
40
|
+
|
41
|
+
uri.query = URI.encode_www_form(args)
|
42
|
+
response = Net::HTTP.get_response(uri)
|
43
|
+
rescue Exception => e
|
44
|
+
response = {"errors" => e.to_s}
|
45
|
+
ensure
|
46
|
+
if response.is_a?(Hash) && response.has_key?('errors')
|
47
|
+
return response
|
48
|
+
else
|
49
|
+
if format.nil? || format == 'json'
|
50
|
+
return response.body
|
51
|
+
elsif format == 'hash'
|
52
|
+
return JSON.parse(response.body)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def prepare_arguments(args)
|
59
|
+
args['apiKey'] = @news_api_key
|
60
|
+
args['from'] = args.has_key?('from') ? args['from'].strftime("%FT%T%:z") : nil
|
61
|
+
args['to'] = args.has_key?('to') ? args['to'].strftime("%FT%T%:z") : nil
|
62
|
+
args
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/metonym/lib/validate.rb
CHANGED
@@ -1,14 +1,9 @@
|
|
1
|
-
module
|
2
|
-
class
|
3
|
-
|
1
|
+
module Validate
|
2
|
+
class Validator
|
3
|
+
def is_key_present?(args)
|
4
4
|
return !args.nil?
|
5
5
|
end
|
6
6
|
|
7
|
-
def is_query_valid?(args)
|
8
|
-
template = %w(q max lang country mindate maxdate in)
|
9
|
-
return (args.keys - template).empty?
|
10
|
-
end
|
11
|
-
|
12
7
|
def is_language_valid?(args)
|
13
8
|
if args['lang'].nil?
|
14
9
|
true
|
@@ -25,24 +20,43 @@ module Gnews
|
|
25
20
|
end
|
26
21
|
end
|
27
22
|
|
23
|
+
def is_format_valid?(args)
|
24
|
+
template = %w(json hash)
|
25
|
+
|
26
|
+
if args.nil?
|
27
|
+
true
|
28
|
+
else
|
29
|
+
template.include?(args.downcase)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class GnewsValidator < Validator
|
35
|
+
COUNTRIES = [
|
36
|
+
'af','al','dz','as','ad','ao','ai','aq','ag','ar','am','aw','au','at','az','bs','bh','bd','bb','by',
|
37
|
+
'be','bz','bj','bm','bt','bo','ba','bw','bv','br','io','bn','bg','bf','bi','kh','cm','ca','cv','ky',
|
38
|
+
'cf','td','cl','cn','cx','cc','co','km','cg','cd','ck','cr','ci','hr','cu','cy','cz','dk','dj','dm',
|
39
|
+
'do','ec','eg','sv','gq','er','ee','et','fk','fo','fj','fi','fr','gf','pf','tf','ga','gm','ge','de',
|
40
|
+
'gh','gi','gr','gl','gd','gp','gu','gt','gn','gw','gy','ht','hm','va','hn','hk','hu','is','in','id',
|
41
|
+
'ir','iq','ie','il','it','jm','jp','jo','kz','ke','ki','kp','kr','kw','kg','la','lv','lb','ls','lr',
|
42
|
+
'ly','li','lt','lu','mo','mk','mg','mw','my','mv','ml','mt','mh','mq','mr','mu','yt','mx','fm','md',
|
43
|
+
'mc','mn','ms','ma','mz','mm','na','nr','np','nl','an','nc','nz','ni','ne','ng','nu','nf','mp','no',
|
44
|
+
'om','pk','pw','ps','pa','pg','py','pe','ph','pn','pl','pt','pr','qa','re','ro','ru','rw','sh','kn',
|
45
|
+
'lc','pm','vc','ws','sm','st','sa','sn','cs','sc','sl','sg','sk','si','sb','so','za','gs','sp','lk',
|
46
|
+
'sd','sr','sj','sz','se','ch','sy','tw','tj','tz','th','tl','tg','tk','to','tt','tn','tr','tm','tc',
|
47
|
+
'tv','ug','ua','ae','uk','us','um','uy','uz','vu','ve','vn','vg','vi','wf','eh','ye','zm','zw'
|
48
|
+
]
|
49
|
+
|
50
|
+
def is_query_valid?(args)
|
51
|
+
template = %w(q max lang country mindate maxdate in)
|
52
|
+
return (args.keys - template).empty?
|
53
|
+
end
|
54
|
+
|
28
55
|
def is_country_valid?(args)
|
29
56
|
if args['country'].nil?
|
30
57
|
true
|
31
58
|
else
|
32
|
-
return [
|
33
|
-
'af','al','dz','as','ad','ao','ai','aq','ag','ar','am','aw','au','at','az','bs','bh','bd','bb','by',
|
34
|
-
'be','bz','bj','bm','bt','bo','ba','bw','bv','br','io','bn','bg','bf','bi','kh','cm','ca','cv','ky',
|
35
|
-
'cf','td','cl','cn','cx','cc','co','km','cg','cd','ck','cr','ci','hr','cu','cy','cz','dk','dj','dm',
|
36
|
-
'do','ec','eg','sv','gq','er','ee','et','fk','fo','fj','fi','fr','gf','pf','tf','ga','gm','ge','de',
|
37
|
-
'gh','gi','gr','gl','gd','gp','gu','gt','gn','gw','gy','ht','hm','va','hn','hk','hu','is','in','id',
|
38
|
-
'ir','iq','ie','il','it','jm','jp','jo','kz','ke','ki','kp','kr','kw','kg','la','lv','lb','ls','lr',
|
39
|
-
'ly','li','lt','lu','mo','mk','mg','mw','my','mv','ml','mt','mh','mq','mr','mu','yt','mx','fm','md',
|
40
|
-
'mc','mn','ms','ma','mz','mm','na','nr','np','nl','an','nc','nz','ni','ne','ng','nu','nf','mp','no',
|
41
|
-
'om','pk','pw','ps','pa','pg','py','pe','ph','pn','pl','pt','pr','qa','re','ro','ru','rw','sh','kn',
|
42
|
-
'lc','pm','vc','ws','sm','st','sa','sn','cs','sc','sl','sg','sk','si','sb','so','za','gs','sp','lk',
|
43
|
-
'sd','sr','sj','sz','se','ch','sy','tw','tj','tz','th','tl','tg','tk','to','tt','tn','tr','tm','tc',
|
44
|
-
'tv','ug','ua','ae','uk','us','um','uy','uz','vu','ve','vn','vg','vi','wf','eh','ye','zm','zw'
|
45
|
-
].include?(args['country'].downcase)
|
59
|
+
return COUNTRIES.include?(args['country'].downcase)
|
46
60
|
end
|
47
61
|
end
|
48
62
|
|
@@ -58,4 +72,53 @@ module Gnews
|
|
58
72
|
end
|
59
73
|
end
|
60
74
|
end
|
75
|
+
|
76
|
+
class NewsApiValidator < Validator
|
77
|
+
COUNTRIES = %w(ar au at be br bg ca cn co cu cz eg fr de gr hk hu in id ie il it jp lv lt my mx ma nl nz ng no ph pl pt ro ru sa rs sg sk si za kr se ch tw th tr ae ua gb us ve)
|
78
|
+
CATEGORIES = %w(business entertainment general health science sports technology)
|
79
|
+
|
80
|
+
def is_query_valid?(args, endpoint = 'top-headlines')
|
81
|
+
case endpoint
|
82
|
+
when 'top-headlines'
|
83
|
+
template = %w(q country category sources page_size page)
|
84
|
+
when 'everything'
|
85
|
+
template = %w(q sources domains excludeDomains from to language sortBy pageSize page)
|
86
|
+
when 'sources'
|
87
|
+
template = %w(category language country)
|
88
|
+
end
|
89
|
+
|
90
|
+
if (endpoint == 'top-headlines' && (args.include?('sources') && (args.include?('country') || args.include?('category'))))
|
91
|
+
false
|
92
|
+
elsif (endpoint == "sources") && (args.has_key?("category")) && (CATEGORIES.include?(args["category"]) == false)
|
93
|
+
false
|
94
|
+
else
|
95
|
+
(args.keys - template).empty?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def is_country_valid?(args)
|
100
|
+
if args['country'].nil?
|
101
|
+
true
|
102
|
+
else
|
103
|
+
return COUNTRIES.include?(args['country'].downcase)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def is_category_valid?(args)
|
108
|
+
template = %w(business entertainment general health science sports technology)
|
109
|
+
return template.include?(args['country'])
|
110
|
+
end
|
111
|
+
|
112
|
+
def is_date_valid?(args)
|
113
|
+
if (args['from'].nil? && args['to'].nil?)
|
114
|
+
true
|
115
|
+
elsif (args['from'] && args['to'])
|
116
|
+
return (args['from'].respond_to?(:strftime) && args['to'].respond_to?(:strftime))
|
117
|
+
elsif (args['from'] && args['to'].nil?)
|
118
|
+
return args['from'].respond_to?(:strftime)
|
119
|
+
elsif (args['from'].nil? && args['to'])
|
120
|
+
return args['from'].respond_to?(:strftime)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
61
124
|
end
|
data/lib/metonym/version.rb
CHANGED
data/lib/metonym.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metonym
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vic Carrasco
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- bin/setup
|
88
88
|
- lib/metonym.rb
|
89
89
|
- lib/metonym/lib/gnews.rb
|
90
|
+
- lib/metonym/lib/news_api.rb
|
90
91
|
- lib/metonym/lib/validate.rb
|
91
92
|
- lib/metonym/version.rb
|
92
93
|
- metonym.gemspec
|