
Apify Unofficial SDK
Pricing
Pay per usage
Go to Apify Store

Apify Unofficial SDK
Under maintenanceApify Unofficial SDK in Other Languanges
0.0 (0)
Pricing
Pay per usage
1
7
1
Last modified
2 years ago
Pricing
Pay per usage
Apify Unofficial SDK in Other Languanges
0.0 (0)
Pricing
Pay per usage
1
7
1
Last modified
2 years ago
FROM ruby:alpine#FROM ruby:slim-bullseye
WORKDIR /app
#RUN apt-get update \#&& apt-get -y install git \ #&& git clone https://github.com/JupriGH/apify-ruby-sdk.git \#&& gem install async-websocket
RUN apk add git --no-cache \&& git clone https://github.com/JupriGH/apify-ruby-sdk.git \&& apk add build-base --no-cache \&& gem install async-websocket sys-proctable mime-types awesome_print colorize \&& apk del build-base \&& rm -rf /var/cache/apk/* \&& df
COPY . .
CMD ruby -W0 src/main.rb
{ "actorSpecification": 1, "name": "my-actor-4", "title": "Empty Python project", "description": "Empty project in Python.", "version": "0.0", "buildTag": "latest", "meta": { "templateId": "python-empty" }, "dockerfile": "./Dockerfile"}
{ "title": "Ruby SDK", "description": "Testing Ruby inputs.", "type": "object", "schemaVersion": 1, "properties": { "secret_message": { "title": "Secret", "type": "string", "editor": "textfield", "isSecret": true, "description": "" }, "proxy_input": { "title": "Proxy", "type": "object", "editor": "proxy", "description": ""} }}
### LESSON 0: line starting with "#" is comment"""This is also (multiline)comments"""### LESSON 1: QUICK LEARN RUBY SYNTAX
# import from relative folder location (.rb file)require_relative '../apify-ruby-sdk/lib/apify_sdk'require_relative 'stackexchange'
# import from installed library, eg: gem install jsonrequire 'json'require 'awesome_print'
# multiline printputs "if you love PHP, JavaScript and Python", "then you'll love Ruby more!"
# when variable first letter is UPPERCASE, its automatically become constant (really ?)GREEN = "🟩"# spread assignmentRED, YELLOW = "🟥", "🟧"# illegal, this is supposed to be constants! (nevermind, assign it anyway, will just show ugly warnings ...)YELLOW = "🟨"# string formattingTRAFFIC_LIGHTS = "#{GREEN}#{RED}#{YELLOW}"
# lambdadef pretty(node) = JSON.pretty_generate(node)def echo(title, *args) = puts "#{TRAFFIC_LIGHTS} #{title}", *args
### LESSON 2: USING SDK
def on_abort data puts "ON_aborting".red, dataenddef on_persistState data puts "ON_persistState".red, dataend def on_systemInfo data puts "ON_systemInfo".red, dataenddef on_dummy data ## testing if event manager catch the errors p undefined_variableend
def main(actor) ### get_env() #echo "ENV :", pretty(actor.get_env) ### get_config() awesome_print echo "CONFIG :" ap actor.config.to_h
### get_input() input = actor.get_input echo "INPUT :", pretty(input)
### create_proxy_configuration() proxy_input = input['proxy_input'] if proxy_input proxy_config = actor.create_proxy_configuration proxy_input new_proxy = proxy_config.new_url echo "PROXYURL :", new_proxy end
### scraping ... echo "Scraping data ..." data = stackexchange(page: 1)["items"]
### push_data() if data actor.push_data data echo "PUSHDATA : #{data.length} rows." end
### open_dataset() dataset = actor.open_dataset info = dataset.get_info echo "DATASET :", pretty(info) ### get_data() data = dataset.get_data # print 2 items start from index 5 echo "DATA :", pretty(data.items[5,2])
# on() echo "Listening platform events ... (try aborting gracefully)" actor.on "aborting", method(:on_abort) actor.on "persistState", method(:on_persistState) actor.on "systemInfo", method(:on_systemInfo) actor.on "systemInfo", method(:on_dummy)
echo "sleeping 10 seconds ..." sleep 10
actor.set_status_message "Another successful mission !!", is_terminal: trueend
### enable loggerlogger = Apify::Loglogger.level = Logger::DEBUGlogger.formatter = Apify::ActorLogFormatter.new
### do the thing!# Apify::Actor.main( method(:main) )
Async do with Apify::Actor do |actor| main actor endend
#------------------------------------------------------------------------------------------------------def stackexchange page: 1 # Define the URL and JSON data url = 'https://api.stackexchange.com/2.2/questions' uri = URI.parse(url) params = { site: 'stackoverflow', page: page } uri.query = URI.encode_www_form params
# start session Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
# Create the request req = Net::HTTP::Get.new uri req['User-Agent'] = 'Chrome'
# Perform the request res = http.request(req) # Handle the response if res.is_a?(Net::HTTPSuccess) puts "Success: #{res.code}" return JSON.parse res.body else raise "HTTP Error: #{res.code}" end endend