Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leo Banos Garcia SOA Challenge #106

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ gem 'jbuilder', '~> 2.5'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'faraday'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
gem 'capybara'
gem 'pry'
gem 'pry-rails'
end

group :development do
Expand Down
17 changes: 14 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ GEM
diff-lcs (1.4.4)
erubi (1.10.0)
execjs (2.8.1)
faraday (2.7.4)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
Expand All @@ -97,10 +101,14 @@ GEM
nio4r (2.5.8)
nokogiri (1.12.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.5-x86_64-darwin)
racc (~> 1.4)
pg (1.2.3)
pry (0.14.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-rails (0.3.9)
pry (>= 0.10.4)
public_suffix (4.0.6)
puma (3.12.6)
racc (1.6.0)
Expand Down Expand Up @@ -153,6 +161,7 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby2_keywords (0.0.5)
ruby_dep (1.5.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
Expand Down Expand Up @@ -199,16 +208,18 @@ GEM

PLATFORMS
arm64-darwin-20
x86_64-darwin-21

DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara
coffee-rails (~> 4.2)
faraday
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
pry-rails
puma (~> 3.11)
rails (~> 5.2.6)
rspec-rails
Expand All @@ -224,4 +235,4 @@ RUBY VERSION
ruby 2.7.4p191

BUNDLED WITH
2.2.27
2.4.4
9 changes: 9 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ItemsController < ApplicationController
def index
@items = ItemFacade.all_items
end

def show
@item = ItemFacade.one_item(params[:id])
end
end
10 changes: 10 additions & 0 deletions app/controllers/merchants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class MerchantsController < ApplicationController
def index
@merchants = MerchantFacade.all_merchants
end

def show
@merchant = MerchantFacade.one_merchant(params[:id])
@items = MerchantFacade.one_merchants_items(params[:id])
end
end
15 changes: 15 additions & 0 deletions app/facades/item_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ItemFacade
def self.all_items
response = RailsEngineService.get_all_items
items = response[:data]

items.map do |data|
Item.new(data)
end
end

def self.one_item(item_id)
response = RailsEngineService.get_one_item(item_id)
Item.new(response[:data])
end
end
24 changes: 24 additions & 0 deletions app/facades/merchant_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MerchantFacade
def self.all_merchants
response = RailsEngineService.get_all_merchants
merchants = response[:data]

merchants.map do |data|
Merchant.new(data)
end
end

def self.one_merchant(merchant_id)
response = RailsEngineService.get_one_merchant(merchant_id)
Merchant.new(response[:data])
end

def self.one_merchants_items(merchant_id)
response = RailsEngineService.get_one_merchants_items(merchant_id)
items = response[:data]

items.map do |data|
Item.new(data)
end
end
end
12 changes: 12 additions & 0 deletions app/poros/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Item
attr_reader :id,
:name,
:description,
:unit_price
def initialize(data)
@id = data[:id]
@name = data[:attributes][:name]
@description = data[:attributes][:description]
@unit_price = data[:attributes][:unit_price]
end
end
9 changes: 9 additions & 0 deletions app/poros/merchant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Merchant
attr_reader :name,
:id
def initialize(data)
@name = data[:attributes][:name]
# binding.pry
@id = data[:id]
end
end
34 changes: 34 additions & 0 deletions app/services/rails_engine_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class RailsEngineService
def self.conn
Faraday.new('http://localhost:3000/api/v1/')
end

def self.json_parse(response)
JSON.parse(response.body, symbolize_names: true)
end

def self.get_all_merchants
response = conn.get('merchants')
json_parse(response)
end

def self.get_one_merchant(merchant_id)
response = conn.get("merchants/#{merchant_id}")
json_parse(response)
end

def self.get_one_merchants_items(merchant_id)
response = conn.get("merchants/#{merchant_id}/items")
json_parse(response)
end

def self.get_all_items
response = conn.get('items')
json_parse(response)
end

def self.get_one_item(item_id)
response = conn.get("items/#{item_id}")
json_parse(response)
end
end
5 changes: 5 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Items</h1>

<% @items.each do |item| %>
<p><%= link_to item.name, "/items/#{item.id}" %></p>
<% end %>
4 changes: 4 additions & 0 deletions app/views/items/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1><%= @item.name %></h1>

<p>Description: <%= @item.description %></p>
<p>Unit price: $<%= @item.unit_price %></p>
7 changes: 7 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Merchants</h1>

<section id="merchants">
<% @merchants. each do |merchant| %>
<p><%= link_to merchant.name, "/merchants/#{merchant.id}" %></p><br/>
<% end %>
</section>
7 changes: 7 additions & 0 deletions app/views/merchants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%= @merchant.name %>'s Items</h1>

<section id="items">
<% @items.each do |item| %>
<p><%= item.name %></p>
<% end %>
</section>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :merchants, only:[:index, :show]
resources :items, only:[:index, :show]
end
28 changes: 28 additions & 0 deletions spec/facades/item_facade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rails_helper'

RSpec.describe ItemFacade do
describe 'Item calls' do
it 'all_items' do
items = ItemFacade.all_items

expect(items).to be_a(Array)

items.each do |item|
expect(item).to be_instance_of(Item)
end
expect(items[0].name).to eq("Item Nemo Facere")
expect(items[1].name).to eq("Item Expedita Aliquam")
end

it 'one_item' do
item = ItemFacade.one_item(179)

expect(item).to be_instance_of(Item)

expect(item.id).to eq("179")
expect(item.name).to eq("Item Qui Veritatis")
expect(item.description).to eq("Totam labore quia harum dicta eum consequatur qui. Corporis inventore consequatur. Illum facilis tempora nihil placeat rerum sint est. Placeat ut aut. Eligendi perspiciatis unde eum sapiente velit.")
expect(item.unit_price).to eq(906.17)
end
end
end
39 changes: 39 additions & 0 deletions spec/facades/merchant_facade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'

RSpec.describe MerchantFacade do
describe 'merchant calls' do
it 'all_merchants' do
merchants = MerchantFacade.all_merchants

expect(merchants).to be_a(Array)

merchants.each do |merchant|
expect(merchant).to be_instance_of(Merchant)
end
expect(merchants[0].name).to eq("Schroeder-Jerde")
expect(merchants[1].name).to eq("Klein, Rempel and Jones")
end

it 'one_merchant' do
merchant = MerchantFacade.one_merchant(42)

expect(merchant).to be_instance_of(Merchant)
expect(merchant.id).to eq("42")
expect(merchant.name).to eq("Glover Inc")
end

it "one_merchants_items" do
items = MerchantFacade.one_merchants_items(99)

expect(items).to be_a(Array)

items.each do |item|
expect(item).to be_instance_of(Item)
end

expect(items[0].name).to eq("Item Excepturi Rem")
expect(items[0].description).to eq("Perferendis reprehenderit fugiat sit eos. Corporis ipsum ut. Natus molestiae quia rerum fugit quis. A cumque doloremque magni.")
expect(items[0].unit_price).to eq(476.82)
end
end
end
11 changes: 11 additions & 0 deletions spec/features/items/item_show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe "Item show" do
it 'an items show page will list a description and unit price' do
visit "/items/45"

expect(page).to have_content("Item Id Aut")
expect(page).to have_content("Description: Blanditiis alias cupiditate dolores. Cum architecto est magnam similique quis culpa est. Incidunt consequatur sunt maxime veritatis labore officia. Quaerat nemo molestias et omnis quia.")
expect(page).to have_content("Unit price: $318.76")
end
end
26 changes: 26 additions & 0 deletions spec/features/items/items_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe 'items index', type: :feature do
describe 'visiting /items' do
it 'see list of items by name' do
visit '/items'

expect(page).to have_content("Item Nemo Facere")
expect(page).to have_content("Item Expedita Aliquam")
expect(page).to have_content("Item Provident At")
expect(page).to have_content("Item Expedita Fuga")
expect(page).to have_content("Item Est Consequuntur")
expect(page).to have_content("Item Quo Magnam")
expect(page).to have_content("Item Quidem Suscipit")
expect(page).to have_content("Item Rerum Magni")
end
end

it 'can click on an items name and be directed to that items show page' do
visit '/items'

click_link "Item Nemo Facere"

expect(current_path).to eq("/items/4")
end
end
32 changes: 32 additions & 0 deletions spec/features/merchants/merchants_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe 'merchants index', type: :feature do
describe 'visiting /merchants' do
it 'see list of merchants by name' do
visit '/merchants'

within "#merchants" do
expect(page).to have_content("Schroeder-Jerde")
expect(page).to have_content("Klein, Rempel and Jones")
end
end

it 'when a merchants name is clicked the user should be directed to /merchants/:id
and they should see a list of items that merchant sells' do
visit '/merchants'

click_link 'Schroeder-Jerde'

expect(current_path).to eq('/merchants/1')
expect(page).to have_content("Schroeder-Jerde's Items")

within "#items" do
expect(page).to have_content("Item Nemo Facere")
expect(page).to have_content("Item Expedita Aliquam")
expect(page).to have_content("Item Provident At")
expect(page).to have_content("Item Expedita Fuga")
expect(page).to have_content("Item Est Consequuntur")
end
end
end
end
Loading