Skip to content

Instantly share code, notes, and snippets.

@weapp
Created October 17, 2018 21:48
Show Gist options
  • Save weapp/804abc83dde00a648061474dbe8cde58 to your computer and use it in GitHub Desktop.
Save weapp/804abc83dde00a648061474dbe8cde58 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "rack"
gem "foxy", git: "git://github.com/weapp/foxyrb.git"
end
class App
def grade(plate)
url = "http://www.dgt.es/es/seguridad-vial/distintivo-ambiental/index.shtml"
qs = { accion: 1, matriculahd: nil, submit: "Comprobar" }
img = Foxy::Client.new
.eraw(path: url, params: qs.merge(matricula: plate))
.foxy
img &&= img.find(id: "resultadoBusqueda")
img &&= img.find(tagname: "img")
img &&= img.attr("src")
img && img[-5] if img != "/resources/cnr/imgm/icn-pdf.png"
end
def call(env)
if env["REQUEST_METHOD"] == "GET"
data = grade(env["PATH_INFO"][1..-1])
if data
[200, {}, [{ data: data }.to_json]]
else
[404, {}, [{ error: "Not found" }.to_json]]
end
else
[405, {}, ["Method Not Allowed"]]
end
end
end
Rack::Server.start(app: App.new, Port: 3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment