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

A.J. Krumholz #87

Open
wants to merge 4 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
11 changes: 10 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@ 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'
end

group :test do
gem 'vcr'
gem 'faker'
gem 'factory_bot_rails'
gem 'shoulda-matchers'
gem 'capybara'
gem 'launchy'
end

group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
Expand Down
36 changes: 30 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
arel (9.0.0)
bindex (0.8.1)
bootsnap (1.9.1)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
capybara (3.36.0)
capybara (3.37.1)
addressable
matrix
mini_mime (>= 0.1.3)
Expand All @@ -72,13 +72,26 @@ GEM
diff-lcs (1.4.4)
erubi (1.10.0)
execjs (2.8.1)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (2.19.0)
i18n (>= 1.6, < 2)
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)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
launchy (2.5.0)
addressable (~> 2.7)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -101,7 +114,7 @@ GEM
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
public_suffix (5.0.0)
puma (3.12.6)
racc (1.6.0)
rack (2.2.3)
Expand Down Expand Up @@ -135,7 +148,7 @@ GEM
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.1.1)
regexp_parser (2.6.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
Expand All @@ -153,6 +166,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 All @@ -165,6 +179,8 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shoulda-matchers (5.2.0)
activesupport (>= 5.2.0)
spring (2.1.1)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand All @@ -186,6 +202,7 @@ 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)
Expand All @@ -199,29 +216,36 @@ GEM

PLATFORMS
arm64-darwin-20
arm64-darwin-21

DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara
coffee-rails (~> 4.2)
factory_bot_rails
faker
faraday
jbuilder (~> 2.5)
launchy
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
puma (~> 3.11)
rails (~> 5.2.6)
rspec-rails
sass-rails (~> 5.0)
shoulda-matchers
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
vcr
web-console (>= 3.3.0)

RUBY VERSION
ruby 2.7.4p191

BUNDLED WITH
2.2.27
2.3.20
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 = ItemsFacade.items
end

def show
@item = ItemsFacade.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 = MerchantsFacade.merchants
end

def show
@merchant = MerchantsFacade.merchant(params[:id])
end
end
7 changes: 7 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class WelcomeController < ApplicationController
def index
if params[:search].present?
@merchant = MerchantsFacade.find(params[:search])
end
end
end
11 changes: 11 additions & 0 deletions app/facades/items_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ItemsFacade
def self.items
items_hash = ItemsService.items[:data]
items_hash.map { |item_info| Item.new(item_info)}
end

def self.item(id)
item_info = ItemsService.item(id)[:data]
Item.new(item_info)
end
end
24 changes: 24 additions & 0 deletions app/facades/merchants_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MerchantsFacade
def self.merchants
merchants_hash = MerchantsService.merchants[:data]
merchants_hash.map { |merchant_info| Merchant.new(merchant_info)}
end

def self.merchant(id)
merchant_hash = MerchantsService.merchant(id)[:data]
Merchant.new(merchant_hash, merchant_items(id))
end

def self.find(query)
merchant_hash = MerchantsService.find(query)[:data]
id = merchant_hash[:id]
Merchant.new(merchant_hash, merchant_items(id))
end

private

def self.merchant_items(merchant_id)
item_hash = MerchantsService.merchant_items(merchant_id)[:data]
item_hash.map { |item_info| Item.new(item_info) }
end
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(data)
@id = data[:id]
@name = data[:attributes][:name]
@description = data[:attributes][:description]
@unit_price = data[:attributes][:unit_price]
@merchant_id = data[:attributes][:merchant_id]
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, :items

def initialize(data, items = [])
@name = data[:attributes][:name]
@id = data[:id]
@items = items
end
end
13 changes: 13 additions & 0 deletions app/services/items_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ItemsService
def self.items
conn = Faraday.new(url: 'http://localhost:3000/api/v1/items')
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end

def self.item(id)
conn = Faraday.new(url: "http://localhost:3000/api/v1/items/#{id}")
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end
end
28 changes: 28 additions & 0 deletions app/services/merchants_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'faraday'
require 'json'

class MerchantsService
def self.merchants
conn = Faraday.new(url: 'http://localhost:3000/api/v1/merchants')
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end

def self.merchant(id)
conn = Faraday.new(url: "http://localhost:3000/api/v1/merchants/#{id}")
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end

def self.merchant_items(id)
conn = Faraday.new(url: "http://localhost:3000/api/v1/merchants/#{id}/items")
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end

def self.find(query)
conn = Faraday.new(url: "http://localhost:3000/api/v1/merchants/find?name=#{query}")
response = conn.get
JSON.parse(response.body, symbolize_names: true)
end
end
9 changes: 9 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>All Items</h1>

<ul>
<% @items.each do |item| %>
<li>
<%= link_to item.name, item_path(item.id) %>
</li>
<% end %>
</ul>
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><%= @item.description %></p>
<p>Price: <%= @item.unit_price %></p>
9 changes: 9 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>merchants index</h1>

<ul>
<% @merchants.each do |merchant| %>
<li>
<p><%= link_to merchant.name, merchant_path(merchant.id) %></p>
</li>
<% end %>
</ul>
13 changes: 13 additions & 0 deletions app/views/merchants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>merchant show page</h1>

<h1><%= @merchant.name %></h1>

<ul>
<% @merchant.items.each do |item| %>
<li>
<h3><%= item.name %></h3>
<p><%= item.description %></p>
<p>Price: <%= item.unit_price %></p>
</li>
<% end %>
</ul>
16 changes: 16 additions & 0 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="search-form">
<%= form_with url: root_path, method: :get do |f| %>
<%= f.text_field :search %>
<%= f.submit "Search Merchants by Name" %>
<% end %>
</div>

<% if [email protected]? %>
<div id="search-results">
<ul>
<li>
<%= link_to @merchant.name, merchant_path(@merchant.id) %>
</li>
</ul>
</div>
<% end %>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'welcome#index'
resources :merchants, only: %i[index show]
resources :items, only: %i[index show]
end
18 changes: 18 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

end
7 changes: 7 additions & 0 deletions spec/factories/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :item do
name {Faker::Commerce.product_name}
description {Faker::Marketing.buzzwords}
unit_price {Faker::Number.decimal(l_digits: 2, r_digits: 2)}
end
end
Loading