Skip to content

Instantly share code, notes, and snippets.

@jvidalba1
Created November 29, 2023 21:01
Show Gist options
  • Save jvidalba1/de834cfe765fc24efde6beeb2077c655 to your computer and use it in GitHub Desktop.
Save jvidalba1/de834cfe765fc24efde6beeb2077c655 to your computer and use it in GitHub Desktop.
basic concept test for api integratio
## Define implmentation for an API call to /products
# base url http://127.0.0.1:8181/products
# Product: title, description, price, manufacturer(optional)
# show a message This product #{title} with #{price}, description #{}
# with no manufacturer or with the manufacturer #{manufacturer}
require 'json'
class Product
attr_accessor :title, :price, :description, :manufacturer
BASE_URL = '/products'
def initialize(title, price, description, manufacturer="")
@title = title
@price = price
@description = description
@manufacturer = manufacturer
end
def self.fetch
request = AppApiRequest.new
raw_products = request.get(BASE_URL)
products = FactoryProduct.call(raw_products)
products
end
end
class FactoryProduct
def self.call(raw_products)
iterate_objects(raw_products)
end
def self.iterate_objects(raw)
raw.map { |k,y| Product.new(raw[:title], raw[:price], raw[:description], raw[:manufacturer]) }
rescue
#ToDo
end
end
class AppApiRequest
BASE_URL = "http://127.0.0.1:8181"
def initialize
@external_api_inst = ExternalAPI.new
end
def get(url)
parse(@external_api_inst.call)
end
def parse(json)
JSON.parse(json)
end
end
class ExternalAPI
def call
{ title: "Oelo1", price: 12, description: "sisarras" }
{
title: "Oelo2",
price: 11,
description: "sisarras 2",
manufacturer: "BCompany"
}.to_json
end
end
p Product.fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment