Skip to content

Instantly share code, notes, and snippets.

@prdanelli
Last active March 23, 2023 22:07
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 prdanelli/0a97c9c43a0cabac89cf7f94f4d9a584 to your computer and use it in GitHub Desktop.
Save prdanelli/0a97c9c43a0cabac89cf7f94f4d9a584 to your computer and use it in GitHub Desktop.
Block / Class / Factory / Function processing
require 'nokogiri'
require 'uri'
require 'net/http'
require 'byebug'
require 'json'
# Block example
def requester(url, &block)
uri = URI(url)
res = Net::HTTP.get_response(uri)
return unless res.is_a?(Net::HTTPSuccess)
puts "\n\nProduct title: #{block.call(res)}"
end
requester("https://www.lipsum.com/feed/html") do |result|
result
.then { |http| Nokogiri::HTML(http.body) }
.then { |doc| doc.at("#lipsum").content }
.then { |text| text.split(".").first }
end
requester("https://dummyjson.com/products/1") do |result|
result
.then { |http| JSON.parse(http.body) }
.then { |json| json.dig("title") }
end
requester("https://cdn.searchspring.net/help/feeds/searchspring.xml") do |result|
result
.then { |http| Nokogiri::XML(http.body) }
.then { |doc| doc.xpath("//Name").children.first.text }
end
# Simple Class
class Requester
attr_reader :url, :type
def initialize(url, type)
@url = url
@type = type
end
def perform
return unless understood_type?
puts "\n\nProduct title: #{send(type.to_sym)}"
end
protected
def json
request
.then { |http| JSON.parse(http.body) }
.then { |json| json.dig("title") }
end
def html
request
.then { |http| Nokogiri::HTML(http.body) }
.then { |doc| doc.at("#lipsum").content }
.then { |text| text.split(".").first }
end
def xml
request
.then { |http| Nokogiri::XML(http.body) }
.then { |doc| doc.xpath("//Name").children.first.text }
end
private
def understood_type?
[:xml, :json, :html].include?(type.to_sym)
end
def request
uri = URI(url)
res = Net::HTTP.get_response(uri)
return unless res.is_a?(Net::HTTPSuccess)
res
end
end
puts Requester.new("https://cdn.searchspring.net/help/feeds/searchspring.xml", :xml).perform
puts Requester.new("https://www.lipsum.com/feed/html", :html).perform
puts Requester.new("https://dummyjson.com/products/1", :json).perform
Class Factory example
class RequesterFactory
attr_reader :type
def initialize(type)
@type = type
end
def perform
requester = case type.to_sym
when :xml
XmlRequester
when :json
JsonRequester
when :html
HtmlRequester
end
puts "\n\nProduct title: #{requester.new.perform}"
end
end
class BaseRequester
def perform
raise NotImplementedError
end
protected
def make_request(url)
uri = URI(url)
res = Net::HTTP.get_response(uri)
return unless res.is_a?(Net::HTTPSuccess)
res
end
end
class XmlRequester < BaseRequester
URL = "https://cdn.searchspring.net/help/feeds/searchspring.xml"
def perform
make_request(URL)
.then { |http| Nokogiri::XML(http.body) }
.then { |doc| doc.xpath("//Name").children.first.text }
end
end
class JsonRequester < BaseRequester
URL = "https://dummyjson.com/products/1"
def perform
make_request(URL)
.then { |http| JSON.parse(http.body) }
.then { |json| json.dig("title") }
end
end
class HtmlRequester < BaseRequester
URL = "https://www.lipsum.com/feed/html"
def perform
make_request(URL)
.then { |http| Nokogiri::HTML(http.body) }
.then { |doc| doc.at("#lipsum").content }
.then { |text| text.split(".").first }
end
end
puts RequesterFactory.new(:xml).perform
puts RequesterFactory.new(:html).perform
puts RequesterFactory.new(:json).perform
Bad example
def request_html
uri = URI("https://www.lipsum.com/feed/html")
res = Net::HTTP.get_response(uri)
return unless res.is_a?(Net::HTTPSuccess)
product_title = res
.then { |http| Nokogiri::HTML(http.body) }
.then { |doc| doc.at("#lipsum").content }
.then { |text| text.split(".").first }
puts "\n\nProduct title: #{product_title}"
end
def request_xml
uri = URI("https://cdn.searchspring.net/help/feeds/searchspring.xml")
res = Net::HTTP.get_response(uri)
return unless res.is_a?(Net::HTTPSuccess)
product_title = res
.then { |http| Nokogiri::XML(http.body) }
.then { |doc| doc.xpath("//Name") }
.then { |nodes| nodes.first.text }
puts "\n\nProduct title: #{product_title}"
end
request_xml
request_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment