Skip to content

Coding Test Javier Botero #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
/test/version_tmp/
/tmp/

# byebug history
.byebug_history

# Used by dotenv library to load environment variables.
# .env

Expand Down
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gem 'httparty'
gem 'nokogiri'

group :test do
gem 'rspec'
gem "webmock"
end

gem "byebug", "~> 12.0", :groups => [:development, :test]
57 changes: 57 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
bigdecimal (3.1.9)
byebug (12.0.0)
crack (1.0.0)
bigdecimal
rexml
csv (3.3.4)
diff-lcs (1.6.1)
hashdiff (1.1.2)
httparty (0.23.1)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
mini_mime (1.1.5)
mini_portile2 (2.8.8)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
nokogiri (1.18.8)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
public_suffix (6.0.1)
racc (1.8.1)
rexml (3.4.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.3)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
webmock (3.25.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby

DEPENDENCIES
byebug (~> 12.0)
httparty
nokogiri
rspec
webmock

BUNDLED WITH
2.1.4
55 changes: 55 additions & 0 deletions files/body_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'httparty'
require 'nokogiri'
require 'json'

class BodyParser
attr_reader :query, :url, :document, :images

def initialize(url)
@url = url
end

def call
begin
response = HTTParty.get(url)
if response.success?
@document = Nokogiri::HTML(response.body)
@images = { artworks: get_images }.to_json
else
warn "HTTP Error fetching #{url}: #{response.code}"
end
rescue StandardError => e
warn "Error fetching or parsing: #{e.message}"
self.document = nil
self.images = nil
end
end

private

def get_images
candidate_images = document.css('a > img')
extract_data(candidate_images)
end

def extract_data(candidate_images)
candidate_images.map do |image|
anchor = image.parent
next unless anchor && anchor.name == 'a' && anchor['href'].match(/\/search\?/)

div = anchor.css('div').first
next unless div && div.name == 'div'

extensions = div.children
.map(&:text)
.select{ |ext| ext.match(/\d{4}/) }

{
name: image['alt'],
extensions: extensions.any? ? extensions : nil,
link: "https://www.google.com#{anchor['href']}",
image: image['src']
}
end.compact
end
end
Loading