Created
February 21, 2012 08:31
-
-
Save makaroni4/1875140 to your computer and use it in GitHub Desktop.
Dictionary sinanra app based on Bing API
This file contains 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
require './translate_app' | |
run Sinatra::Application |
This file contains 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
source 'http://rubygems.org' | |
gem 'sinatra' | |
gem 'haml' |
This file contains 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
- # this file should be on /views folder | |
%html | |
%head | |
%title Simple translation app | |
%body | |
#header | |
%h1 EN-RU translation app | |
- if translation | |
%h2= "#{word} - #{translation}" | |
- elsif word | |
%h2 No translation found | |
#content | |
%form{ :action => "" } | |
%label{ :for => "body"} Enter word: | |
%input{ :name => "body" } | |
%input{ :type => "submit", :value => "Translate" } |
This file contains 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
require 'net/http' | |
require 'uri' | |
class Hash | |
def to_uri | |
to_a.map{ |p| p.join('=') }.join('&') | |
end | |
end | |
class TranslateAPI | |
URL = "http://api.bing.net/json.aspx" | |
PARAMS = { | |
# get your AppId here: http://www.bing.com/toolbox/webmaster/, this one is fake | |
:AppId => "WEFIUN23015K1J24K23U2O3U5N2O3N5K2J3N", | |
:Sources => :Translation, | |
:Version => :"2.2", | |
:"Translation.SourceLanguage" => :en, | |
:"Translation.TargetLanguage" => :ru | |
} | |
def self.translate(query) | |
params = {:Query => query} | |
url = '' | |
url << URL | |
url << '?' | |
url << PARAMS.to_uri | |
url << '&' | |
url << params.to_uri | |
puts url | |
data = Net::HTTP.get(URI(url)) | |
data.scan(/\{\"TranslatedTerm\"\:\"(.+)\"\}/).flatten.first | |
end | |
end |
This file contains 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
require "sinatra" | |
require 'haml' | |
require "./translate_api" | |
set :views, File.dirname(__FILE__) + "/views" | |
get "/" do | |
if @word = params[:body] | |
@translation = TranslateAPI.translate(@word) | |
end | |
haml :index, :locals => {:word => @word, :translation => @translation} | |
end | |
# this route is for load testing with blitz.io | |
get "/mu-ec30996a-59e79091-ba72de54-819405ca" do | |
'42' | |
end | |
not_found do | |
"Ooops ta dam" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment