Skip to content

Instantly share code, notes, and snippets.

@linqueta
Last active December 23, 2019 11:54
Show Gist options
  • Save linqueta/a1ebe9bb598d2a66556019502e1eb433 to your computer and use it in GitHub Desktop.
Save linqueta/a1ebe9bb598d2a66556019502e1eb433 to your computer and use it in GitHub Desktop.
Integrating with JsonPlaceHolder API and Go Rest API - Without Services and Request Options
# GUIDE: https://jsonplaceholder.typicode.com/guide.html
# GUIDE: https://gorest.co.in/
require 'eezee'
require 'post'
require 'user'
post_id = JsonPlaceHolder::Post.create!(
title: 'My new post',
body: 'It is my new post',
userId: 1
).body[:id]
post = JsonPlaceHolder::Post.find!(100).body
post = JsonPlaceHolder::Post.update!(
post[:id],
title: 'My new post (Updated)',
body: 'It is my new post (Updated)'
)
JsonPlaceHolder::Post.destroy!(post_id)
JsonPlaceHolder::Post.index!
GoRest::User.create!(
first_name: 'Linqueta',
last_name: 'Lincoln',
email: "linqueta_#{Random.rand(10..1000)}@linqueta.com",
gender: 'male'
)
module JsonPlaceHolder; end
module JsonPlaceHolder::Post
extend Eezee::Client
module_function
URL = 'jsonplaceholder.typicode.com'
PATH = 'posts/:post_id'
PROTOCOL = :https
HEADERS = { 'Content-Type' => 'application/json' }
def index!
get(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS)
end
def find!(id)
get(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS, params: { post_id: id })
end
def create!(payload)
post(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS, payload: payload)
end
def update!(id, payload)
put(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS, params: { post_id: id }, payload: payload)
end
def destroy!(id)
delete(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS, params: { post_id: id })
end
end
module GoRest; end
module GoRest::User
extend Eezee::Client
module_function
URL = 'gorest.co.in/public-api'
PATH = 'users/:user_id'
PROTOCOL = :https
HEADERS = {
'Content-Type' => 'application/json',
Authorization: 'Bearer Kz79g8huqeRtFjPsd-P2Th095S1KjHoUbykT'
}
def create!(payload)
post(url: URL, path: PATH, protocol: PROTOCOL, headers: HEADERS, payload: payload, raise_error: true, logger: true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment