From 8d157c845802cae79090849123ac5c1f2638119f Mon Sep 17 00:00:00 2001 From: Noah Durbin <13364668+noahdurbin@users.noreply.github.com> Date: Fri, 30 Aug 2024 15:37:09 -0600 Subject: [PATCH] feat: service for connecting to the mock data endpoint --- Gemfile | 4 +++- Gemfile.lock | 15 +++++++++++++++ app/services/summary_service.rb | 8 +++++--- dump.rdb | Bin 0 -> 566 bytes spec/rails_helper.rb | 7 +++++++ spec/services/summary_service_spec.rb | 17 +++++++++++++++++ 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 dump.rdb create mode 100644 spec/services/summary_service_spec.rb diff --git a/Gemfile b/Gemfile index 66b394f..374174c 100644 --- a/Gemfile +++ b/Gemfile @@ -51,6 +51,8 @@ group :development, :test do gem 'rspec-rails', '~> 6.1' gem 'shoulda-matchers', '~> 6.4' gem 'simplecov', '~> 0.22.0' + gem 'vcr', '~> 6.3' + gem "webmock", "~> 3.23" end group :development do @@ -60,4 +62,4 @@ end gem 'redis' gem 'sidekiq' -gem 'sidekiq-cron' \ No newline at end of file +gem 'sidekiq-cron' diff --git a/Gemfile.lock b/Gemfile.lock index 3f01e20..5c71581 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -95,6 +95,9 @@ GEM coderay (1.1.3) concurrent-ruby (1.3.4) connection_pool (2.4.1) + crack (1.0.0) + bigdecimal + rexml crass (1.0.6) date (3.3.4) debug (1.9.2) @@ -123,6 +126,7 @@ GEM raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) + hashdiff (1.1.1) i18n (1.14.5) concurrent-ruby (~> 1.0) io-console (0.7.2) @@ -231,6 +235,8 @@ GEM regexp_parser (2.9.2) reline (0.5.9) io-console (~> 0.5) + rexml (3.3.6) + strscan rspec-core (3.13.0) rspec-support (~> 3.13.0) rspec-expectations (3.13.2) @@ -267,11 +273,18 @@ GEM simplecov-html (0.12.3) simplecov_json_formatter (0.1.4) stringio (3.1.1) + strscan (3.1.0) thor (1.3.1) timeout (0.4.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) uri (0.13.0) + vcr (6.3.1) + base64 + webmock (3.23.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) webrick (1.8.1) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) @@ -309,6 +322,8 @@ DEPENDENCIES sidekiq-cron simplecov (~> 0.22.0) tzinfo-data + vcr (~> 6.3) + webmock (~> 3.23) RUBY VERSION ruby 3.2.2p53 diff --git a/app/services/summary_service.rb b/app/services/summary_service.rb index bff4f9d..521d901 100644 --- a/app/services/summary_service.rb +++ b/app/services/summary_service.rb @@ -6,9 +6,11 @@ def conn ) end - def summarize - conn.post('api/v1/ping') do |req| - req.body = { link: 'wwww.example.com' }.to_json + def summarize(resource) + response = conn.post('api/v1/ping') do |req| + req.body = { link: resource }.to_json end + + JSON.parse(response.body, symbolize_names: true) end end diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000000000000000000000000000000000000..ef8da4a1ba06e827ea7a111e6fd762201b0ef3ed GIT binary patch literal 566 zcma)(O>5Lp7=>>p#7Sq0of%s!1?@F{ETjoHvBT7?Qd+@ESL(_##GB-ua-DqC+@$R& z#UNwH4-f{5Zd`QZ(%)dIt8V=PrN1Ku$4;ePi3soN@E#t{IS=nGE-wy&&DHfr<8Ey*K5_=&ns(w9(AP8JpTt{(%FX|J{4rmfUzlDH5Sl{WkOm-q z`Ad@Od6X+}+CJs{!sp!^XleI4TDFw22az+`9nz$C1QUGQ5~fNYJm7B3))E$lqnWVH zqcnD**z-C#m}airrSU44UavI8UI3iaRgk~GR8Ct`%(CJ^i>7{}%qXt4fvZOsH}N&= zenl}?np@U3PiT_zVh=*=py0{xvxDMrgEA8OPn*cbugYMr#n57#wM^JKDvXbM;In`w t5Km-6A8&|>4Pkaw91e|VN*{SAa3Y_*0NYB%F(eEIpN`)iE`KvhzX4v+zSRH# literal 0 HcmV?d00001 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 87d20f2..8dbb918 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -73,3 +73,10 @@ with.library :rails end end + +VCR.configure do |config| + config.cassette_library_dir = 'spec/fixtures/vcr_cassettes' + config.hook_into :webmock + config.configure_rspec_metadata! + config.allow_http_connections_when_no_cassette = true +end diff --git a/spec/services/summary_service_spec.rb b/spec/services/summary_service_spec.rb new file mode 100644 index 0000000..aee4d04 --- /dev/null +++ b/spec/services/summary_service_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +RSpec.describe SummaryService do + it 'gets a summary for a link', :vcr do + sum_service = SummaryService.new + sum_service.summarize('www.example.com') + + expect(response).to be_successful + summary = JSON.parse(response.body, symbolize_names: true) + + expect(summary[:data]).to have_key(:link) + expect(summary[:data]).to have_key(:summary) + + expect(summary[:data][link]).to eq 'www.example.com' + expect(summary[:data][:summary]).to eq "1. example 1\n2. example 2\n3. example 3" + end +end