From d0351339544d6b187fbd7ec6e36147a6d8c16ba6 Mon Sep 17 00:00:00 2001 From: Oleg Date: Sat, 6 Jan 2018 15:53:41 +0300 Subject: [PATCH 1/6] api wrapper initial commit --- .gitignore | 2 ++ README.md | 19 ++++++++-- lib/everything.rb | 21 +++++++++++ lib/exception.rb | 23 ++++++++++++ lib/news-api.rb | 90 +++++++++++++++++++++++++++++++++++++++++++++++ lib/source.rb | 19 ++++++++++ news-api.gemspec | 15 ++++++++ 7 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 lib/everything.rb create mode 100644 lib/exception.rb create mode 100644 lib/news-api.rb create mode 100644 lib/source.rb create mode 100644 news-api.gemspec diff --git a/.gitignore b/.gitignore index 5e1422c..25de002 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,5 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc + +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index bc1b103..0d60280 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,20 @@ Coming soon... this is where our officially supported SDK for Ruby is going to l *** -## Developers... we need you! -We need some help fleshing out this repo. If you're a Ruby dev with experience building gems and web API wrappers, we're offering a reward of $250 to help us get started. For more info please email support@newsapi.org, or dive right in and send us a pull request. +News API is a simple HTTP REST API for searching and retrieving live articles from all over the web. It can help you answer questions like: +What top stories is the NY Times running right now? +What new articles were published about the next iPhone today? +Has my company or product been mentioned or reviewed by any blogs recently? +How many social shares has an article received? (Coming soon!) +You can search for articles with any combination of the following criteria: +Keyword or phrase. Eg: find all articles containing the word 'Microsoft'. +Date published. Eg: find all articles published yesterday. +Source name. Eg: find all articles by 'TechCrunch'. +Source domain name. Eg: find all articles published on nytimes.com. +Language. Eg: find all articles written in English. +You can sort the results in the following orders: +Date published +Relevancy to search keyword +Popularity of source +Social shares (Coming soon!) +You need an API key to use the API - this is a unique key that identifies your requests. They're free for development, open-source, and non-commercial use. You can get one here: https://newsapi.org. \ No newline at end of file diff --git a/lib/everything.rb b/lib/everything.rb new file mode 100644 index 0000000..5959d21 --- /dev/null +++ b/lib/everything.rb @@ -0,0 +1,21 @@ +class Everything + attr_accessor :id + attr_accessor :name + attr_accessor :author + attr_accessor :title + attr_accessor :description + attr_accessor :url + attr_accessor :urlToImage + attr_accessor :publishedAt + + def initialize(source, author, title, description, url, urlToImage, publishedAt) + @id = source["id"] + @name = source["name"] + @author = author + @title = title + @description = description + @url = url + @urlToImage = urlToImage + @publishedAt = publishedAt + end +end \ No newline at end of file diff --git a/lib/exception.rb b/lib/exception.rb new file mode 100644 index 0000000..9d247cc --- /dev/null +++ b/lib/exception.rb @@ -0,0 +1,23 @@ +class UnauthorizedException < StandardError + def initialize(json) + puts json["message"] + end +end + +class BadRequestException < StandardError + def initialize(json) + puts json["message"] + end +end + +class TooManyRequestsException < StandardError + def initialize(json) + puts json["message"] + end +end + +class ServerException < StandardError + def initialize(json) + puts json["message"] + end +end \ No newline at end of file diff --git a/lib/news-api.rb b/lib/news-api.rb new file mode 100644 index 0000000..ad80dad --- /dev/null +++ b/lib/news-api.rb @@ -0,0 +1,90 @@ +require 'net/http' +require 'json' + +require_relative './everything' +require_relative './source' +require_relative './exception' + +class News + VERSION = 'v2' + BASE_URL = 'https://newsapi.org/' + VERSION + '/' + + def initialize(api_key) + @api_key = api_key + end + + def get_top_headlines(**args) + endpoint = 'top-headlines' + return _get_everything(endpoint, **args) + end + + def get_everything(**args) + endpoint = 'everything' + return _get_everything(endpoint, **args) + end + + def get_sources(**args) + endpoint = 'sources' + request = _make_request(endpoint, **args) + status = request['status'] + sources = request['sources'] + data = Array.new + sources.each do |v| + data.push( + Source.new( + v["id"], v["name"], v["description"], + v["url"], v["category"], v["language"], + v["country"] + ) + ) + end + return data + end + + private + + def _make_request(endpoint, **queries) + params = eval(queries.inspect) + uri = URI(_make_request_string(endpoint, params)) + req = Net::HTTP::Get.new(uri) + req['X-Api-Key'] = @api_key + res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) } + json = JSON.parse(res.body) + if res.code == '200' + return json + elsif res.code == '401' + raise UnauthorizedException, json + elsif res.code == '400' + raise BadRequestException, json + elsif res.code == '429' + raise TooManyRequestsException, json + elsif res.code == '500' + raise ServerException, json + end + end + + def _make_request_string(endpoint, params) + url = BASE_URL + endpoint + '?' + params.each { |key, value| url += key.to_s + '=' + value.to_s + '&' } + url = url[0..-2] + return url + end + + def _get_everything(endpoint, **args) + request = _make_request(endpoint, **args) + status = request['status'] + totalResults = request['totalResults'] + articles = request['articles'] + data = Array.new + articles.each do |a| + data.push( + Everything.new( + a["source"], a["author"], a["title"], + a["description"], a["url"], + a["urlToImage"], a["publishedAt"] + ) + ) + end + return data + end +end \ No newline at end of file diff --git a/lib/source.rb b/lib/source.rb new file mode 100644 index 0000000..913f2fa --- /dev/null +++ b/lib/source.rb @@ -0,0 +1,19 @@ +class Source + attr_accessor :id + attr_accessor :name + attr_accessor :description + attr_accessor :url + attr_accessor :category + attr_accessor :language + attr_accessor :country + + def initialize(id, name, description, url, category, language, country) + @id = id + @name = name + @description = description + @url = url + @category = category + @language = language + @country = country + end +end \ No newline at end of file diff --git a/news-api.gemspec b/news-api.gemspec new file mode 100644 index 0000000..a6e234b --- /dev/null +++ b/news-api.gemspec @@ -0,0 +1,15 @@ +Gem::Specification.new do |s| + s.name = 'news-api' + s.version = '0.0.0' + s.date = '2018-02-01' + s.summary = "News API SDK for Ruby" + s.description = "News API SDK gem" + s.authors = ["Oleg Mikhnovich"] + s.email = 'oleg5966346@gmail.com' + s.files = ["lib/news-api.rb", + "lib/source.rb", + "lib/everything.rb", + "lib/exception.rb"] + s.homepage = 'http://rubygems.org/gems/' + s.license = 'MIT' + end \ No newline at end of file From c944ff346ae725c6ec4b46b2234ce3229dec38aa Mon Sep 17 00:00:00 2001 From: Oleg Date: Sat, 6 Jan 2018 16:03:59 +0300 Subject: [PATCH 2/6] readme change --- README.md | 55 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0d60280..03f3291 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,35 @@ # News API SDK for Ruby -Coming soon... this is where our officially supported SDK for Ruby is going to live. - -*** - -News API is a simple HTTP REST API for searching and retrieving live articles from all over the web. It can help you answer questions like: -What top stories is the NY Times running right now? -What new articles were published about the next iPhone today? -Has my company or product been mentioned or reviewed by any blogs recently? -How many social shares has an article received? (Coming soon!) -You can search for articles with any combination of the following criteria: -Keyword or phrase. Eg: find all articles containing the word 'Microsoft'. -Date published. Eg: find all articles published yesterday. -Source name. Eg: find all articles by 'TechCrunch'. -Source domain name. Eg: find all articles published on nytimes.com. -Language. Eg: find all articles written in English. -You can sort the results in the following orders: -Date published -Relevancy to search keyword -Popularity of source -Social shares (Coming soon!) -You need an API key to use the API - this is a unique key that identifies your requests. They're free for development, open-source, and non-commercial use. You can get one here: https://newsapi.org. \ No newline at end of file + +## Installation + +```shell +gem install news-api +``` + +## Usage + +### Initilisation + +```ruby +require 'news-api' + +n = News.new("api_key") +``` + +### Sources + +```ruby +n.get_sources(country: 'us', language: 'en') +``` + +### Everything + +```ruby +n.get_everything(q: "apple", from: "2018-01-05&to=2018-01-05", sortBy: "popularity") +``` + +### Top Headlines + +```ruby +n.get_top_headlines(sources: "bbc-news") +``` \ No newline at end of file From 88a2f79167ac8096c8fba3fe35bca4100e8ae72a Mon Sep 17 00:00:00 2001 From: Surender Date: Tue, 25 Jun 2019 10:14:01 +0530 Subject: [PATCH 3/6] Added exception for Developer account with status code "426" --- lib/news-api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/news-api.rb b/lib/news-api.rb index ad80dad..b3f9e13 100644 --- a/lib/news-api.rb +++ b/lib/news-api.rb @@ -56,7 +56,7 @@ def _make_request(endpoint, **queries) raise UnauthorizedException, json elsif res.code == '400' raise BadRequestException, json - elsif res.code == '429' + elsif res.code == '429' || res.code == '426' raise TooManyRequestsException, json elsif res.code == '500' raise ServerException, json From 5b550fd1ffa1e519ad3377799eb053142faa1b29 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 25 Jun 2019 15:30:02 +0300 Subject: [PATCH 4/6] Update news-api.gemspec --- news-api.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/news-api.gemspec b/news-api.gemspec index a6e234b..2c92025 100644 --- a/news-api.gemspec +++ b/news-api.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'news-api' - s.version = '0.0.0' - s.date = '2018-02-01' + s.version = '0.1.0' + s.date = '2019-06-25' s.summary = "News API SDK for Ruby" s.description = "News API SDK gem" s.authors = ["Oleg Mikhnovich"] @@ -12,4 +12,4 @@ Gem::Specification.new do |s| "lib/exception.rb"] s.homepage = 'http://rubygems.org/gems/' s.license = 'MIT' - end \ No newline at end of file + end From 9f7b702a1f9f3613dd83f616ed1d1f07257c23b8 Mon Sep 17 00:00:00 2001 From: Guillaume Illien Date: Fri, 23 Aug 2019 09:29:17 +0200 Subject: [PATCH 5/6] Added missing content field on everything class --- lib/everything.rb | 6 ++++-- lib/news-api.rb | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/everything.rb b/lib/everything.rb index 5959d21..e2a580e 100644 --- a/lib/everything.rb +++ b/lib/everything.rb @@ -4,18 +4,20 @@ class Everything attr_accessor :author attr_accessor :title attr_accessor :description + attr_accessor :content attr_accessor :url attr_accessor :urlToImage attr_accessor :publishedAt - def initialize(source, author, title, description, url, urlToImage, publishedAt) + def initialize(source, author, title, description, content, url, urlToImage, publishedAt) @id = source["id"] @name = source["name"] @author = author @title = title @description = description + @content = content @url = url @urlToImage = urlToImage @publishedAt = publishedAt end -end \ No newline at end of file +end diff --git a/lib/news-api.rb b/lib/news-api.rb index b3f9e13..4e1aa23 100644 --- a/lib/news-api.rb +++ b/lib/news-api.rb @@ -10,7 +10,7 @@ class News BASE_URL = 'https://newsapi.org/' + VERSION + '/' def initialize(api_key) - @api_key = api_key + @api_key = api_key end def get_top_headlines(**args) @@ -80,11 +80,11 @@ def _get_everything(endpoint, **args) data.push( Everything.new( a["source"], a["author"], a["title"], - a["description"], a["url"], + a["description"], a["content"], a["url"], a["urlToImage"], a["publishedAt"] ) ) end return data end -end \ No newline at end of file +end From dd7ea9d1c5a26735272a20f46a42909193cd2695 Mon Sep 17 00:00:00 2001 From: Oleg Date: Fri, 23 Aug 2019 07:52:27 +0000 Subject: [PATCH 6/6] Update news-api.gemspec --- news-api.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/news-api.gemspec b/news-api.gemspec index 2c92025..452f32d 100644 --- a/news-api.gemspec +++ b/news-api.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'news-api' - s.version = '0.1.0' - s.date = '2019-06-25' + s.version = '0.2.0' + s.date = '2019-08-23' s.summary = "News API SDK for Ruby" s.description = "News API SDK gem" s.authors = ["Oleg Mikhnovich"]