diff --git a/Gemfile b/Gemfile index d4fbf7c0..b2ce5d21 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 9c8920e9..391319a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -72,6 +72,17 @@ 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) @@ -79,6 +90,8 @@ GEM 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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -199,13 +216,18 @@ 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 @@ -213,15 +235,17 @@ DEPENDENCIES 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 diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb new file mode 100644 index 00000000..c4f65326 --- /dev/null +++ b/app/controllers/items_controller.rb @@ -0,0 +1,9 @@ +class ItemsController < ApplicationController + def index + @items = ItemsFacade.items + end + + def show + @item = ItemsFacade.item(params[:id]) + end +end \ No newline at end of file diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb new file mode 100644 index 00000000..302f03b3 --- /dev/null +++ b/app/controllers/merchants_controller.rb @@ -0,0 +1,9 @@ +class MerchantsController < ApplicationController + def index + @merchants = MerchantsFacade.merchants + end + + def show + @merchant = MerchantsFacade.merchant(params[:id]) + end +end \ No newline at end of file diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb new file mode 100644 index 00000000..42b76725 --- /dev/null +++ b/app/controllers/welcome_controller.rb @@ -0,0 +1,7 @@ +class WelcomeController < ApplicationController + def index + if params[:search].present? + @merchant = MerchantsFacade.find(params[:search]) + end + end +end \ No newline at end of file diff --git a/app/facades/items_facade.rb b/app/facades/items_facade.rb new file mode 100644 index 00000000..f02a71e6 --- /dev/null +++ b/app/facades/items_facade.rb @@ -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 \ No newline at end of file diff --git a/app/facades/merchants_facade.rb b/app/facades/merchants_facade.rb new file mode 100644 index 00000000..dc171b5c --- /dev/null +++ b/app/facades/merchants_facade.rb @@ -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 \ No newline at end of file diff --git a/app/poros/item.rb b/app/poros/item.rb new file mode 100644 index 00000000..77b138d0 --- /dev/null +++ b/app/poros/item.rb @@ -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 \ No newline at end of file diff --git a/app/poros/merchant.rb b/app/poros/merchant.rb new file mode 100644 index 00000000..b2c0060e --- /dev/null +++ b/app/poros/merchant.rb @@ -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 \ No newline at end of file diff --git a/app/services/items_service.rb b/app/services/items_service.rb new file mode 100644 index 00000000..c5d64a9a --- /dev/null +++ b/app/services/items_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/merchants_service.rb b/app/services/merchants_service.rb new file mode 100644 index 00000000..d6ff3e58 --- /dev/null +++ b/app/services/merchants_service.rb @@ -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 \ No newline at end of file diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb new file mode 100644 index 00000000..b5e8b2da --- /dev/null +++ b/app/views/items/index.html.erb @@ -0,0 +1,9 @@ +
<%= @item.description %>
+Price: <%= @item.unit_price %>
diff --git a/app/views/merchants/index.html.erb b/app/views/merchants/index.html.erb new file mode 100644 index 00000000..47c4a888 --- /dev/null +++ b/app/views/merchants/index.html.erb @@ -0,0 +1,9 @@ +<%= link_to merchant.name, merchant_path(merchant.id) %>
+<%= item.description %>
+Price: <%= item.unit_price %>
+