-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·93 lines (73 loc) · 2.14 KB
/
app.rb
File metadata and controls
executable file
·93 lines (73 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Wordsmith Server v1.01
# getwordsmith.co
#
# Copyright (c) Rich Hollis, Jess Eddy 2012
# Available under the BSD and MIT licenses: http://getwordsmith.co/license/
#
require 'sinatra'
require 'yaml'
require 'net/http'
require 'uri'
require 'json'
require './model.rb'
# config
config = YAML.load_file("config.yaml")
# set configuration
configure do
set :app_name => config['app_name']
set :api_key => config['api_key']
set :ga_id => config['ga_id']
end
# echo config to console
puts config
# helper methods
def open(url)
Net::HTTP.get(URI.parse(url))
end
#
# route: /
#
get "/" do
erb :about
end
#
# route: /css/definition.css
#
get "/css/definition.css" do
less :definition
end
#
# route: /:word
#
get '/:word' do
definitions_url = URI.encode("http://api.wordnik.com//v4/word.json/" + params[:word] + "/definitions?includeRelated=false&includeTags=false&limit=200&sourceDictionaries=ahd&useCanonical=false" + "&api_key=" + settings.api_key)
examples_url = URI.encode("http://api.wordnik.com//v4/word.json/" + params[:word] + "/examples?includeDuplicates=false&useCanonical=false" + "&api_key=" + settings.api_key)
@attributions = Array.new
@definitions = Array.new
@examples = Array.new
definitions_content = open(definitions_url)
@definition_json = JSON.parse(definitions_content)
if @definition_json.include? 'type' and @definition_json['type'] == 'error'
erb :definition_error
else
@definition_json.each do |d|
definition = Definition.new(d['partOfSpeech'], d['text'], d['attributionText'])
@definitions.push definition
@attributions.push d['attributionText']
end
examples_content = open(examples_url)
@examples_json = JSON.parse(examples_content)
if @examples_json.include? 'examples'
@examples_json['examples'].each do |e|
example = Example.new(e['title'], e['text'], e['rating'], e['url'])
@examples.push example
end
end
unless @attributions.count > 0
erb :definition_not_found
else
erb :definition, :layout => :layout
end
end
end