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..03f3291 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,35 @@ # News API SDK for Ruby -Coming soon... this is where our officially supported SDK for Ruby is going to live. -*** +## Installation -## 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. +```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 diff --git a/lib/everything.rb b/lib/everything.rb new file mode 100644 index 0000000..e2a580e --- /dev/null +++ b/lib/everything.rb @@ -0,0 +1,23 @@ +class Everything + attr_accessor :id + attr_accessor :name + 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, 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 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..4e1aa23 --- /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' || res.code == '426' + 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["content"], a["url"], + a["urlToImage"], a["publishedAt"] + ) + ) + end + return data + end +end 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..452f32d --- /dev/null +++ b/news-api.gemspec @@ -0,0 +1,15 @@ +Gem::Specification.new do |s| + s.name = 'news-api' + 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"] + 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