news-api 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: feb791a82c3c434862971e8da9def6db81f02e2a39e1dc5c27c9535aa16dbb82
4
+ data.tar.gz: bce596f6f655e04379149c8ea90561183411b9828710cf63da51ee187714715e
5
+ SHA512:
6
+ metadata.gz: 1e44a910f362dbc8e11f08b4a9aab11b2cf4a15489bc11901fe3ae90cc00817c897a3b68b78c259324b4d3ccee7c09ce3ef1a3efead75055001269998d9d0a18
7
+ data.tar.gz: f557cef50bd3a15d212decc78abf0565905d96dfe3a85ee7ffaed0792e9906128e697add538c7969528291f55ce4b04a971809e6a2a2c5fa8ffbf805e1b94bd6
@@ -0,0 +1,21 @@
1
+ class Everything
2
+ attr_accessor :id
3
+ attr_accessor :name
4
+ attr_accessor :author
5
+ attr_accessor :title
6
+ attr_accessor :description
7
+ attr_accessor :url
8
+ attr_accessor :urlToImage
9
+ attr_accessor :publishedAt
10
+
11
+ def initialize(source, author, title, description, url, urlToImage, publishedAt)
12
+ @id = source["id"]
13
+ @name = source["name"]
14
+ @author = author
15
+ @title = title
16
+ @description = description
17
+ @url = url
18
+ @urlToImage = urlToImage
19
+ @publishedAt = publishedAt
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ class UnauthorizedException < StandardError
2
+ def initialize(json)
3
+ puts json["message"]
4
+ end
5
+ end
6
+
7
+ class BadRequestException < StandardError
8
+ def initialize(json)
9
+ puts json["message"]
10
+ end
11
+ end
12
+
13
+ class TooManyRequestsException < StandardError
14
+ def initialize(json)
15
+ puts json["message"]
16
+ end
17
+ end
18
+
19
+ class ServerException < StandardError
20
+ def initialize(json)
21
+ puts json["message"]
22
+ end
23
+ end
@@ -0,0 +1,90 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ require_relative './everything'
5
+ require_relative './source'
6
+ require_relative './exception'
7
+
8
+ class News
9
+ VERSION = 'v2'
10
+ BASE_URL = 'https://newsapi.org/' + VERSION + '/'
11
+
12
+ def initialize(api_key)
13
+ @api_key = api_key
14
+ end
15
+
16
+ def get_top_headlines(**args)
17
+ endpoint = 'top-headlines'
18
+ return _get_everything(endpoint, **args)
19
+ end
20
+
21
+ def get_everything(**args)
22
+ endpoint = 'everything'
23
+ return _get_everything(endpoint, **args)
24
+ end
25
+
26
+ def get_sources(**args)
27
+ endpoint = 'sources'
28
+ request = _make_request(endpoint, **args)
29
+ status = request['status']
30
+ sources = request['sources']
31
+ data = Array.new
32
+ sources.each do |v|
33
+ data.push(
34
+ Source.new(
35
+ v["id"], v["name"], v["description"],
36
+ v["url"], v["category"], v["language"],
37
+ v["country"]
38
+ )
39
+ )
40
+ end
41
+ return data
42
+ end
43
+
44
+ private
45
+
46
+ def _make_request(endpoint, **queries)
47
+ params = eval(queries.inspect)
48
+ uri = URI(_make_request_string(endpoint, params))
49
+ req = Net::HTTP::Get.new(uri)
50
+ req['X-Api-Key'] = @api_key
51
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) }
52
+ json = JSON.parse(res.body)
53
+ if res.code == '200'
54
+ return json
55
+ elsif res.code == '401'
56
+ raise UnauthorizedException, json
57
+ elsif res.code == '400'
58
+ raise BadRequestException, json
59
+ elsif res.code == '429'
60
+ raise TooManyRequestsException, json
61
+ elsif res.code == '500'
62
+ raise ServerException, json
63
+ end
64
+ end
65
+
66
+ def _make_request_string(endpoint, params)
67
+ url = BASE_URL + endpoint + '?'
68
+ params.each { |key, value| url += key.to_s + '=' + value.to_s + '&' }
69
+ url = url[0..-2]
70
+ return url
71
+ end
72
+
73
+ def _get_everything(endpoint, **args)
74
+ request = _make_request(endpoint, **args)
75
+ status = request['status']
76
+ totalResults = request['totalResults']
77
+ articles = request['articles']
78
+ data = Array.new
79
+ articles.each do |a|
80
+ data.push(
81
+ Everything.new(
82
+ a["source"], a["author"], a["title"],
83
+ a["description"], a["url"],
84
+ a["urlToImage"], a["publishedAt"]
85
+ )
86
+ )
87
+ end
88
+ return data
89
+ end
90
+ end
@@ -0,0 +1,19 @@
1
+ class Source
2
+ attr_accessor :id
3
+ attr_accessor :name
4
+ attr_accessor :description
5
+ attr_accessor :url
6
+ attr_accessor :category
7
+ attr_accessor :language
8
+ attr_accessor :country
9
+
10
+ def initialize(id, name, description, url, category, language, country)
11
+ @id = id
12
+ @name = name
13
+ @description = description
14
+ @url = url
15
+ @category = category
16
+ @language = language
17
+ @country = country
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: news-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Mikhnovich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: News API SDK gem
14
+ email: oleg5966346@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/everything.rb
20
+ - lib/exception.rb
21
+ - lib/news-api.rb
22
+ - lib/source.rb
23
+ homepage: http://rubygems.org/gems/
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.7.4
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: News API SDK for Ruby
47
+ test_files: []