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

Tom Musselman #94

Open
wants to merge 13 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: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ 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]
Expand All @@ -54,5 +56,6 @@ group :development do
end



# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
11 changes: 11 additions & 0 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.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)
Expand All @@ -92,9 +96,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 @@ -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,12 +208,14 @@ 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 Down
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.get_items
end

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

def show
@merchant = MerchantsFacade.get_merchant(params[:id])
end
end
20 changes: 20 additions & 0 deletions app/facades/items_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ItemsFacade
def self.get_merchant_items(merchant_id)
items_data = ItemsService.get_merchant_items(merchant_id)[:data]
items_data.map do |item_data|
Item.new(item_data)
end
end

def self.get_item(item_id)
item_data = ItemsService.get_item(item_id)[:data]
Item.new(item_data)
end

def self.get_items
items_data = ItemsSerice.get_items[:data]
items_data.map do |item_data|
Item.new(item_data)
end
end
end
13 changes: 13 additions & 0 deletions app/facades/merchants_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MerchantsFacade
def self.get_merchants
merchants_data = MerchantsService.get_merchants[:data]
merchants_data.map do |merchant_data|
Merchant.new(merchant_data)
end
end

def self.get_merchant(merchant_id)
merchant_data = MerchantsService.get_merchant(merchant_id)[:data]
Merchant.new(merchant_data)
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

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

def merchant
MerchantsFacade.get_merchant(@merchant_id)
end
end
12 changes: 12 additions & 0 deletions app/poros/merchant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Merchant
attr_reader :id, :name, :items

def initialize(merchant_data)
@id = merchant_data[:id].to_i
@name = merchant_data[:attributes][:name]
end

def items
ItemsFacade.get_merchant_items(@id)
end
end
22 changes: 22 additions & 0 deletions app/services/items_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class ItemsService
def self.get_merchant_items(merchant_id)
response = conn.get("/api/v1/merchants/#{merchant_id}/items")
JSON.parse(response.body, symbolize_names: true)
end

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

def self.get_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
18 changes: 18 additions & 0 deletions app/services/merchants_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class MerchantsService

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

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

private

def self.conn
Faraday.new(url: "http://localhost:3000/")
end
end
Empty file added app/views/items/index.html.erb
Empty file.
Empty file added app/views/items/show.html.erb
Empty file.
5 changes: 5 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<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.name %></h1>
<ul>
<% @merchant.items.each do |item| %>
<li><%= link_to item.name, item_path(item.id) %></li>
<% end %>
</ul>
2 changes: 2 additions & 0 deletions 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
resources :merchants, only: [:index, :show]
resources :items, only: [: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
28 changes: 28 additions & 0 deletions spec/features/merchants/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rails_helper'

RSpec.describe "merchants index page" do

before :each do
@merchants = MerchantsFacade.get_merchants
end

it 'lists all the merchants' do
visit merchants_path

@merchants.each do |merchant|
expect(page).to have_content(merchant.name)
end
end

it 'each merchant name is a link to their show page' do
visit merchants_path

@merchants.each do |merchant|
expect(page).to have_link(merchant.name)
end

click_link @merchants.first.name

expect(current_path).to eq(merchant_path(@merchants.first.id))
end
end
22 changes: 22 additions & 0 deletions spec/features/merchants/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'

RSpec.describe "merchants show page" do

before :each do
@merchant = MerchantsFacade.get_merchants.first
visit merchant_path(@merchant.id)
end

it 'shows the merchants name' do
expect(page).to have_content(@merchant.name)
end

it 'shows the merchant items as links to their show pages' do
@merchant.items.each do |item|
expect(page).to have_link(item.name)
end
first_item = @merchant.items.first
click_link first_item.name
expect(current_path).to eq(item_path(first_item.id))
end
end
1 change: 1 addition & 0 deletions spec/fixtures/files/merchants_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":[{"id":"1","type":"merchant","attributes":{"name":"Schroeder-Jerde"}},{"id":"2","type":"merchant","attributes":{"name":"Klein, Rempel and Jones"}},{"id":"3","type":"merchant","attributes":{"name":"Willms and Sons"}},{"id":"4","type":"merchant","attributes":{"name":"Cummings-Thiel"}},{"id":"5","type":"merchant","attributes":{"name":"Williamson Group"}},{"id":"6","type":"merchant","attributes":{"name":"Williamson Group"}},{"id":"7","type":"merchant","attributes":{"name":"Bernhard-Johns"}},{"id":"8","type":"merchant","attributes":{"name":"Osinski, Pollich and Koelpin"}},{"id":"9","type":"merchant","attributes":{"name":"Hand-Spencer"}},{"id":"10","type":"merchant","attributes":{"name":"Bechtelar, Jones and Stokes"}},{"id":"11","type":"merchant","attributes":{"name":"Pollich and Sons"}},{"id":"12","type":"merchant","attributes":{"name":"Kozey Group"}},{"id":"13","type":"merchant","attributes":{"name":"Tillman Group"}},{"id":"14","type":"merchant","attributes":{"name":"Dicki-Bednar"}},{"id":"15","type":"merchant","attributes":{"name":"Adams-Kovacek"}},{"id":"16","type":"merchant","attributes":{"name":"Bosco, Howe and Davis"}},{"id":"17","type":"merchant","attributes":{"name":"Ullrich-Moen"}},{"id":"18","type":"merchant","attributes":{"name":"Koepp LLC"}},{"id":"19","type":"merchant","attributes":{"name":"Brown Inc"}},{"id":"20","type":"merchant","attributes":{"name":"Schulist, Wilkinson and Leannon"}}]}
7 changes: 7 additions & 0 deletions spec/poros/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe Item do
before :each do

end
end
16 changes: 16 additions & 0 deletions spec/poros/merchant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe Merchant do
before :each do
merchants = JSON.parse(file_fixture("merchants_response.json").read, symbolize_names: true)
merchant_data = merchants[:data].first

@merchant = Merchant.new(merchant_data)
end

it 'exists and has attributes' do
expect(@merchant).to be_instance_of(Merchant)
expect(@merchant.id).to eq(1)
expect(@merchant.name).to eq("Schroeder-Jerde")
end
end
34 changes: 34 additions & 0 deletions spec/services/merchants_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rails_helper'

RSpec.describe MerchantsService do
it 'returns a list of the first 20 merchants' do
merchants = MerchantsService.get_merchants

expect(merchants).to be_a(Hash)
expect(merchants).to have_key(:data)
expect(merchants[:data]).to be_an(Array)

merchants[:data].each do |merchant|
expect(merchant).to have_key(:id)
expect(merchant[:type]).to eq("merchant")
expect(merchant).to have_key(:attributes)
expect(merchant[:attributes]).to have_key(:name)
expect(merchant[:attributes][:name]).to be_a(String)
end
end

it 'returns a single merchant' do
first_merchant_id = MerchantsService.get_merchants[:data].first[:id]

first_merchant = MerchantsService.get_merchant(first_merchant_id)

expect(first_merchant).to be_a(Hash)
expect(first_merchant).to have_key(:data)
expect(first_merchant[:data]).to be_a(Hash)
expect(first_merchant[:data][:id]).to eq(first_merchant_id)
expect(first_merchant[:data][:type]).to eq("merchant")
expect(first_merchant[:data][:attributes]).to be_a(Hash)
expect(first_merchant[:data][:attributes]).to have_key(:name)
expect(first_merchant[:data][:attributes][:name]).to be_a(String)
end
end