worlddb-service 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 / 2013-03-03
2
+
3
+ * Everything is new. First release
@@ -0,0 +1,13 @@
1
+ History.md
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/worlddb/service.rb
6
+ lib/worlddb/service/public/style.css
7
+ lib/worlddb/service/server.rb
8
+ lib/worlddb/service/version.rb
9
+ lib/worlddb/service/views/_debug.erb
10
+ lib/worlddb/service/views/_version.erb
11
+ lib/worlddb/service/views/debug.erb
12
+ lib/worlddb/service/views/index.erb
13
+ lib/worlddb/service/views/layout.erb
@@ -0,0 +1,26 @@
1
+ # world.db.api
2
+
3
+ world.db web service - open world database web service sample // world.db HTTP API sample (JSON / CSV / HTML)
4
+
5
+
6
+ * home :: [github.com/geraldb/world.db.api](https://github.com/geraldb/world.db.api)
7
+ * bugs :: [github.com/geraldb/world.db.api/issues](https://github.com/geraldb/world.db.api/issues)
8
+ * gem :: [rubygems.org/gems/world-service](https://rubygems.org/gems/worlddb-service)
9
+ * rdoc :: [rubydoc.info/gems/world-service](http://rubydoc.info/gems/worlddb-service)
10
+ * forum :: [groups.google.com/group/opensport](https://groups.google.com/group/opensport)
11
+
12
+
13
+ ## Demo
14
+
15
+ Try the `world.db` Web Service demo running
16
+ on Heroku [`worlddb.herokuapp.com/api`](http://worlddb.herokuapp.com/api).
17
+
18
+
19
+ ## License
20
+
21
+ The `world.db.api` scripts are dedicated to the public domain.
22
+ Use it as you please with no restrictions whatsoever.
23
+
24
+ ## Questions? Comments?
25
+
26
+ Send them along to the [Open Sports Database & Friends Forum/Mailing List](http://groups.google.com/group/opensport). Thanks!
@@ -0,0 +1,30 @@
1
+ require 'hoe'
2
+ require './lib/worlddb/service/version.rb'
3
+
4
+
5
+ Hoe.spec 'worlddb-service' do
6
+
7
+ self.version = WorldDB::Service::VERSION
8
+
9
+ self.summary = 'worlddb plugin for web service/HTTP API (JSON/CSV/HTML)'
10
+ self.description = summary
11
+
12
+ self.urls = ['https://github.com/geraldb/world.db.api']
13
+
14
+ self.author = 'Gerald Bauer'
15
+ self.email = 'opensport@googlegroups.com'
16
+
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'History.md'
19
+
20
+ self.extra_deps = [
21
+ ['sinatra', '~> 1.3.5']
22
+ ]
23
+
24
+ self.licenses = ['Public Domain']
25
+
26
+ self.spec_extras = {
27
+ :required_ruby_version => '>= 1.9.2'
28
+ }
29
+
30
+ end
@@ -0,0 +1,45 @@
1
+ ######
2
+ # NB: use rackup to startup Sinatra service (see config.ru)
3
+ #
4
+ # e.g. config.ru:
5
+ # require './boot'
6
+ # run WorldDb::Service::Server
7
+
8
+
9
+ # stdlibs
10
+
11
+ require 'csv'
12
+
13
+ # 3rd party libs/gems
14
+
15
+ require 'sinatra/base'
16
+
17
+ # our own code
18
+
19
+ require 'worlddb/service/version'
20
+
21
+
22
+ module WorldDB::Service
23
+
24
+ def self.banner
25
+ "worlddb-service #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] on Sinatra/#{Sinatra::VERSION} (#{ENV['RACK_ENV']})"
26
+ end
27
+
28
+ ### fix: move to WorldDB
29
+ def self.root
30
+ "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
31
+ end
32
+
33
+ =begin
34
+ def self.config_path
35
+ ## needed? use default db connection?
36
+ "#{root}/config"
37
+ end
38
+ =end
39
+
40
+ end # module WorldDB
41
+
42
+ require 'worlddb/service/server'
43
+
44
+ # say hello
45
+ puts WorldDB::Service.banner
@@ -0,0 +1,36 @@
1
+ body {
2
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; }
3
+
4
+ a {
5
+ color: black;
6
+ text-decoration: none; }
7
+ a:hover {
8
+ color: black;
9
+ background-color: aqua;
10
+ text-decoration: underline; }
11
+ a:visited {
12
+ color: black; }
13
+
14
+ .params {
15
+ color: green;
16
+ font-style: italic;
17
+ font-weight: bold; }
18
+
19
+ pre {
20
+ background-color: #F3F3F3;
21
+ border-bottom: 1px solid #BBBBBB;
22
+ border-top: 1px solid #BBBBBB;
23
+ padding: 4px; }
24
+
25
+
26
+ /**********
27
+ * version / powered by
28
+ */
29
+
30
+ .version {
31
+ text-align: center;
32
+ margin-top: 10px;
33
+ color: grey; }
34
+ .version a, .version span {
35
+ font-size: 12px;
36
+ color: grey; }
@@ -0,0 +1,186 @@
1
+
2
+ module WorldDB::Service
3
+
4
+ class Server < Sinatra::Base
5
+
6
+ PUBLIC_FOLDER = "#{WorldDB::Service.root}/lib/worlddb/service/public"
7
+ VIEWS_FOLDER = "#{WorldDB::Service.root}/lib/worlddb/service/views"
8
+
9
+ puts "[debug] worlddb-service - setting public folder to: #{PUBLIC_FOLDER}"
10
+ puts "[debug] worlddb-service - setting views folder to: #{VIEWS_FOLDER}"
11
+
12
+ set :public_folder, PUBLIC_FOLDER # set up the static dir (with images/js/css inside)
13
+ set :views, VIEWS_FOLDER # set up the views dir
14
+
15
+ set :static, true # set up static file routing
16
+
17
+ #####################
18
+ # Models
19
+
20
+ include WorldDB::Models
21
+
22
+ ##############################################
23
+ # Controllers / Routing / Request Handlers
24
+
25
+ get '/' do
26
+ erb :index
27
+ end
28
+
29
+
30
+ get '/countries.?:format?' do
31
+ # todo: remove .all why? why not?
32
+ data = data_for_countries( Country.by_key.all )
33
+
34
+ format = params[:format] || 'json'
35
+
36
+ render_data( data, format )
37
+ end
38
+
39
+
40
+ get '/cities.?:format?' do
41
+ # todo: remove .all why? why not?
42
+ data = data_for_cities( City.by_key.all )
43
+
44
+ format = params[:format] || 'json'
45
+
46
+ render_data( data, format )
47
+ end
48
+
49
+
50
+ get '/tag/:part' do # e.g. /tag/north_america.txt
51
+
52
+ ##############
53
+ # NB:
54
+ # get '/tag/:key.?:format?'
55
+ # will NOT work - key is greedy and will include the optional format part
56
+
57
+ # split part into slug/key and optional format
58
+ slug, format = params[:part].split('.')
59
+ format ||= 'json'
60
+
61
+ data = data_for_countries( Tag.find_by_slug!( slug ).countries )
62
+
63
+ render_data( data, format )
64
+ end
65
+
66
+
67
+ get '/d*' do
68
+ erb :debug
69
+ end
70
+
71
+
72
+ private
73
+
74
+ =begin
75
+ def csv_content_type
76
+ case request.user_agent
77
+ when /windows/i then 'application/vnd.ms-excel'
78
+ else 'text/csv'
79
+ end
80
+ end
81
+ =end
82
+
83
+ def data_for_cities( cities )
84
+ ## fix: move to model? why? why not?
85
+
86
+ ## add virtual column like kind for metro, metro|city, city, district
87
+
88
+ ## todo add region.title if present
89
+
90
+ data = []
91
+ cities.each do |city|
92
+ data << { key: city.key,
93
+ title: city.title,
94
+ code: city.code,
95
+ pop: city.pop,
96
+ popm: city.popm,
97
+ area: city.area,
98
+ synonyms: city.synonyms,
99
+ country: city.country.title }
100
+ end # each city
101
+ data
102
+ end
103
+
104
+ def data_for_countries( countries )
105
+ ## fix: move to model? why? why not?
106
+
107
+ ## todo: add tags too??
108
+
109
+ data = []
110
+ countries.each do |country|
111
+ data << { key: country.key,
112
+ title: country.title,
113
+ code: country.code,
114
+ pop: country.pop,
115
+ area: country.area,
116
+ synonyms: country.synonyms }
117
+ end # each country
118
+ data
119
+ end
120
+
121
+
122
+ def csv_for_data( data )
123
+ ## :col_sep => "\t"
124
+ ## :col_sep => ";"
125
+
126
+ ## todo: use rec.key for headers/first row
127
+
128
+ csv_string = CSV.generate() do |csv|
129
+ data.each do |rec|
130
+ csv << rec.values
131
+ end
132
+ end
133
+ csv_string
134
+ end # method csv_for_data
135
+
136
+
137
+ def table_for_data( data )
138
+ buf = ""
139
+ buf << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>\n"
140
+ buf << "<table>\n"
141
+ data.each do |rec|
142
+ buf << " <tr>"
143
+ rec.values.each do |value|
144
+ buf << "<td>#{value}</td>"
145
+ end
146
+ buf << "</tr>\n"
147
+ end
148
+ buf << "</table>\n"
149
+ buf << "</body></html>"
150
+ buf
151
+ end # method table_for_data
152
+
153
+ ### helper for json or jsonp response (depending on callback para)
154
+
155
+ def json_or_jsonp( data )
156
+ callback = params.delete('callback')
157
+ response = ''
158
+
159
+ if callback
160
+ content_type :js
161
+ response = "#{callback}(#{data.to_json})"
162
+ else
163
+ content_type :json
164
+ response = data.to_json
165
+ end
166
+
167
+ response
168
+ end
169
+
170
+
171
+ def render_data( data, format )
172
+ if format == 'csv' || format == 'txt'
173
+ headers 'Content-Type' => 'text/plain; charset=utf-8'
174
+ csv_for_data( data )
175
+ elsif format == 'html' || format == 'htm'
176
+ table_for_data( data )
177
+ else
178
+ json_or_jsonp( data )
179
+ end
180
+ end
181
+
182
+
183
+ end # class Server
184
+
185
+
186
+ end # module WorldDB::Service
@@ -0,0 +1,6 @@
1
+
2
+ module WorldDB
3
+ module Service
4
+ VERSION='0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+
2
+ <h3>Debug</h3>
3
+
4
+ <pre>
5
+ request.scheme <%= request.scheme %>
6
+ request.script_name <%= request.script_name %>
7
+ request.path_info <%= request.path_info %>
8
+ request.port <%= request.port %>
9
+ request.request_method <%= request.request_method %>
10
+ request.query_string <%= request.query_string %>
11
+ request.host <%= request.host %>
12
+ request.referrer <%= request.referrer %>
13
+ request.user_agent <%= request.user_agent %>
14
+ request.url <%= request.url %>
15
+ request.path <%= request.path %>
16
+ request.ip <%= request.ip %>
17
+ </pre>
@@ -0,0 +1,7 @@
1
+ <div class='version'>
2
+ <a href="http://groups.google.com/group/opensport">Questions? Comments?</a> |
3
+ <a href="https://github.com/geraldb/world.db">world.db/<%= WorldDB::VERSION %></a>,
4
+ <a href="https://github.com/geraldb/world.db.api">world.db.api/<%= WorldDB::Service::VERSION %></a> -
5
+ <span>Ruby/<%= "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}/#{RUBY_PLATFORM})" %> on</span>
6
+ <span>Sinatra/<%= Sinatra::VERSION %> (<%= ENV['RACK_ENV'] %>)</span>
7
+ </div>
@@ -0,0 +1,2 @@
1
+
2
+ <%= erb :'_debug' %>
@@ -0,0 +1,131 @@
1
+
2
+ <h1>world.db JSON(P) API Demo</h1>
3
+
4
+ <h2>Examples</h2>
5
+
6
+
7
+ <h3>List all countries <code>/countries.(txt|html)</code></h3>
8
+
9
+ <table>
10
+ <td>
11
+ <code>
12
+ <a href='<%= url("/countries") %>'>
13
+ /countries
14
+ </a>
15
+ </code>
16
+ </td>
17
+ <td>
18
+ &nbsp; &nbsp; &nbsp; &nbsp;
19
+ Alternative Formats:
20
+ <code><a href='<%= url("/countries.txt") %>'>.txt</a></code> |
21
+ <code><a href='<%= url("/countries.html") %>'>.html</a></code>
22
+ </td>
23
+ </tr>
24
+ </table>
25
+
26
+ <h3>List all countries for a tag <code>/tag/<span class='params'>:tag</span></code></h3>
27
+
28
+ <table>
29
+ <tr>
30
+ <td>
31
+ <code>
32
+ <a href='<%= url("/tag/europe") %>'>
33
+ /tag/<span class='params'>europe</span>
34
+ </a>
35
+ </code>
36
+ </td>
37
+ <td>
38
+ &nbsp; &nbsp; &nbsp; &nbsp;
39
+ Alternative Formats:
40
+ <code><a href='<%= url("/tag/europe.txt") %>'>.txt</a></code> |
41
+ <code><a href='<%= url("/tag/europe.html") %>'>.html</a></code>
42
+ </td>
43
+ </tr>
44
+
45
+ <tr>
46
+ <td>
47
+ <code>
48
+ <a href='<%= url("/tag/south_america") %>'>
49
+ /tag/<span class='params'>south_america</span>
50
+ </a>
51
+ </code>
52
+ </td>
53
+ <td>
54
+ &nbsp; &nbsp; &nbsp; &nbsp;
55
+ Alternative Formats:
56
+ <code><a href='<%= url("/tag/south_america.txt") %>'>.txt</a></code> |
57
+ <code><a href='<%= url("/tag/south_america.html") %>'>.html</a></code>
58
+ </td>
59
+ </tr>
60
+
61
+ <tr>
62
+ <td>
63
+ <code>
64
+ <a href='<%= url("/tag/un") %>'>
65
+ /tag/<span class='params'>un</span>
66
+ </a>
67
+ </code>
68
+ </td>
69
+ <td>
70
+ &nbsp; &nbsp; &nbsp; &nbsp;
71
+ Alternative Formats:
72
+ <code><a href='<%= url("/tag/un.txt") %>'>.txt</a></code> |
73
+ <code><a href='<%= url("/tag/un.html") %>'>.html</a></code>
74
+ </td>
75
+ </tr>
76
+
77
+ <tr>
78
+ <td>
79
+ <code>
80
+ <a href='<%= url("/tag/eu") %>'>
81
+ /tag/<span class='params'>eu</span>
82
+ </a>
83
+ </code>
84
+ </td>
85
+ <td>
86
+ &nbsp; &nbsp; &nbsp; &nbsp;
87
+ Alternative Formats:
88
+ <code><a href='<%= url("/tag/eu.txt") %>'>.txt</a></code> |
89
+ <code><a href='<%= url("/tag/eu.html") %>'>.html</a></code>
90
+ </td>
91
+ </tr>
92
+
93
+ <tr>
94
+ <td>
95
+ <code>
96
+ <a href='<%= url("/tag/uefa") %>'>
97
+ /tag/<span class='params'>uefa</span>
98
+ </a>
99
+ </code>
100
+ </td>
101
+ <td>
102
+ &nbsp; &nbsp; &nbsp; &nbsp;
103
+ Alternative Formats:
104
+ <code><a href='<%= url("/tag/uefa.txt") %>'>.txt</a></code> |
105
+ <code><a href='<%= url("/tag/uefa.html") %>'>.html</a></code>
106
+ </td>
107
+ </tr>
108
+
109
+ </table>
110
+
111
+ <h3>List all cities <code>/cities.(txt|html)</code></h3>
112
+
113
+ <table>
114
+ <td>
115
+ <code>
116
+ <a href='<%= url("/cities") %>'>
117
+ /cities
118
+ </a>
119
+ </code>
120
+ </td>
121
+ <td>
122
+ &nbsp; &nbsp; &nbsp; &nbsp;
123
+ Alternative Formats:
124
+ <code><a href='<%= url("/cities.txt") %>'>.txt</a></code> |
125
+ <code><a href='<%= url("/cities.html") %>'>.html</a></code>
126
+ </td>
127
+ </tr>
128
+ </table>
129
+
130
+
131
+ <%= erb :'_debug' %>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='UTF-8'>
5
+ <title>world.db web service / HTTP API sample (JSON / CSV / HTML)</title>
6
+ <link href="<%= url('/style.css') %>" rel='stylesheet'>
7
+ </head>
8
+ <body>
9
+
10
+ <%= yield %>
11
+
12
+ <%= erb :'_version' %>
13
+
14
+ </body>
15
+ </html>
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: worlddb-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gerald Bauer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: &78697180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *78697180
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &78696960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.10'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *78696960
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ requirement: &78696740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.3'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *78696740
47
+ description: worlddb plugin for web service/HTTP API (JSON/CSV/HTML)
48
+ email: opensport@googlegroups.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - Manifest.txt
53
+ files:
54
+ - History.md
55
+ - Manifest.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/worlddb/service.rb
59
+ - lib/worlddb/service/public/style.css
60
+ - lib/worlddb/service/server.rb
61
+ - lib/worlddb/service/version.rb
62
+ - lib/worlddb/service/views/_debug.erb
63
+ - lib/worlddb/service/views/_version.erb
64
+ - lib/worlddb/service/views/debug.erb
65
+ - lib/worlddb/service/views/index.erb
66
+ - lib/worlddb/service/views/layout.erb
67
+ homepage: https://github.com/geraldb/world.db.api
68
+ licenses:
69
+ - Public Domain
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --main
73
+ - README.md
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: 1.9.2
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project: worlddb-service
90
+ rubygems_version: 1.8.17
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: worlddb plugin for web service/HTTP API (JSON/CSV/HTML)
94
+ test_files: []