news-api-ruby-rails 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47506c89f91f99e55a7cc3eec7b52f462e939fea
4
+ data.tar.gz: 66c5651e3dbebdc952c6fb6f62e3f7be05164cc8
5
+ SHA512:
6
+ metadata.gz: 1b3660ded715374453eba578f65bd8dc5793c538f23a56be50d9a769caf4cd720a6c733352fa9f5fa3ea64ba8024b6a873f1fa0bd9fcbe81ef0cb1f58d245114
7
+ data.tar.gz: 011fe1485116a55f03f7b050641f3101732a91c834e7ba2b3c2591d701be15b89307e21dd5102dde76f88ea2ba272b79059ef9229b8a7ac005f67f891612dbbe
@@ -0,0 +1,24 @@
1
+ class BadRequestException < StandardError
2
+ def initialize(json)
3
+ puts json["message"]
4
+ end
5
+ end
6
+
7
+ class UnauthorizedException < StandardError
8
+ def initialize(json)
9
+ puts json["message"]
10
+ end
11
+ end
12
+
13
+
14
+ class RequestsLimitException < StandardError
15
+ def initialize(json)
16
+ puts json["message"]
17
+ end
18
+ end
19
+
20
+ class ServerException < StandardError
21
+ def initialize(json)
22
+ puts json["message"]
23
+ end
24
+ end
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ require_relative './news_api_attributes'
5
+ require_relative './source'
6
+ require_relative './exception'
7
+
8
+ class NewsApiRubyRails
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_sources(**args)
22
+ endpoint = 'sources'
23
+ request = _make_request(endpoint, **args)
24
+ sources = request['sources']
25
+ data = Array.new
26
+ sources.each do |v|
27
+ data.push(get_source(v))
28
+ end
29
+ return data
30
+ end
31
+
32
+ def get_everything(**args)
33
+ endpoint = 'everything'
34
+ return _get_everything(endpoint, **args)
35
+ end
36
+
37
+ def get_source(v)
38
+ Source.new(v["id"], v["name"], v["description"], v["url"], v["category"], v["language"], v["country"])
39
+ end
40
+
41
+
42
+ private
43
+
44
+ def _make_request(endpoint, **queries)
45
+ params = eval(queries.inspect)
46
+ url = BASE_URL + endpoint + '?'
47
+ params.each { |key, value| url += key.to_s + '=' + value.to_s + '&' }
48
+ url = url[0..-2]
49
+ uri = URI(url)
50
+ req = Net::HTTP::Get.new(uri)
51
+ req['X-Api-Key'] = @api_key
52
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) }
53
+ json = JSON.parse(res.body)
54
+ if res.code == '200'
55
+ return json
56
+ elsif res.code == '401'
57
+ raise UnauthorizedException, json
58
+ elsif res.code == '400'
59
+ raise BadRequestException, json
60
+ elsif res.code == '429'
61
+ raise RequestsLimitException, json
62
+ elsif res.code == '500'
63
+ raise ServerException, json
64
+ end
65
+ end
66
+
67
+ def _get_everything(endpoint, **args)
68
+ request = _make_request(endpoint, **args)
69
+ articles = request['articles']
70
+ data = Array.new
71
+ articles.each do |a|
72
+ data.push(
73
+ _get_news_attribute(a)
74
+ )
75
+ end
76
+ return data
77
+ end
78
+
79
+ def _get_news_attribute(a)
80
+ NewsApiAttributes.new(
81
+ a["source"], a["author"], a["title"],
82
+ a["description"], a["url"],
83
+ a["urlToImage"], a["publishedAt"]
84
+ )
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ class NewsApiAttributes
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,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-ruby-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Amah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: News API SDK gem
14
+ email: dnlamah1@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/exception.rb
20
+ - lib/news-api-ruby-rails.rb
21
+ - lib/news_api_attributes.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.6.13
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: News API SDK for Ruby and Rails
47
+ test_files: []