-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
46 lines (35 loc) · 982 Bytes
/
app.rb
File metadata and controls
46 lines (35 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require "nokogiri"
require "httparty"
require "aws-record"
require "digest"
#####
# This Lambda function scrapes a website to get the names of all countries.
# The countries are then written into a DynamoDB table and returned as a json array.
#####
SOURCE = "https://www.scrapethissite.com/pages/simple/"
class Country
include Aws::Record
string_attr :id, hash_key: true
string_attr :name
end
def fetch_countries
response = HTTParty.get(SOURCE)
doc = Nokogiri::HTML5(response.body)
doc.css("#countries .country").map do |row|
row.css(".country-name").first&.content&.strip
end
end
def store_countries(country_names)
country_names.each do |name|
country_id = Digest::SHA256.hexdigest(name)
country = Country.find(id: country_id)
next unless country.nil?
country = Country.new(id: country_id, name: name)
country.save
end
end
def handler(event:, context:)
countries = fetch_countries
store_countries(countries)
countries
end