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

Soa challenge #92

Open
wants to merge 3 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
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem 'jbuilder', '~> 2.5'
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'faraday'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
Expand All @@ -38,10 +39,11 @@ gem 'bootsnap', '>= 1.1.0', require: false

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 'webmock'
gem 'vcr'
end

group :development do
Expand Down
24 changes: 22 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ GEM
bootsnap (1.9.1)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
capybara (3.36.0)
addressable
matrix
Expand All @@ -68,13 +67,20 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.9)
crack (0.4.5)
rexml
crass (1.0.6)
diff-lcs (1.4.4)
erubi (1.10.0)
execjs (2.8.1)
faraday (2.6.0)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.1)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
hashdiff (1.0.1)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
Expand All @@ -92,9 +98,13 @@ GEM
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
msgpack (1.4.2)
nio4r (2.5.8)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
nokogiri (1.12.5-arm64-darwin)
racc (~> 1.4)
pg (1.2.3)
Expand Down Expand Up @@ -136,6 +146,7 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
Expand All @@ -153,6 +164,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 @@ -186,11 +198,16 @@ GEM
thread_safe (~> 0.1)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
vcr (6.1.0)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -199,12 +216,13 @@ GEM

PLATFORMS
arm64-darwin-20
ruby

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)
Expand All @@ -218,7 +236,9 @@ DEPENDENCIES
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
vcr
web-console (>= 3.3.0)
webmock

RUBY VERSION
ruby 2.7.4p191
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/merchants.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/merchants.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Merchants controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
10 changes: 10 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ItemsController < ApplicationController
def index
@items = ItemFacade.items
end

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

def show
@items = MerchantFacade.merchant_items(params[:id])
end
end
13 changes: 13 additions & 0 deletions app/facades/item_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ItemFacade
def self.items
items_json = EngineService.items
items_json[:data].map do |item|
Item.new(item)
end
end

def self.item(id)
item = EngineService.item(id)
Item.new(item[:data])
end
end
16 changes: 16 additions & 0 deletions app/facades/merchant_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MerchantFacade

def self.merchants
merchants_json = EngineService.merchants
merchants_json[:data].map do |merchant|
Merchant.new(merchant)
end
end

def self.merchant_items(merchant_id)
items_json = EngineService.merchant_items(merchant_id)
items_json[:data].map do |item|
Item.new(item)
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/merchants_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MerchantsHelper
end
15 changes: 15 additions & 0 deletions app/poros/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Item
attr_reader :id,
:name,
:description,
:unit_price,
:merchant_id

def initialize(item_data)
@id = item_data[:id].to_i
@name = item_data[:attributes][:name]
@description = item_data[:attributes][:description]
@unit_price = item_data[:attributes][:unit_price]
@merchant_id = item_data[:attributes][:merchant_id]
end
end
8 changes: 8 additions & 0 deletions app/poros/merchant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Merchant
attr_reader :id, :name

def initialize(merchant_data)
@id = merchant_data[:id].to_i
@name = merchant_data[:attributes][:name]
end
end
28 changes: 28 additions & 0 deletions app/services/engine_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class EngineService

def self.merchants
response = conn.get('/api/v1/merchants')
JSON.parse(response.body, symbolize_names: true)
end

def self.merchant_items(merchant_id)
response = conn.get("/api/v1/merchants/#{merchant_id}/items")
JSON.parse(response.body, symbolize_names: true)
end

def self.items
response = conn.get('/api/v1/items')
JSON.parse(response.body, symbolize_names: true)
end

def self.item(item_id)
response = conn.get("/api/v1/items/#{item_id}")
JSON.parse(response.body, symbolize_names: true)
end

private

def self.conn
Faraday.new(url: 'http://localhost:3000')
end
end
6 changes: 6 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1> Items </h1>
<ul>
<% @items.each do |item| %>
<li><%= link_to item.name, item_path(item.id) %></li>
<% end %>
</ul>
5 changes: 5 additions & 0 deletions app/views/items/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1><%= @item.name %> </h1>
<ul>
<li><%[email protected]%></li>
<li><%[email protected]_price%></li>
</ul>
6 changes: 6 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1> Merchants <h1>
<ul>
<% @merchants.each do |merchant| %>
<li><%= link_to merchant.name, merchant_path(merchant.id) %></li>
<% end %>
</ul>
6 changes: 6 additions & 0 deletions app/views/merchants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Merchant <%= @items.first.merchant_id %> </h1>
<ul>
<% @items.each do |item| %>
<li><%=item.name%></li>
<% end %>
</ul>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :merchants, only: %i[index show] do
resources :items, only: %i[index]
end

resources :items, only: %i[index show]

end
Loading