require "zup/generator/version" require 'net/http' require 'uri' require 'json' require 'yaml' class Zup class Generator attr_accessor :user, :organization attr_accessor :apis def initialize() @api_versions = {} end def login(username, password) uri = URI.parse("https://user-manager-azure.api.zup.me/users/sign_in") request = Net::HTTP::Post.new(uri) request.content_type = "application/json; charset=UTF-8" request["Accept"] = "*/*" request["Referer"] = "https://ole.zup.me/auth/login.html" request["Origin"] = "https://ole.zup.me" request["X-Organization-Slug"] = "ole" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request.body = JSON.dump({ "uid" => username, "password" => password }) req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end if response.code == "201" json = JSON.parse(response.body, :symbolize_names => true) @user = json[:user] @organization = json[:organization] @token = json[:refreshToken] @sessionData = json[:sessionData] return true else return false end end def list_apis @apis.map {|api| {id: api[:id], name: api[:name]}} end def list_versions(api_id) api = @apis.select {|api| api[:id] == api_id}.first api[:versions].map {|version| {id: version[:id], path: version[:path]} } end def load_data @apis = get_apis @apis.each do |api| api[:versions].each do |version| version[:resources] = get_resources_of_api_version(version[:id]) version[:resources].each do |resource| resource[:methods] = get_resource_methods(resource[:id]) resource[:methods].each do |resource_method| resource_method[:metadata] = get_metadata(resource_method[:id]) end end end end end def get_resources_of_api_version(api_version) uri = URI.parse("https://api-manager-azure.api.zup.me/api_versions/#{api_version}/resources") request = Net::HTTP::Get.new(uri) request["X-Session-Data"] = @sessionData request["Pragma"] = "no-cache" request["Origin"] = "https://ole.zup.me" request["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request["Accept"] = "application/json" request["Cache-Control"] = "no-cache" request["Referer"] = "https://ole.zup.me/api-manager/" request["Connection"] = "keep-alive" request["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end return JSON.parse(response.body, :symbolize_names => true) end def get_resource_methods(resource_id) uri = URI.parse("https://api-manager-azure.api.zup.me/resources/#{resource_id}/resource_methods") request = Net::HTTP::Get.new(uri) request["X-Session-Data"] = @sessionData request["Pragma"] = "no-cache" request["Origin"] = "https://ole.zup.me" request["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request["Accept"] = "application/json" request["Cache-Control"] = "no-cache" request["Referer"] = "https://ole.zup.me/api-manager/" request["Connection"] = "keep-alive" request["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end return JSON.parse(response.body, :symbolize_names => true) end def get_metadata(resource_method_id) uri = URI.parse("https://api-manager-azure.api.zup.me/resource_methods/#{resource_method_id}/metadata") request = Net::HTTP::Get.new(uri) request["X-Session-Data"] = @sessionData request["Pragma"] = "no-cache" request["Origin"] = "https://ole.zup.me" request["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request["Accept"] = "application/json" request["Cache-Control"] = "no-cache" request["Referer"] = "https://ole.zup.me/api-manager/" request["Connection"] = "keep-alive" request["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end return nil if response.code == "404" return JSON.parse(response.body, :symbolize_names => true) end def generate_swagger(api_id, version_id) api = @apis.select {|e| e[:id] == api_id}.first version = api[:versions].select {|e| e[:id] == version_id}.first if !api.nil? && !version.nil? content = { swagger: '2.0', info: { title: api[:name], description: api[:description], version: version[:path] }, basePath: version[:path], paths: {} } version[:resources].each do |resource| content[:paths][resource[:path]] = {} resource[:methods].each do |resource_method| content[:paths][resource[:path]][resource_method[:http_verb].downcase] = { summary: resource[:name], consumes: ["application/json"], tags: resource[:path].split('/').select {|e| !e.empty? && !e.include?('{') }, parameters: [], responses: {} } url_params = resource[:path].split('/').select {|e| e.include?('{') }.map { |e| e.tr('{}', '') } url_params.each do |param| content[:paths][resource[:path]][resource_method[:http_verb].downcase][:parameters] << { name: param, in: "path", required: true, type: "string" } end if url_params resource_method[:metadata][:request][:queryParameters].each do |key, param| content[:paths][resource[:path]][resource_method[:http_verb].downcase][:parameters] << { name: key, in: "query", description: param[:description], type: param[:type] } end resource_method[:metadata][:request][:body].each do |type, body| content[:paths][resource[:path]][resource_method[:http_verb].downcase][:parameters] << { in: "body", name: "body", required: true, schema: form_object_at_json(JSON.parse(body[:example], symbolize_names: true)) } end if resource_method[:metadata][:responses] resource_method[:metadata][:responses].each do |status, response| content[:paths][resource[:path]][resource_method[:http_verb].downcase][:responses][status] = {} response[:body].each do |type, body| content[:paths][resource[:path]][resource_method[:http_verb].downcase][:responses][status] = { description: "success", schema: form_object_at_json(JSON.parse(body[:example], symbolize_names: true)) } end if response end else content[:paths][resource[:path]][resource_method[:http_verb].downcase][:responses]["200"] = { description: "success" } end end end # pbcopy content.to_json return content else return nil end end def form_object_at_json(obj) object = { type: "object", properties: parse_properties(obj) } return object end def parse_properties(obj) result = {} obj.keys.each do |key| result[key] = { type: get_type(obj[key]) } if get_type(obj[key]) == "array" if get_type(obj[key][0]) == "object" result[key][:items] = { type: get_type(obj[key][0]), properties: parse_properties(obj[key][0]) } else result[key][:items] = { type: get_type(obj[key][0]) } end end if get_type(obj[key]) == "object" result[key][:properties] = parse_properties(obj[key]) end end result end def pbcopy(input) str = input.to_s IO.popen('pbcopy', 'w') { |f| f << str } str end private def get_type(obj) case obj when String "string" when Integer "integer" when Array "array" when Hash "object" else "string" end end def get_permissions uri = URI.parse("https://api-manager-azure.api.zup.me/team_permissions/by_user") request = Net::HTTP::Get.new(uri) request["X-Session-Data"] = @sessionData request["Pragma"] = "no-cache" request["Origin"] = "https://ole.zup.me" request["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request["Accept"] = "application/json" request["Cache-Control"] = "no-cache" request["Referer"] = "https://ole.zup.me/api-manager/" request["Connection"] = "keep-alive" request["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end return JSON.parse(response.body, :symbolize_names => true) end def get_apis uri = URI.parse("https://api-manager-azure.api.zup.me/apis") request = Net::HTTP::Get.new(uri) request["X-Session-Data"] = @sessionData request["Pragma"] = "no-cache" request["Origin"] = "https://ole.zup.me" request["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4" request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" request["Accept"] = "application/json" request["Cache-Control"] = "no-cache" request["Referer"] = "https://ole.zup.me/api-manager/" request["Connection"] = "keep-alive" request["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end return JSON.parse(response.body, :symbolize_names => true) end end end