Apify Unofficial SDK
Under maintenanceTry for free
No credit card required
View all Actors
This Actor may be unreliable while under maintenance. Would you like to try a similar Actor instead?
See alternative ActorsApify Unofficial SDK
jupri/apify-unofficial-sdk
Try for free
No credit card required
Apify Unofficial SDK in Other Languanges
.actor/Dockerfile
1FROM ruby:alpine
2#FROM ruby:slim-bullseye
3
4WORKDIR /app
5
6#RUN apt-get update \
7#&& apt-get -y install git \
8#&& git clone https://github.com/JupriGH/apify-ruby-sdk.git \
9#&& gem install async-websocket
10
11RUN apk add git --no-cache \
12&& git clone https://github.com/JupriGH/apify-ruby-sdk.git \
13&& apk add build-base --no-cache \
14&& gem install async-websocket sys-proctable mime-types awesome_print colorize \
15&& apk del build-base \
16&& rm -rf /var/cache/apk/* \
17&& df
18
19COPY . .
20
21CMD ruby -W0 src/main.rb
.actor/actor.json
1{
2 "actorSpecification": 1,
3 "name": "my-actor-4",
4 "title": "Empty Python project",
5 "description": "Empty project in Python.",
6 "version": "0.0",
7 "buildTag": "latest",
8 "meta": {
9 "templateId": "python-empty"
10 },
11 "dockerfile": "./Dockerfile"
12}
.actor/input_schema.json
1{
2 "title": "Ruby SDK",
3 "description": "Testing Ruby inputs.",
4 "type": "object",
5 "schemaVersion": 1,
6 "properties": {
7 "secret_message": { "title": "Secret", "type": "string", "editor": "textfield", "isSecret": true, "description": "" },
8 "proxy_input": { "title": "Proxy", "type": "object", "editor": "proxy", "description": ""}
9 }
10}
src/main.rb
1### LESSON 0: line starting with "#" is comment
2"""
3This is also (multiline)comments
4"""
5### LESSON 1: QUICK LEARN RUBY SYNTAX
6
7# import from relative folder location (.rb file)
8require_relative '../apify-ruby-sdk/lib/apify_sdk'
9require_relative 'stackexchange'
10
11# import from installed library, eg: gem install json
12require 'json'
13require 'awesome_print'
14
15# multiline print
16puts "if you love PHP, JavaScript and Python", "then you'll love Ruby more!"
17
18# when variable first letter is UPPERCASE, its automatically become constant (really ?)
19GREEN = "🟩"
20# spread assignment
21RED, YELLOW = "🟥", "🟧"
22# illegal, this is supposed to be constants! (nevermind, assign it anyway, will just show ugly warnings ...)
23YELLOW = "🟨"
24# string formatting
25TRAFFIC_LIGHTS = "#{GREEN}#{RED}#{YELLOW}"
26
27# lambda
28def pretty(node) = JSON.pretty_generate(node)
29def echo(title, *args) = puts "#{TRAFFIC_LIGHTS} #{title}", *args
30
31### LESSON 2: USING SDK
32
33def on_abort data
34 puts "ON_aborting".red, data
35end
36def on_persistState data
37 puts "ON_persistState".red, data
38end
39def on_systemInfo data
40 puts "ON_systemInfo".red, data
41end
42def on_dummy data
43 ## testing if event manager catch the errors
44 p undefined_variable
45end
46
47def main(actor)
48 ### get_env()
49 #echo "ENV :", pretty(actor.get_env)
50
51 ### get_config() awesome_print
52 echo "CONFIG :"
53 ap actor.config.to_h
54
55 ### get_input()
56 input = actor.get_input
57 echo "INPUT :", pretty(input)
58
59 ### create_proxy_configuration()
60 proxy_input = input['proxy_input']
61 if proxy_input
62 proxy_config = actor.create_proxy_configuration proxy_input
63 new_proxy = proxy_config.new_url
64 echo "PROXYURL :", new_proxy
65 end
66
67 ### scraping ...
68 echo "Scraping data ..."
69 data = stackexchange(page: 1)["items"]
70
71 ### push_data()
72 if data
73 actor.push_data data
74 echo "PUSHDATA : #{data.length} rows."
75 end
76
77 ### open_dataset()
78 dataset = actor.open_dataset
79 info = dataset.get_info
80 echo "DATASET :", pretty(info)
81
82 ### get_data()
83 data = dataset.get_data
84
85 # print 2 items start from index 5
86 echo "DATA :", pretty(data.items[5,2])
87
88 # on()
89 echo "Listening platform events ... (try aborting gracefully)"
90
91 actor.on "aborting", method(:on_abort)
92 actor.on "persistState", method(:on_persistState)
93 actor.on "systemInfo", method(:on_systemInfo)
94 actor.on "systemInfo", method(:on_dummy)
95
96 echo "sleeping 10 seconds ..."
97 sleep 10
98
99 actor.set_status_message "Another successful mission !!", is_terminal: true
100end
101
102### enable logger
103logger = Apify::Log
104logger.level = Logger::DEBUG
105logger.formatter = Apify::ActorLogFormatter.new
106
107### do the thing!
108# Apify::Actor.main( method(:main) )
109
110Async do
111 with Apify::Actor do |actor|
112 main actor
113 end
114end
src/stackexchange.rb
1#------------------------------------------------------------------------------------------------------
2def stackexchange page: 1
3 # Define the URL and JSON data
4 url = 'https://api.stackexchange.com/2.2/questions'
5
6 uri = URI.parse(url)
7 params = { site: 'stackoverflow', page: page }
8 uri.query = URI.encode_www_form params
9
10 # start session
11 Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
12
13 # Create the request
14 req = Net::HTTP::Get.new uri
15 req['User-Agent'] = 'Chrome'
16
17 # Perform the request
18 res = http.request(req)
19
20 # Handle the response
21 if res.is_a?(Net::HTTPSuccess)
22 puts "Success: #{res.code}"
23 return JSON.parse res.body
24 else
25 raise "HTTP Error: #{res.code}"
26 end
27 end
28end
Developer
Maintained by Community
Actor metrics
- 3 monthly users
- 100.0% runs succeeded
- days response time
- Created in Oct 2023
- Modified 6 months ago
Categories