news_aggregator 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 75ae6c5548c27b7085a2da7d4d7a03a947f5d7672cab59835d67c347f06e778a
4
- data.tar.gz: e8eabadd12de56319dffb50e58371566d9ceae1797d68c8119cc983920db7922
3
+ metadata.gz: c43118ae179764003c7b4c034bb1d028f3a79597c3ced860968f02694dd35b24
4
+ data.tar.gz: eba9c891cba9043606f8f7e1314f7dd9f54ae68a79a465e4d14bb6278582b6ac
5
5
  SHA512:
6
- metadata.gz: 228e68c92becb26d8b04cd51c9f962d024117fdebbf9999f3974a8b6e486a5405eead7060dabea5b4a5c09562257c1b45a721b5efcca2a6eb4490f6c30a7dba1
7
- data.tar.gz: 5ef7536c062d1cd0f035c60f534267746395472b9e8287a9da57b155c83800a5955d8083540b04a489fb782824cd131ddaccda6f17a9d30eeadf199413a43c51
6
+ metadata.gz: fe6c2127517f0df1f1a96f361b7cbdc6085c112ef655ebf688f4297e93e1d5015be477be4937402b7fe399879c71db4b83a9621ff86e1d5c05224befae214ba3
7
+ data.tar.gz: ae7da07934324c97f90fa4e7c882af988396914024421ecba016593d1de67f57c1642a9a4708db4347ff7ea19c72a6e119072ebd16e9ee1ca2ae792154f4d15b
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/news_aggregator.svg)](https://badge.fury.io/rb/news_aggregator)
1
2
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
2
3
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide)
3
4
  [![Ruby on Rails CI](https://github.com/enowmbi/news_aggregator_ruby/actions/workflows/ruby.yml/badge.svg)](https://github.com/enowmbi/news_aggregator_ruby/actions/workflows/ruby.yml)
@@ -2,10 +2,15 @@
2
2
 
3
3
  require "faraday"
4
4
  require "faraday_middleware"
5
+
6
+ # NewsAggregator
5
7
  module NewsAggregator
6
8
  # Locate articles and breaking news headlines from news sources and blogs across the web with newsapi.org
7
9
  class News
8
10
  BASE_URL = "https://newsapi.org/v2"
11
+
12
+ END_POINTS = %w[top-headlines everything sources].freeze
13
+
9
14
  attr_reader :api_key, :adapter
10
15
 
11
16
  def initialize(api_key:, adapter: Faraday.default_adapter)
@@ -22,69 +27,31 @@ module NewsAggregator
22
27
  end
23
28
  end
24
29
 
25
- def top_headlines(**args)
26
- endpoint = "top-headlines"
27
- _get_everything(endpoint, **args)
28
- end
30
+ def make_request(endpoint, **query_params)
31
+ response = connection.get(endpoint, query_params, { authorization: "bearer #{api_key}" })
29
32
 
30
- def everything(**args)
31
- endpoint = "everything"
32
- _get_everything(endpoint, **args)
33
- end
33
+ return response.body if response.status == 200
34
34
 
35
- def sources(**args)
36
- endpoint = "sources"
37
- request = _make_request(endpoint, **args)
38
- # status = request["status"]
39
- sources = request["sources"]
40
- data = []
41
- sources.each do |v|
42
- data.push(
43
- Source.new(
44
- v["id"], v["name"], v["description"],
45
- v["url"], v["category"], v["language"],
46
- v["country"]
47
- )
48
- )
49
- end
50
- data
35
+ raise_exception(response)
51
36
  end
52
37
 
53
- private
54
-
55
- def _make_request(endpoint, **query_params)
56
- response = connection.get(endpoint, query_params, { authorization: "bearer #{api_key}" })
57
- case response.status
58
- when "200"
59
- return response.body
60
- when "401"
61
- raise UnauthorizedException, response.body
62
- when "400"
63
- raise BadRequestException, response.body
64
- when "429" || "426"
65
- raise TooManyRequestsException, response.body
66
- when "500"
67
- raise ServerException, response.body
38
+ END_POINTS.each do |end_point|
39
+ define_method(end_point.tr("/-", "_").to_sym) do |**args|
40
+ make_request(end_point, **args)
68
41
  end
69
- response.body
70
42
  end
43
+ end
71
44
 
72
- def _get_everything(endpoint, **args)
73
- request = _make_request(endpoint, **args)
74
- # status = request['status']
75
- # totalResults = request['totalResults']
76
- articles = request["articles"]
77
- data = []
78
- articles.each do |a|
79
- data.push(
80
- Everything.new(
81
- a["source"], a["author"], a["title"],
82
- a["description"], a["content"], a["url"],
83
- a["urlToImage"], a["publishedAt"]
84
- )
85
- )
86
- end
87
- data
45
+ private
46
+
47
+ def raise_exception(response)
48
+ case response.status
49
+ when "400" then raise BadRequestException, response.body
50
+ when "401" then raise UnauthorizedException, response.body
51
+ when "402" then raise PaymentRequiredException, response.body
52
+ when "403" then raise ForbiddenException, response.body
53
+ when "429" then raise TooManyRequestsException, response.body
54
+ when "500" then raise ServerException, response.body
88
55
  end
89
56
  end
90
57
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NewsAggregator
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -7,6 +7,4 @@ module NewsAggregator
7
7
  autoload(:BadRequestException, "news_aggregator/exceptions")
8
8
  autoload(:TooManyRequestsException, "news_aggregator/exceptions")
9
9
  autoload(:ServerException, "news_aggregator/exceptions")
10
- autoload(:Source, "news_aggregator/source")
11
- autoload(:Everything, "news_aggregator/everything")
12
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: news_aggregator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Enow Mbi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-09 00:00:00.000000000 Z
11
+ date: 2022-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -64,17 +64,13 @@ files:
64
64
  - CHANGELOG.md
65
65
  - CODE_OF_CONDUCT.md
66
66
  - Gemfile
67
- - Gemfile.lock
68
67
  - LICENSE.txt
69
68
  - README.md
70
69
  - Rakefile
71
70
  - lib/news_aggregator.rb
72
- - lib/news_aggregator/everything.rb
73
71
  - lib/news_aggregator/exceptions.rb
74
72
  - lib/news_aggregator/news.rb
75
- - lib/news_aggregator/source.rb
76
73
  - lib/news_aggregator/version.rb
77
- - news_aggregator-0.1.0.gem
78
74
  - sig/news_aggregator.rbs
79
75
  homepage: https://github.com/enowmbi/news_aggregator_ruby
80
76
  licenses:
data/Gemfile.lock DELETED
@@ -1,92 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- news_aggregator (0.1.0)
5
- faraday (~> 1.10.2)
6
- faraday_middleware (~> 1.2)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- ast (2.4.2)
12
- coderay (1.1.3)
13
- diff-lcs (1.5.0)
14
- faraday (1.10.2)
15
- faraday-em_http (~> 1.0)
16
- faraday-em_synchrony (~> 1.0)
17
- faraday-excon (~> 1.1)
18
- faraday-httpclient (~> 1.0)
19
- faraday-multipart (~> 1.0)
20
- faraday-net_http (~> 1.0)
21
- faraday-net_http_persistent (~> 1.0)
22
- faraday-patron (~> 1.0)
23
- faraday-rack (~> 1.0)
24
- faraday-retry (~> 1.0)
25
- ruby2_keywords (>= 0.0.4)
26
- faraday-em_http (1.0.0)
27
- faraday-em_synchrony (1.0.0)
28
- faraday-excon (1.1.0)
29
- faraday-httpclient (1.0.1)
30
- faraday-multipart (1.0.4)
31
- multipart-post (~> 2)
32
- faraday-net_http (1.0.1)
33
- faraday-net_http_persistent (1.2.0)
34
- faraday-patron (1.0.0)
35
- faraday-rack (1.0.0)
36
- faraday-retry (1.0.3)
37
- faraday_middleware (1.2.0)
38
- faraday (~> 1.0)
39
- json (2.6.2)
40
- method_source (1.0.0)
41
- multipart-post (2.2.3)
42
- parallel (1.22.1)
43
- parser (3.1.2.1)
44
- ast (~> 2.4.1)
45
- pry (0.14.1)
46
- coderay (~> 1.1)
47
- method_source (~> 1.0)
48
- rainbow (3.1.1)
49
- rake (13.0.6)
50
- regexp_parser (2.5.0)
51
- rexml (3.2.5)
52
- rspec (3.11.0)
53
- rspec-core (~> 3.11.0)
54
- rspec-expectations (~> 3.11.0)
55
- rspec-mocks (~> 3.11.0)
56
- rspec-core (3.11.0)
57
- rspec-support (~> 3.11.0)
58
- rspec-expectations (3.11.1)
59
- diff-lcs (>= 1.2.0, < 2.0)
60
- rspec-support (~> 3.11.0)
61
- rspec-mocks (3.11.1)
62
- diff-lcs (>= 1.2.0, < 2.0)
63
- rspec-support (~> 3.11.0)
64
- rspec-support (3.11.1)
65
- rubocop (1.36.0)
66
- json (~> 2.3)
67
- parallel (~> 1.10)
68
- parser (>= 3.1.2.1)
69
- rainbow (>= 2.2.2, < 4.0)
70
- regexp_parser (>= 1.8, < 3.0)
71
- rexml (>= 3.2.5, < 4.0)
72
- rubocop-ast (>= 1.20.1, < 2.0)
73
- ruby-progressbar (~> 1.7)
74
- unicode-display_width (>= 1.4.0, < 3.0)
75
- rubocop-ast (1.21.0)
76
- parser (>= 3.1.1.0)
77
- ruby-progressbar (1.11.0)
78
- ruby2_keywords (0.0.5)
79
- unicode-display_width (2.3.0)
80
-
81
- PLATFORMS
82
- x86_64-darwin-19
83
-
84
- DEPENDENCIES
85
- news_aggregator!
86
- pry
87
- rake (~> 13.0)
88
- rspec (~> 3.0)
89
- rubocop (~> 1.21)
90
-
91
- BUNDLED WITH
92
- 2.3.14
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NewsAggregator
4
- # /v2/everything
5
- class Everything
6
- attr_accessor :id, :name, :author, :title, :description, :url, :url_to_image, :published_at, :content
7
-
8
- def initialize(source, author, title, description, url, url_to_image, published_at, content)
9
- @id = source["id"]
10
- @name = source["name"]
11
- @author = author
12
- @title = title
13
- @description = description
14
- @url = url
15
- @url_to_image = url_to_image
16
- @published_at = published_at
17
- @content = content
18
- end
19
- end
20
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NewsAggregator
4
- # source class
5
- class Source
6
- attr_accessor :id, :name, :description, :url, :category, :language, :country
7
-
8
- def initialize(id, name, description, url, category, language, country)
9
- @id = id
10
- @name = name
11
- @description = description
12
- @url = url
13
- @category = category
14
- @language = language
15
- @country = country
16
- end
17
- end
18
- end
Binary file