Skip to content

Instantly share code, notes, and snippets.

@caffeineshock
Created November 11, 2011 11:41
Show Gist options
  • Save caffeineshock/1357815 to your computer and use it in GitHub Desktop.
Save caffeineshock/1357815 to your computer and use it in GitHub Desktop.
Sinatra App, um zwei Web-Services zu orchestrieren.
require 'rubygems'
require 'bundler/setup'
require 'savon'
require 'savon_model'
require 'sinatra'
require 'bank'
require 'location'
get '/' do
erb :form
end
post '/' do
bank = Bank.find_by_blz params[:blz]
if bank.nil? then
"BLZ nicht gefunden!"
else
locations = Location.find_nearest(bank, params[:near])
if locations.nil? then
"Nix gefunden!"
else
erb :results, :locals => {:results => locations }
end
end
end
class Bank
include Savon::Model
document "http://www.thomas-bayer.com/axis2/services/BLZService?wsdl"
actions :get_bank
def self.find_by_blz(blz)
get_bank(:blz => blz).to_hash[:get_bank_response][:details][:bezeichnung]
rescue Savon::SOAP::Fault
nil
end
end
<script language="Javascript" tpye="text/javascript">
$(document).ready(function() {
$('input[type="text"]').click(function () {
if ($(this).val() == "BLZ" || $(this).val() == "Aktueller Ort") {
$(this).val("");
}
});
});
</script>
<form action="/" method="post">
<input type="text" name="blz" value="BLZ" autocomplete="off"/>
<input type="text" name="near" value="Aktueller Ort" autocomplete="off"/>
<input type="submit" value="Los!"/>
</form>
<!DOCTYPE HTML>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Give dem moneys!1</title>
<link rel="stylesheet" type="text/css" href="/style.css" media="all">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
</head>
<body>
<div id="container">
<%= yield %>
</div>
</body>
</html>
class Location
include Savon::Model
@@app_id = "AlYqO1_MdULDoZZ4cVoQUW4y23rPHJ1nJ1bTipH8Ce1vk-17fRvu5_Fy7aVL3Jfr"
endpoint "http://dev.virtualearth.net/webservices/v1/searchservice/searchservice.svc"
namespace "http://dev.virtualearth.net/webservices/v1/search/contracts"
def self.find_nearest(keyword, near)
response = client.request(:con, "Search") do
# Weil die WSDL-Datei nicht korrekt geparst wird (Import)
soap.namespaces["xmlns:com"] = "http://dev.virtualearth.net/webservices/v1/common"
soap.namespaces["xmlns:sear"] = "http://dev.virtualearth.net/webservices/v1/search"
soap.body = {
"con:request" => {
"com:Credentials" => { "com:ApplicationId" => @@app_id },
"sear:StructuredQuery" => { "sear:Keyword" => keyword, "sear:Location" => near },
:order! => ["com:Credentials", "sear:StructuredQuery"] # Weil die Reihenfolge wichtig ist (grrr)
}
}
http.headers["SOAPAction"] = "http://dev.virtualearth.net/webservices/v1/search/contracts/ISearchService/Search"
end
postprocess(response)
rescue Savon::SOAP::Fault
nil
end
private
def self.postprocess(response)
results = response.to_hash[:search_response][:search_result][:result_sets][:search_result_set][:results][:search_result_base].to_a
results.map do |r|
{ :distance => r[:distance], :name => r[:name], :address => r[:address][:formatted_address] }
end
end
end
<script language="Javascript" tpye="text/javascript">
$(document).ready(function() {
$('a').click(function () {
$(this).siblings().fadeIn();
});
});
</script>
<table>
<tr>
<th>Entfernung (km)</th>
<th>Name</th>
<tr>
<% results.each_with_index do |r, i| %>
<tr class="<%= i.even? ? "even" : "" %>">
<td><%= r[:distance] %></td>
<td>
<a href="javascript:;"><%= r[:name] %></a>
<div class="address"><%= r[:address] %></div>
</td>
<tr>
<% end %>
</table>
require 'rubygems'
require 'bundler/setup'
require 'savon'
require 'savon_model'
require 'simplews'
require 'bank'
require 'location'
class ClosestATMWS < SimpleWS
serve :find_closest_atm, %w(blz near),
:blz => :integer, :return => :array do |blz, near|
bank = Bank.find_by_blz params[:blz]
Location.find_nearest(bank, params[:near]).to_xml
end
end
server = ClosestATMWS.new
trap('INT'){
puts "Stopping server"
server.shutdown
}
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment