Created
November 27, 2023 09:52
-
-
Save DenKey/c696b9f822ea0fa82010c0e3866b68c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileException < Exception | |
end | |
# Connection client | |
class ConnectionClient | |
CLIENT_URL = 'https://our-system.com/connections'.freeze | |
attr_reader :conn | |
def initialize(_options) | |
uri = URI.parse(CLIENT_URL) | |
@conn = Net::HTTP.new(uri.host, uri.port) | |
@conn.use_ssl = true | |
end | |
def request(params) | |
request = Net::HTTP::Post.new(uri) | |
request.set_form_data(params) | |
http.request(request) | |
rescue => e | |
puts e.message | |
end | |
end | |
# Parse and print some amount of connections data | |
class ConnectionService | |
CONNECTIONS_FILE = './connections.json'.freeze | |
STATIONS_PAIR = './station_pairs.json'.freeze | |
def initialize | |
@client = ConnectionClient.new | |
end | |
# print connections data | |
def print_connections | |
station_pairs_object = station_pairs.each_with_object({}) do |item, mem| | |
mem[item[:id]] = item | |
end | |
connections_data.each do |connection| | |
pair = station_pairs.filter { |pair| pair['id'] == connection['pair_id'] }.first | |
pair = station_pairs[connection['pair_id']] | |
if pair | |
params = { | |
"passphrase" => "our-password", | |
"source" => "loopholes", | |
"start_station" => pair['start_station'], | |
"end_station" => pair['end_station'], | |
"start_time" => connection.dig('time', 'start'), | |
"end_time" => connection.dig('time', 'end') | |
} | |
response = @client.request(params) | |
p response.code | |
p response.body | |
end | |
end | |
end | |
private | |
def connections_data | |
file = File.read(CONNECTIONS_FILE) | |
if file.nil? | |
raise FileException, 'fail' | |
end | |
connections = JSON.parse(file) | |
connections['connections'] | |
end | |
def station_pairs | |
file = File.read(STATIONS_PAIR) | |
if file.nil? | |
raise FileException, 'fail' | |
end | |
station_pairs = JSON.parse(file) | |
station_pairs['station_pairs'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment