Skip to content

MVP Attribute Extractor with test file #323

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

Closed
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
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

# Gems here
gem 'nokogiri'
gem 'uri'
gem 'rspec'
gem 'json'
55 changes: 55 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.6.1)
json (2.10.2)
nokogiri (1.18.7-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.18.7-aarch64-linux-musl)
racc (~> 1.4)
nokogiri (1.18.7-arm-linux-gnu)
racc (~> 1.4)
nokogiri (1.18.7-arm-linux-musl)
racc (~> 1.4)
nokogiri (1.18.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.18.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.18.7-x86_64-linux-gnu)
racc (~> 1.4)
nokogiri (1.18.7-x86_64-linux-musl)
racc (~> 1.4)
racc (1.8.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)
uri (1.0.3)

PLATFORMS
aarch64-linux-gnu
aarch64-linux-musl
arm-linux-gnu
arm-linux-musl
arm64-darwin
x86_64-darwin
x86_64-linux-gnu
x86_64-linux-musl

DEPENDENCIES
json
nokogiri
rspec
uri

BUNDLED WITH
2.5.11
37 changes: 37 additions & 0 deletions app/attribute_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "nokogiri"

class AttributeExtractor

def initialize(html_file:)
@doc = Nokogiri::HTML(html_file)
# find the g-loading tag,
icon_tag = @doc.css('g-loading-icon').first
# then go up 1 element to the parent div
container_div = icon_tag.parent
# Inside that div, an a-href tag.
a_tag = container_div.css('a').first
# Get the parent div's class
@class_to_search = a_tag.ancestors('div').first['class']
end


def get_artworks_attributes_hash
@cards = @doc.css(".#{@class_to_search}")
@json_of_cards = []
@cards.each do |card|
link_tag = card.css('a').first
image_tag = card.css('img').first
name_tag = link_tag.css('div').first
extension_tag = name_tag.css('div').count > 1 ? link_tag.css('div') : nil

values = {
name: "#{name_tag.css('div').first.text.strip.gsub(/\s+/, ' ')}",
extentions: extension_tag ? ["#{name_tag.css('div').last.text.strip}"] : nil,
link: "#{link_tag['href']}",
image: "#{image_tag['src']}",
}
@json_of_cards << values
end
@json_of_cards
end
end
28 changes: 28 additions & 0 deletions spec/attribute_extractor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rspec'
require 'json'
require_relative '../app/attribute_extractor'

RSpec.describe AttributeExtractor do
context "with Van Gogh HTML" do
let(:html_file) do
File.open(File.expand_path('../../files/van-gogh-paintings.html', __FILE__))
end
let(:expected_array) do
file_path = File.expand_path('../../files/expected-array.json', __FILE__)
JSON.parse(File.read(file_path), symbolize_names: true)
end

it "finds the files and intiates the extractor" do
expect(html_file).to be_truthy
expect(expected_array).to be_truthy

extractor = AttributeExtractor.new(html_file: html_file)
expect(extractor).to be_truthy
end
it "provides a serialized attributes_hash for cards" do
extractor = AttributeExtractor.new(html_file: html_file)
json_of_artworks = extractor.get_artworks_attributes_hash
expect(json_of_artworks.count).to eq((expected_array.first[1].count))
end
end
end