From 0e5c8ad6748765007a19ef212410e6d115d0d93c Mon Sep 17 00:00:00 2001 From: Fernando Maia <2966576+fernandomaia@users.noreply.github.com> Date: Wed, 2 Oct 2019 10:07:28 -0300 Subject: [PATCH] Add API Info endpoint (#69) * Add API Info endpoint * Replace 'use' method with 'adapter' * Fix PR number and spacing before it block; --- CHANGELOG.md | 1 + README.md | 19 ++++++++ lib/spacex.rb | 1 + lib/spacex/api_info.rb | 7 +++ lib/spacex/base_request.rb | 2 +- spec/fixtures/spacex/api_info/info.yml | 65 ++++++++++++++++++++++++++ spec/spacex/api_info_spec.rb | 16 +++++++ 7 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 lib/spacex/api_info.rb create mode 100644 spec/fixtures/spacex/api_info/info.yml create mode 100644 spec/spacex/api_info_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 778c559..1628929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 1.0.2 (next) * Your contribution here. +* [#69](https://github.com/rodolfobandeira/spacex/pull/69): Implement API Info endpoint [@fernandomaia](https://github.com/fernandomaia). * [#65](https://github.com/rodolfobandeira/spacex/pull/65): Add CodeClimate test coverage [@rodolfobandeira](http://github.com/rodolfobandeira). * [#64](https://github.com/rodolfobandeira/spacex/pull/64): Fix failing build on Travis-CI [@rodolfobandeira](http://github.com/rodolfobandeira). diff --git a/README.md b/README.md index 2defea1..09d9bf1 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ A Ruby library that consumes the [SpaceX API](https://github.com/r-spacex/SpaceX - [Installation](#installation) - [Usage](#usage) + - [API Info](#api-info) + - `SPACEX::ApiInfo.info` - [Capsules](#capsules) - `SPACEX::Capsules.info` - `SPACEX::Capsules.info('capsule_serial')` @@ -66,6 +68,23 @@ Then run `bundle install`. ## Usage +### API Info + +- Get information about the API: `SPACEX::ApiInfo.info` + +Here's an example of the response: + +```ruby +api_info = SPACEX::ApiInfo.info + +api_info.project_name # 'SpaceX-API' +api_info.description = # 'Open Source REST API for rocket, core, capsule, pad, and launch data, created and maintained by the developers of the r/SpaceX organization' +api_info.organization = # 'r/SpaceX' +api_info.organization_link = # 'https://github.com/r-spacex' +api_info.project_link = # 'https://github.com/r-spacex/SpaceX-API' +api_info.version = # '3.1.0' +``` + ### Capsules - Get information for all capsules: `SPACEX::Capsules.info` diff --git a/lib/spacex.rb b/lib/spacex.rb index f68e41a..76a9f65 100644 --- a/lib/spacex.rb +++ b/lib/spacex.rb @@ -3,6 +3,7 @@ require 'faraday_middleware/response_middleware' require 'hashie' +require_relative 'spacex/api_info' require_relative 'spacex/base_request' require_relative 'spacex/capsules' require_relative 'spacex/company_info' diff --git a/lib/spacex/api_info.rb b/lib/spacex/api_info.rb new file mode 100644 index 0000000..d252f5b --- /dev/null +++ b/lib/spacex/api_info.rb @@ -0,0 +1,7 @@ +module SPACEX + module ApiInfo + def self.info + SPACEX::BaseRequest.info('') + end + end +end diff --git a/lib/spacex/base_request.rb b/lib/spacex/base_request.rb index 5cba6ca..9e50ca9 100644 --- a/lib/spacex/base_request.rb +++ b/lib/spacex/base_request.rb @@ -49,7 +49,7 @@ def conn(path) ) do |c| c.use ::FaradayMiddleware::ParseJson c.use Faraday::Response::RaiseError - c.use Faraday::Adapter::NetHttp + c.adapter Faraday::Adapter::NetHttp end end end diff --git a/spec/fixtures/spacex/api_info/info.yml b/spec/fixtures/spacex/api_info/info.yml new file mode 100644 index 0000000..1766b96 --- /dev/null +++ b/spec/fixtures/spacex/api_info/info.yml @@ -0,0 +1,65 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.spacexdata.com/v3/ + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Faraday v0.16.2 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 02 Oct 2019 01:04:51 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Set-Cookie: + - __cfduid=dd7abdb6b7f9c36b6bdc298c15ec390b81569978291; expires=Thu, 01-Oct-20 + 01:04:51 GMT; path=/; domain=.spacexdata.com; HttpOnly; Secure + X-Dns-Prefetch-Control: + - 'off' + X-Frame-Options: + - SAMEORIGIN + Strict-Transport-Security: + - max-age=15552000; includeSubDomains + X-Download-Options: + - noopen + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - 1; mode=block + Vary: + - Origin + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - spacex-api-cache,spacex-api-count,spacex-api-response-time + Spacex-Api-Response-Time: + - 19ms + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 51f2c3c2ae854a4c-GRU + body: + encoding: ASCII-8BIT + string: '{"project_name":"SpaceX-API","version":"3.1.0","project_link":"https://github.com/r-spacex/SpaceX-API","docs":"https://documenter.getpostman.com/view/2025350/RWaEzAiG","organization":"r/SpaceX","organization_link":"https://github.com/r-spacex","description":"Open + Source REST API for rocket, core, capsule, pad, and launch data, created and + maintained by the developers of the r/SpaceX organization"}' + http_version: + recorded_at: Wed, 02 Oct 2019 01:04:51 GMT +recorded_with: VCR 4.0.0 diff --git a/spec/spacex/api_info_spec.rb b/spec/spacex/api_info_spec.rb new file mode 100644 index 0000000..2404cd3 --- /dev/null +++ b/spec/spacex/api_info_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe SPACEX::ApiInfo do + context '#info', vcr: { cassette_name: 'api_info/info' } do + subject do + SPACEX::ApiInfo.info + end + + it 'returns API info' do + expect(subject.project_name).to eq 'SpaceX-API' + expect(subject.organization).to eq 'r/SpaceX' + expect(subject.organization_link).to eq 'https://github.com/r-spacex' + expect(subject.description).to eq 'Open Source REST API for rocket, core, capsule, pad, and launch data, created and maintained by the developers of the r/SpaceX organization' + end + end +end