Skip to content

Commit

Permalink
Merge pull request #35 from turingschool/feature/microservice-connection
Browse files Browse the repository at this point in the history
Feature/microservice connection
  • Loading branch information
jdmchugh111 authored Aug 31, 2024
2 parents 54f2697 + fcf5249 commit fcfaa5f
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,4 +62,4 @@ end

gem 'redis'
gem 'sidekiq'
gem 'sidekiq-cron'
gem 'sidekiq-cron'
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,22 @@ This is the backend API repository for TurLink. TurLink is a link shortener app
]
}
```
### Get Summary for a Link
- **GET** `/api/v1/summary?link={original link}`
- Description: Retrieves a summary of the content at the given link.
- Example Request: GET `https://turlink-be-53ba7254a7c1.herokuapp.com/api/v1/summary?link=www.example.com`
- Successful Response (200 OK):
```json
{
"data": {
"attributes": {
"link": "www.example.com",
"summary": "1. example 1\n2. example 2\n3. example 3"
}
}
}
```
- Notes:
- This endpoint currently returns mock data.
- In the future, it will provide an actual summary of the content at the given link.
- The summary is expected to be a string with numbered points, separated by newline characters.
7 changes: 7 additions & 0 deletions app/controllers/api/v1/summaries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Api::V1::SummariesController < ApplicationController
def show
summary = SummaryService.new.summarize
summary_json = JSON.parse(summary.body, symbolize_names: true)
render json: SummarySerializer.new(summary_json).serialize_json
end
end
17 changes: 17 additions & 0 deletions app/serializers/summary_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class SummarySerializer
def initialize(summary)
@link = summary[:data][:link]
@summary = summary[:data][:summary]
end

def serialize_json
{
data: {
attributes: {
link: @link,
summary: @summary
}
}
}
end
end
9 changes: 9 additions & 0 deletions app/services/summary_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SummaryService
def conn
Faraday.new('https://nameless-garden-14218-de5663d17d61.herokuapp.com/')
end

def summarize
conn.get('/api/v1/ping/')
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
resources :links, only: %i[index], action: :show
resources :tags, only: %i[index create destroy]
get 'top_links', to: 'links#top_links'
get 'summary', to: 'summaries#show'
end
end

Expand Down
Binary file added dump.rdb
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/requests/api/v1/summary_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe 'summary request' do
it 'can get a summary of a link', :vcr do
get '/api/v1/summary?link=www.example.com'

expect(response).to be_successful

summary = JSON.parse(response.body, symbolize_names: true)

expect(summary[:data][:attributes][:link]).to eq('www.example.com')
expect(summary[:data][:attributes][:summary]).to eq("1. example 1\n2. example 2\n3. example 3")
end
end
15 changes: 15 additions & 0 deletions spec/services/summary_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe SummaryService do
it 'gets a summary for a link', :vcr do
response = SummaryService.new.summarize

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

0 comments on commit fcfaa5f

Please sign in to comment.