From 42d30f5fe4df48bdaed9338d50768fc32dc6540a Mon Sep 17 00:00:00 2001 From: Chaitanya Kale Date: Tue, 1 Oct 2019 21:39:07 -0400 Subject: [PATCH] Implement "LaunchPads" endpoint ***Why?*** - Implement Launch Pads endpoint as requested by [#66](https://github.com/rodolfobandeira/spacex/issues/68) ***Description*** - Uses reference specified in the issue above https://docs.spacexdata.com/?version=latest#e232e64a-58a2-4bc0-af42-eb20499425cc - Closes [#66](https://github.com/rodolfobandeira/spacex/issues/68) ***Side effects/ Notes*** - Got an error when running this locally due to Faraday enforcing `.adapter` over `.use`, so had to make that change as well --- CHANGELOG.md | 1 + README.md | 31 ++++++ lib/spacex.rb | 1 + lib/spacex/base_request.rb | 2 +- lib/spacex/launch_pads.rb | 11 ++ spec/fixtures/spacex/launch_pads/info.yml | 104 ++++++++++++++++++ .../spacex/launch_pads/info/vafb_slc_4e.yml | 71 ++++++++++++ spec/spacex/launch_pads_spec.rb | 77 +++++++++++++ 8 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 lib/spacex/launch_pads.rb create mode 100644 spec/fixtures/spacex/launch_pads/info.yml create mode 100644 spec/fixtures/spacex/launch_pads/info/vafb_slc_4e.yml create mode 100644 spec/spacex/launch_pads_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 778c559..c4124ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 1.0.2 (next) * Your contribution here. +* [#70](https://github.com/rodolfobandeira/spacex/pull/70): Implement Landing Pads endpoint [@invacuo](http://github.com/invacuo). * [#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..c8b9aea 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ A Ruby library that consumes the [SpaceX API](https://github.com/r-spacex/SpaceX - [History](#history) - `SPACEX::History.info` - `SPACEX::History.info(4)` + - [LaunchPads](#launch_pads) + - `SPACEX::LaunchPads.info` + - `SPACEX::LaunchPads.info('vafb_slc_4e')` - [Launches](#launches) - `SPACEX::Launches.all` - `SPACEX::Launches.info` @@ -211,6 +214,34 @@ first_event.links['article'] # http://www.spacex.com/news/2013/02/11/flight-4-la first_event.links['wikipedia'] # https://en.wikipedia.org/wiki/Falcon_1 ``` + +### LaunchPads + +- Get information for all LaunchPads: `SPACEX::LaunchPads.info` +- Get information about a specific LaunchPad: `SPACEX::LaunchPads.info('vafb_slc_4e')` + +This code shows how to get the information for a specific LaunchPad by site_id and lists the fields: + +```ruby +launch_pad = SPACEX::LaunchPads.info('vafb_slc_4e') + +launch_pad.id # 6 +launch_pad.name # 'VAFB SLC 4E' +launch_pad.status # 'active' +launch_pad.location['name'] # 'Vandenberg Air Force Base', +launch_pad.location['region'] # 'California', +launch_pad.location['latitude'] # 34.632093, +launch_pad.location['longitude'] # -120.610829 +launch_pad.vehicles_launched # ['Falcon 9'] +launch_pad.attempted_launches # 15 +launch_pad.successful_launches # 15 +launch_pad.wikipedia # 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4' +launch_pad.details # 'SpaceX primary west coast launch pad for polar orbits and sun synchronous orbits, primarily used for Iridium. Also intended to be capable of launching Falcon Heavy.' +launch_pad.site_id # 'vafb_slc_4e' +launch_pad.site_name_long # 'Vandenberg Air Force Base Space Launch Complex 4E' +``` + + ### Launches - Get information for all launches: `SPACEX::Launches.all` or `SPACEX::Launches.info` diff --git a/lib/spacex.rb b/lib/spacex.rb index f68e41a..cc849dd 100644 --- a/lib/spacex.rb +++ b/lib/spacex.rb @@ -10,6 +10,7 @@ require_relative 'spacex/dragon_capsules' require_relative 'spacex/endpoint' require_relative 'spacex/history' +require_relative 'spacex/launch_pads' require_relative 'spacex/launches' require_relative 'spacex/missions' require_relative 'spacex/payloads' 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/lib/spacex/launch_pads.rb b/lib/spacex/launch_pads.rb new file mode 100644 index 0000000..9a22d3e --- /dev/null +++ b/lib/spacex/launch_pads.rb @@ -0,0 +1,11 @@ +module SPACEX + module LaunchPads + def self.info(site_id = nil) + SPACEX::BaseRequest.info("launchpads/#{site_id}") + end + + def self.all + info + end + end +end diff --git a/spec/fixtures/spacex/launch_pads/info.yml b/spec/fixtures/spacex/launch_pads/info.yml new file mode 100644 index 0000000..5a96558 --- /dev/null +++ b/spec/fixtures/spacex/launch_pads/info.yml @@ -0,0 +1,104 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.spacexdata.com/v3/launchpads/ + 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:14:07 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Set-Cookie: + - __cfduid=da830d2d8f160129e4ad12af7bfdb29561569978847; expires=Thu, 01-Oct-20 + 01:14:07 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-Cache: + - HIT + Spacex-Api-Count: + - '6' + Spacex-Api-Response-Time: + - 1ms + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 51f2d1570f89e0aa-IAD + body: + encoding: ASCII-8BIT + string: '[{"id":5,"name":"VAFB SLC 3W","status":"retired","location":{"name":"Vandenberg + Air Force Base","region":"California","latitude":34.6440904,"longitude":-120.5931438},"vehicles_launched":["Falcon + 1"],"attempted_launches":0,"successful_launches":0,"wikipedia":"https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_3","details":"SpaceX + original west coast launch pad for Falcon 1. Performed a static fire but was + never used for a launch and abandoned due to scheduling conflicts.","site_id":"vafb_slc_3w","site_name_long":"Vandenberg + Air Force Base Space Launch Complex 3W"},{"id":2,"name":"CCAFS SLC 40","status":"active","location":{"name":"Cape + Canaveral","region":"Florida","latitude":28.5618571,"longitude":-80.577366},"vehicles_launched":["Falcon + 9"],"attempted_launches":45,"successful_launches":43,"wikipedia":"https://en.wikipedia.org/wiki/Cape_Canaveral_Air_Force_Station_Space_Launch_Complex_40","details":"SpaceX + primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior + to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed + Martin. Back online since CRS-13 on 2017-12-15.","site_id":"ccafs_slc_40","site_name_long":"Cape + Canaveral Air Force Station Space Launch Complex 40"},{"id":8,"name":"STLS","status":"under + construction","location":{"name":"Boca Chica Village","region":"Texas","latitude":25.9972641,"longitude":-97.1560845},"vehicles_launched":["Falcon + 9"],"attempted_launches":0,"successful_launches":0,"wikipedia":"https://en.wikipedia.org/wiki/SpaceX_South_Texas_Launch_Site","details":"SpaceX + new launch site currently under construction to help keep up with the Falcon + 9 and Heavy manifests. Expected to be completed in late 2018. Initially will + be limited to 12 flights per year, and only GTO launches.","site_id":"stls","site_name_long":"SpaceX + South Texas Launch Site"},{"id":1,"name":"Kwajalein Atoll","status":"retired","location":{"name":"Omelek + Island","region":"Marshall Islands","latitude":9.0477206,"longitude":167.7431292},"vehicles_launched":["Falcon + 1"],"attempted_launches":5,"successful_launches":2,"wikipedia":"https://en.wikipedia.org/wiki/Omelek_Island","details":"SpaceX + original launch site, where all of the Falcon 1 launches occured. Abandoned + as SpaceX decided against upgrading the pad to support Falcon 9.","site_id":"kwajalein_atoll","site_name_long":"Kwajalein + Atoll Omelek Island"},{"id":6,"name":"VAFB SLC 4E","status":"active","location":{"name":"Vandenberg + Air Force Base","region":"California","latitude":34.632093,"longitude":-120.610829},"vehicles_launched":["Falcon + 9"],"attempted_launches":15,"successful_launches":15,"wikipedia":"https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4","details":"SpaceX + primary west coast launch pad for polar orbits and sun synchronous orbits, + primarily used for Iridium. Also intended to be capable of launching Falcon + Heavy.","site_id":"vafb_slc_4e","site_name_long":"Vandenberg Air Force Base + Space Launch Complex 4E"},{"id":4,"name":"KSC LC 39A","status":"active","location":{"name":"Cape + Canaveral","region":"Florida","latitude":28.6080585,"longitude":-80.6039558},"vehicles_launched":["Falcon + 9","Falcon Heavy"],"attempted_launches":18,"successful_launches":18,"wikipedia":"https://en.wikipedia.org/wiki/Kennedy_Space_Center_Launch_Complex_39#Launch_Pad_39A","details":"NASA + historic launch pad that launched most of the Saturn V and Space Shuttle missions. + Initially for Falcon Heavy launches, it is now launching all of SpaceX east + coast missions due to the damage from the AMOS-6 anomaly. After SLC-40 repairs + are complete, it will be upgraded to support Falcon Heavy, a process which + will take about two months. In the future it will launch commercial crew missions + and the Interplanetary Transport System.","site_id":"ksc_lc_39a","site_name_long":"Kennedy + Space Center Historic Launch Complex 39A"}]' + http_version: + recorded_at: Wed, 02 Oct 2019 01:14:07 GMT +recorded_with: VCR 4.0.0 diff --git a/spec/fixtures/spacex/launch_pads/info/vafb_slc_4e.yml b/spec/fixtures/spacex/launch_pads/info/vafb_slc_4e.yml new file mode 100644 index 0000000..2c66108 --- /dev/null +++ b/spec/fixtures/spacex/launch_pads/info/vafb_slc_4e.yml @@ -0,0 +1,71 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.spacexdata.com/v3/launchpads/vafb_slc_4e + 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:26:54 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Set-Cookie: + - __cfduid=d77218edc77d1c7147daab312fe4c4eb51569979614; expires=Thu, 01-Oct-20 + 01:26:54 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-Cache: + - MISS + Spacex-Api-Response-Time: + - 32ms + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 51f2e40cebffcef0-IAD + body: + encoding: ASCII-8BIT + string: '{"id":6,"name":"VAFB SLC 4E","status":"active","location":{"name":"Vandenberg + Air Force Base","region":"California","latitude":34.632093,"longitude":-120.610829},"vehicles_launched":["Falcon + 9"],"attempted_launches":15,"successful_launches":15,"wikipedia":"https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4","details":"SpaceX + primary west coast launch pad for polar orbits and sun synchronous orbits, + primarily used for Iridium. Also intended to be capable of launching Falcon + Heavy.","site_id":"vafb_slc_4e","site_name_long":"Vandenberg Air Force Base + Space Launch Complex 4E"}' + http_version: + recorded_at: Wed, 02 Oct 2019 01:26:54 GMT +recorded_with: VCR 4.0.0 diff --git a/spec/spacex/launch_pads_spec.rb b/spec/spacex/launch_pads_spec.rb new file mode 100644 index 0000000..b55c5d6 --- /dev/null +++ b/spec/spacex/launch_pads_spec.rb @@ -0,0 +1,77 @@ +require 'spec_helper' + +describe SPACEX::LaunchPads do + context '#info', vcr: { cassette_name: 'launch_pads/info' } do + subject do + SPACEX::LaunchPads.info + end + + it "returns all LaunchPads' info when no id is passed" do + first_subject = subject.first + + expect(subject.class).to eq Array + expect(first_subject.id).to eq 5 + expect(first_subject.name).to eq 'VAFB SLC 3W' + expect(first_subject.status).to eq 'retired' + expect(first_subject.location).to eq( + 'name' => 'Vandenberg Air Force Base', + 'region' => 'California', + 'latitude' => 34.6440904, + 'longitude' => -120.5931438 + ) + expect(first_subject.vehicles_launched).to eq [ + 'Falcon 1' + ] + expect(first_subject.attempted_launches).to eq 0 + expect(first_subject.successful_launches).to eq 0 + expect(first_subject.wikipedia).to eq( + 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_3' + ) + expect(first_subject.details).to eq 'SpaceX original ' \ + 'west coast launch pad for Falcon 1. Performed a static fire but was ' \ + 'never used for a launch and abandoned due to scheduling conflicts.' + expect(first_subject.site_id).to eq 'vafb_slc_3w' + expect(first_subject.site_name_long).to eq( + 'Vandenberg Air Force Base Space Launch Complex 3W' + ) + end + end + + context "#info('vafb_slc_4e')", vcr: { + cassette_name: 'launch_pads/info/vafb_slc_4e' + } do + subject do + SPACEX::LaunchPads.info('vafb_slc_4e') + end + + it 'returns LaunchPad info for "vafb_slc_4e"' do + expect(subject.class).to eq SPACEX::Response + expect(subject.id).to eq 6 + expect(subject.name).to eq 'VAFB SLC 4E' + expect(subject.status).to eq 'active' + expect(subject.location).to eq( + 'name' => 'Vandenberg Air Force Base', + 'region' => 'California', + 'latitude' => 34.632093, + 'longitude' => -120.610829 + ) + expect(subject.vehicles_launched).to eq [ + 'Falcon 9' + ] + expect(subject.attempted_launches).to eq 15 + expect(subject.successful_launches).to eq 15 + expect(subject.wikipedia).to eq( + 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4' + ) + + expect(subject.details).to eq 'SpaceX primary ' \ + 'west coast launch pad for polar orbits and sun synchronous orbits, ' \ + 'primarily used for Iridium. Also intended to be capable of ' \ + 'launching Falcon Heavy.' + expect(subject.site_id).to eq 'vafb_slc_4e' + expect(subject.site_name_long).to eq( + 'Vandenberg Air Force Base Space Launch Complex 4E' + ) + end + end +end