Skip to content

Instantly share code, notes, and snippets.

@criess
Last active September 6, 2017 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criess/5cb6e0b764980d72702e380c4aee3fcc to your computer and use it in GitHub Desktop.
Save criess/5cb6e0b764980d72702e380c4aee3fcc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
if !defined?(Rails)
$stdout << "must be executed in rails environment via runner: rails r <script>" << "\n"
exit 1
end
require "tenancy_export_helper"
templates = %w(
app/views/tenancy_export/tenancy_commercial.xml.builder
app/views/tenancy_export/tenancy_retail.xml.builder
app/views/tenancy_export/rent_guarantee/insurance.xml.builder
)
states = {
"TenancyCommercial" => :completed_accepted,
"TenancyRetail" => :completed_not_reservation_accepted,
"RentGuarantee::Insurance" => :completed
}.freeze
module Helpers
def self.included base
base.extend ClassMethods
end
module ClassMethods
include TenancyExportHelper
end
end
class NilClass
def iso8601
Time.now.iso8601
end
end
class Mock
METHOD_BLACKLIST = %w(<< comment!)
def initialize
@mocking_name = ""
@tree = []
@plain_xml = {}
end
def method_missing(symbol, *args, &blk)
if symbol == :"tag!"
symbol = args.first
end
if symbol == :"target!"
@plain_xml["#{mtt_mocking_name}"] ||= []
@plain_xml["#{mtt_mocking_name}"].push("")
return @plain_xml["#{mtt_mocking_name}"].last
end
if block_given?
@mocking_name = "#{mtt_mocking_name}/#{symbol}"
yield
@mocking_name = "#{mtt_mocking_name.split("/")[0..-2].join("/")}"
else
@tree << "#{mtt_mocking_name}/#{symbol}" unless METHOD_BLACKLIST.include?("#{symbol}")
end
self
end
def mtt_tree
@tree
end
def mtt_mocking_name
@mocking_name
end
def mtt_plain_xml
@plain_xml.inject([]) do |tree, (key, arr)|
arr.each do |entry|
tree << "#{key}/#{entry.gsub(/[^<]*(<[^>]+>).*/m,"\\1").scan(/[a-zA-Z0-9:]+/).first}"
end
tree
end.flatten
end
end
class Processor
include Helpers
def self.method_missing(symbol, *args)
if symbol == :render
$stderr << "-- template render with #{args.last || ""}" << "\n"
else
$stderr << "-- unresolved symbol: #{symbol} with #{args.last || ""}" << "\n"
end
end
def self.t(*args)
I18n.t(*args)
end
def self.locale(*args)
I18n.locale(*args)
end
end
templates.each do |template|
content = File.open(template, "rb") do |f|
f.read
end
export_class_name = template.gsub(/^app\/views\/tenancy_export\//,"").gsub(/\.xml\.builder$/,"").camelcase
Processor.class_eval <<-eoruby, __FILE__, __LINE__ + 1
def self.template_for_xml(xml)
@export_obj = #{export_class_name.constantize}.find_by_state(:#{states[export_class_name]}) || (return xml)
#{content}
xml
end
eoruby
$stdout << "#{File.basename template}" << "\n"
$stdout << "#{"+" * (File.basename template).size}" << "\n"
run = Processor.template_for_xml(Mock.new())
$stdout << run.mtt_tree.uniq.sort do |x,y|
if (x.split("/").size <=> y.split("/").size) == 0
x <=> y
else
x.split("/").size <=> y.split("/").size
end
end.join("\n") << "\n"
$stdout << run.mtt_plain_xml.join("\n") << "\n"
end
#!/usr/bin/env ruby
require 'cgi'
require 'json'
require 'yaml'
if ARGV.find("--help")
puts "invocation: curl_places_api.rb --address=\"my example address\" --apikey=EXAMPLE_API_KEY"
puts " --help : shows this message"
puts " --address=[OPTION] : search for address"
puts " --apikey=[OPTION] : set apikey used on location search"
exit 1
end
address = (ARGV.find do |arg|
arg.match(/^--address=.*$/)
end || "").gsub("--address=", "")
api_key = (ARGV.find do |arg|
arg.match(/^--apikey=.*$/)
end || "").gsub("--apikey=", "")
puts address
puts api_key
puts "init ready ..."
target_api = "https://maps.googleapis.com/maps/api/place/textsearch/json"
res = JSON.parse `curl -s "#{target_api}?type=res&query=#{CGI.escape(address)}&lang=en&key=#{api_key}"`
add = nil
if (res["results"].is_a? Array) && (res["results"].size > 0)
add = res["results"].first["formatted_address"]
else
puts "#{address}"
puts "#{res.to_yaml}"
puts "#" * 64
end
#puts "#{sprintf("% 58s",address)} >>> #{sprintf("% 58s",add)}"
<script type="text/javascript">
//<![CDATA[
(function () {
var lookup_table_n = ["start", "tenant", "landlord", "object", "payment", "check"];
var old_steps = ["lessee", "lessor", "apartment", "banking_account", "finalization"];
var params = (function getQueryParams(qs) {
qs = qs.replace(/\+/g, " ");
var params = {},
re = /[?&]?([^=]+)=([^&]*)/g,
tokens;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
})(document.location.search);
// will always redirect if special step_usemax parameter is given
if ( typeof params["step_usemax"] == "string" && (/^n[0-9]$/).test(params["step_usemax"]) ) {
if ( typeof js_plusforta_com != "undefined" && js_plusforta_com.customHistory ) {
var historyUrl = "/" +
lookup_table_n[parseInt((/[0-9]+/).exec(params["step_usemax"])) - 1];
if ( params["cid"] ) {
historyUrl += "?cid=" +
encodeURIComponent(params["cid"]);
}
js_plusforta_com.customHistory.push(
historyUrl
);
}
}
if ( typeof params["step_usemax"] == "string" && (/^a[0-9]$/).test(params["step_usemax"]) ) {
var new_location = document.location.href.replace(
/^[a-z]+:\/\/[^\?]+/,
"https://kautionsfrei.de/onlineantrag-legacy"
).replace(
"step_usemax=" + params["step_usemax"],
"step=" + old_steps[parseInt((/[0-9]+/).exec(params["step_usemax"])) - 1]
);
document.location.href = new_location;
}
})();
//]]>
</script>
#!/usr/bin/env ruby
# mailer transaction_type loader and generator
api_key = begin
Rails.application.config.action_mailer[:maileon_transaction_settings][:api_key]
rescue
$stderr << "unable to load rails with cofiguration; Perhaps you forget to run with 'rails r' command?" << "\n"
exit 1
end
api = Maileon::API.new(api_key, false)
mailer = TenancyMailer
subject = TenancyRetail
mapping = mailer.send(:new).maileon_mapper(subject.where(1).last).tap do |hash|
hash.delete("import")
hash.keys.each do |k|
hash[k] = "string"
end
end
exists_type_for_name = lambda do |name|
types = api.get_all_transaction_types
types = types.select do |entry|
entry = OpenStruct.new entry
entry.name == name
end
(types.is_a? Array) && (types.size > 0)
end
actions_banner = "Operations: (L/List all) (G/Generate new) (E/Exit)" << "\n"
outer_action = nil
until %w(L G E).include? outer_action do
$stdout << actions_banner
outer_action = gets.chomp
case outer_action
when "L"
existing_types = []
api.get_all_transaction_types.each do |entry|
entry = OpenStruct.new entry
existing_types.push(entry)
end
$stdout << "Active Types (existing) Name // ID" << "\n"
$stdout << "#######################" << "\n"
$stdout << "#{existing_types.map do |entry| (entry.name.ljust 48, " ") + "//" + (entry.id.rjust 4, " ") end.join("\n")}" << "\n"
when "G"
$stdout << "key for transaction_type" << "\n"
type_name = gets.strip
$stdout << "Default mapping for type <<#{type_name}>> is #{mapping.to_yaml}" << "\n"
inner_action = nil
until %w(C D).include? inner_action
$stdout << "Mapping Options (D/use defaults) (C/use Custom)" << "\n"
inner_action = gets.chomp
case inner_action
when "D"
api.create_transaction_type type_name, mapping
when "C"
file_mapping = nil
until file_mapping && ( File.exists? file_mapping )
$stdout << "Load mapping from file" << "\n"
file_mapping = gets.strip
end
puts YAML.load(File.read(file_mapping))
res = api.create_transaction_type type_name, YAML.load(File.read(file_mapping))
puts res.to_yaml
else
$stdout << "Mapping Options (D/use defaults) (C/use Custom)" << "\n"
inner_action = gets.chomp
end
end
when "E"
exit
else
# command not understood chomp again please!
$stdout << actions_banner
outer_action = gets.chomp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment