Skip to content

Instantly share code, notes, and snippets.

@craigjbass
Last active May 30, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigjbass/5f90eef7b2b17431fb12fb381298d04b to your computer and use it in GitHub Desktop.
Save craigjbass/5f90eef7b2b17431fb12fb381298d04b to your computer and use it in GitHub Desktop.
class HTTPProductGateway
def find_by(id:)
@id = id
product_for(decode(fetch_product_from_api))
end
private
def product_for(product_payload)
Product.new(name: product_payload[:name], description: product_payload[:description])
end
def fetch_product_from_api
HTTP.get(uri).response.body
end
def decode(api_product)
JSON.decode(api_product)
end
def uri
"https://example.com/api/products/#{@id}"
end
end
class HTTPProductGateway
def find_by(id:)
product_for(decode(fetch_product_from_api(id)))
end
private
def product_for(product_payload)
Domain::Product.new(name: product_payload[:name], description: product_payload[:description])
end
def fetch_product_from_api(id)
HTTP.get(uri(id)).response.body
end
def decode(api_product)
JSON.decode(api_product)
end
def uri(id)
"https://example.com/api/products/#{id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment