Skip to content

Commit

Permalink
faraday CRUD via API
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Apr 16, 2023
1 parent 9ed34f2 commit 37148c8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ group :test do
gem "selenium-webdriver"
gem "webdrivers"
end

gem "faraday", "~> 2.7"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ GEM
irb (>= 1.5.0)
reline (>= 0.3.1)
erubi (1.12.0)
faraday (2.7.4)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.12.0)
Expand Down Expand Up @@ -167,6 +171,7 @@ GEM
reline (0.3.3)
io-console (~> 0.5)
rexml (3.2.5)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
selenium-webdriver (4.8.6)
rexml (~> 3.2, >= 3.2.5)
Expand Down Expand Up @@ -213,6 +218,7 @@ DEPENDENCIES
bootsnap
capybara
debug
faraday (~> 2.7)
importmap-rails
jbuilder
pg (~> 1.1)
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# require 'faraday'
# require 'json'

class HomeController < ApplicationController
# protect_from_forgery with: :null_session

def index
conn = Faraday.new(url: 'http://localhost:3001')

This comment has been minimized.

Copy link
@yshmarov

yshmarov Apr 16, 2023

Author Member

here we are making a request to another app, that is running locally on the port 3001 and that has a scaffold Movies

# index
response = conn.get('/movies.json')

# show
# response = conn.get('/movies/2.json')

# create
# movie_data = { movie: {title: 'Yaro'}}
# response = conn.post('/movies.json') do |request|
# request.headers['Content-Type'] = 'application/json'
# request.body = JSON.generate(movie_data)
# end

# update
# movie_data = { movie: {title: 'Yaro part 2'}}
# response = conn.patch('/movies/11.json') do |request|
# request.headers['Content-Type'] = 'application/json'
# request.body = JSON.generate(movie_data)
# end

# delete
# conn.delete('/movies/2.json')
@body_json = JSON.parse(response.body)
end
end
6 changes: 6 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

<% first_post = OpenStruct.new(@body_json.first) %>
<%#= @body_json.first['title'] %>
<%= first_post.title %>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
get 'home/index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
root "home#index"
end

0 comments on commit 37148c8

Please sign in to comment.