Created
October 17, 2018 21:48
-
-
Save weapp/804abc83dde00a648061474dbe8cde58 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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