TokiCLI 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +9 -17
  4. data/.rspec +3 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.md +4 -0
  7. data/README.md +99 -163
  8. data/Rakefile +5 -0
  9. data/TokiCLI.gemspec +5 -4
  10. data/bin/toki +2 -2
  11. data/lib/API/helpers.rb +27 -170
  12. data/lib/API/toki_api.rb +227 -0
  13. data/lib/API/toki_db.rb +67 -0
  14. data/lib/TokiCLI.rb +270 -5
  15. data/lib/TokiCLI/adnimport.rb +202 -0
  16. data/lib/TokiCLI/fileops.rb +187 -0
  17. data/lib/TokiCLI/status.rb +49 -37
  18. data/lib/TokiCLI/version.rb +1 -1
  19. data/lib/TokiCLI/view.rb +133 -86
  20. data/lib/TokiServer/.bowerrc +3 -0
  21. data/lib/TokiServer/.gitignore +2 -0
  22. data/lib/TokiServer/Gemfile +1 -0
  23. data/lib/TokiServer/bower.json +0 -0
  24. data/lib/TokiServer/bower_components/fastclick/.bower.json +4 -4
  25. data/lib/TokiServer/bower_components/fastclick/bower.json +1 -1
  26. data/lib/TokiServer/bower_components/fastclick/lib/fastclick.js +33 -2
  27. data/lib/TokiServer/bower_components/foundation/.bower.json +5 -4
  28. data/lib/TokiServer/bower_components/foundation/bower.json +2 -1
  29. data/lib/TokiServer/bower_components/foundation/css/foundation.css +2173 -922
  30. data/lib/TokiServer/bower_components/foundation/css/foundation.css.map +7 -0
  31. data/lib/TokiServer/bower_components/foundation/css/normalize.css +53 -16
  32. data/lib/TokiServer/bower_components/foundation/css/normalize.css.map +7 -0
  33. data/lib/TokiServer/bower_components/foundation/js/foundation.js +782 -542
  34. data/lib/TokiServer/bower_components/foundation/js/foundation.min.js +4 -3
  35. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.abide.js +45 -31
  36. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.accordion.js +12 -6
  37. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.alert.js +5 -5
  38. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.clearing.js +34 -10
  39. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.dropdown.js +83 -29
  40. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.equalizer.js +3 -3
  41. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.interchange.js +25 -12
  42. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.joyride.js +112 -40
  43. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.js +19 -5
  44. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.magellan.js +22 -11
  45. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.offcanvas.js +52 -8
  46. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.orbit.js +133 -271
  47. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.reveal.js +27 -20
  48. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.slider.js +73 -33
  49. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.tab.js +88 -31
  50. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.tooltip.js +7 -5
  51. data/lib/TokiServer/bower_components/foundation/js/foundation/foundation.topbar.js +44 -24
  52. data/lib/TokiServer/bower_components/foundation/js/vendor/fastclick.js +2 -2
  53. data/lib/TokiServer/bower_components/foundation/js/vendor/modernizr.js +2 -2
  54. data/lib/TokiServer/bower_components/foundation/scss/foundation.scss +38 -38
  55. data/lib/TokiServer/bower_components/foundation/scss/foundation/_functions.scss +3 -3
  56. data/lib/TokiServer/bower_components/foundation/scss/foundation/_settings.scss +417 -271
  57. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_accordion.scss +110 -6
  58. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_alert-boxes.scss +2 -2
  59. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_block-grid.scss +2 -2
  60. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_breadcrumbs.scss +8 -3
  61. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_button-groups.scss +99 -9
  62. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_buttons.scss +66 -28
  63. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_clearing.scss +5 -5
  64. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_dropdown-buttons.scss +4 -4
  65. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_dropdown.scss +48 -35
  66. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_forms.scss +104 -32
  67. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_global.scss +48 -30
  68. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_grid.scss +19 -4
  69. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_icon-bar.scss +293 -0
  70. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_joyride.scss +11 -9
  71. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_keystrokes.scss +4 -4
  72. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_labels.scss +4 -2
  73. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_magellan.scss +1 -1
  74. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_offcanvas.scss +193 -35
  75. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_orbit.scss +92 -147
  76. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_pagination.scss +22 -10
  77. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_panels.scss +10 -7
  78. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_pricing-tables.scss +11 -11
  79. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_progress-bars.scss +2 -2
  80. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_range-slider.scss +29 -9
  81. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_reveal.scss +60 -56
  82. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_side-nav.scss +3 -2
  83. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_split-buttons.scss +2 -2
  84. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_sub-nav.scss +2 -2
  85. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_switches.scss +226 -0
  86. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_tables.scss +13 -7
  87. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_tabs.scss +22 -8
  88. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_thumbs.scss +2 -4
  89. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_toolbar.scss +70 -0
  90. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_tooltips.scss +9 -7
  91. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_top-bar.scss +77 -44
  92. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_type.scss +21 -9
  93. data/lib/TokiServer/bower_components/modernizr/.bower.json +4 -4
  94. data/lib/TokiServer/bower_components/modernizr/feature-detects/workers-blobworkers.js +2 -2
  95. data/lib/TokiServer/bower_components/modernizr/grunt.js +1 -1
  96. data/lib/TokiServer/bower_components/modernizr/modernizr.js +2 -2
  97. data/lib/TokiServer/config.rb +0 -0
  98. data/lib/TokiServer/humans.txt +0 -0
  99. data/lib/TokiServer/{itunesicon.rb → itunesicons.rb} +25 -23
  100. data/lib/TokiServer/js/app.js +0 -0
  101. data/lib/TokiServer/public/stylesheets/app.css +2636 -1610
  102. data/lib/TokiServer/robots.txt +0 -0
  103. data/lib/TokiServer/scss/_settings.scss +0 -0
  104. data/lib/TokiServer/scss/app.scss +7 -1
  105. data/lib/TokiServer/tokiserver.rb +239 -244
  106. data/lib/TokiServer/views/activity.erb +42 -0
  107. data/lib/TokiServer/views/apps_total.erb +8 -2
  108. data/lib/TokiServer/views/error.erb +13 -4
  109. data/lib/TokiServer/views/index.erb +36 -27
  110. data/lib/TokiServer/views/logs_total.erb +34 -0
  111. data/spec/TokiCLI_spec.rb +354 -0
  112. data/spec/mock/mock.sqlite3 +0 -0
  113. data/spec/spec_helper.rb +26 -0
  114. metadata +78 -23
  115. data/lib/API/dbapi.rb +0 -488
  116. data/lib/TokiCLI/app.rb +0 -389
  117. data/lib/TokiCLI/authorize.rb +0 -77
  118. data/lib/TokiCLI/export.rb +0 -81
  119. data/lib/TokiCLI/get_channels.rb +0 -22
  120. data/lib/TokiCLI/get_messages.rb +0 -32
  121. data/lib/TokiCLI/import.rb +0 -122
  122. data/lib/TokiCLI/scan.rb +0 -19
  123. data/lib/TokiCLI/search_messages.rb +0 -23
  124. data/lib/TokiServer/README.md +0 -37
  125. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_reveal-new.scss +0 -0
  126. data/lib/TokiServer/bower_components/foundation/scss/foundation/components/_switch.scss +0 -294
  127. data/lib/TokiServer/views/name_log.erb +0 -50
  128. data/lib/TokiServer/views/name_split.erb +0 -37
  129. data/lib/TokiServer/views/name_total.erb +0 -34
File without changes
File without changes
@@ -47,6 +47,11 @@ h1.title {
47
47
  border-bottom: 1px solid $blue;
48
48
  }
49
49
 
50
+ h3.total {
51
+ text-align: center;
52
+ color: $white;
53
+ }
54
+
50
55
  .name {
51
56
  color: $blue;
52
57
  }
@@ -98,5 +103,6 @@ h3.back {
98
103
  }
99
104
 
100
105
  img.icon {
101
- margin-top: 3em;
106
+ margin: 2em auto;
107
+ text-align: center;
102
108
  }
@@ -3,10 +3,48 @@
3
3
 
4
4
  require "sinatra"
5
5
  require "sinatra/assetpack"
6
- require_relative "itunesicon"
6
+ require_relative '../API/toki_api'
7
+ require_relative '../TokiCLI/fileops'
8
+ require_relative "itunesicons"
9
+ require "sinatra/reloader"
7
10
 
8
11
  class TokiServer < Sinatra::Application
9
12
 
13
+ class Getters
14
+
15
+ def initialize(toki, fileops)
16
+ @toki = toki
17
+ @fileops = fileops
18
+ end
19
+
20
+ def bundles_from_name(bundles)
21
+ @fileops.get_bundle_from_name(Array(bundles))
22
+ end
23
+
24
+ end
25
+
26
+ # TOKI INIT
27
+
28
+ fileops = TokiCLI::FileOps.new
29
+ toki = TokiCLI::TokiAPI.new(fileops.db_file, fileops.bundles)
30
+ # Each toki instance has @response, which always contains the last result from a command
31
+ # So you can go with state (updated toki.response) or stateless (create new TokiAPI instances)
32
+ getters = Getters.new(toki, fileops)
33
+ icons = ItunesIcons.new(fileops)
34
+
35
+
36
+ # SINATRA INIT
37
+
38
+ configure :development do
39
+ register Sinatra::Reloader
40
+ end
41
+
42
+ configure do
43
+ file = File.new("#{fileops.log_path}/server.log", 'a+')
44
+ file.sync = true
45
+ use Rack::CommonLogger, file
46
+ end
47
+
10
48
  set :server, %w[thin webrick]
11
49
  set :port, 4567
12
50
  set :root, File.dirname(__FILE__)
@@ -14,6 +52,11 @@ class TokiServer < Sinatra::Application
14
52
  assets do
15
53
  serve '/js', :from => 'js'
16
54
  serve '/bower_components', :from => 'bower_components'
55
+ # serve '/css', :from => 'public/stylesheets'
56
+
57
+ # css :application, '/stylesheets/app.css', [
58
+ # '/stylesheets/app.css'
59
+ # ]
17
60
 
18
61
  js :modernizr, [
19
62
  '/bower_components/modernizr/modernizr.js',
@@ -31,347 +74,299 @@ class TokiServer < Sinatra::Application
31
74
  js_compression :jsmin
32
75
  end
33
76
 
34
- tk = TokiCLI::DBAPI.new
35
- itunesgrabber = ItunesIcon.new
77
+
78
+ # WEB ROUTES
36
79
 
37
80
  get '/' do
38
81
  erb :index
39
82
  end
40
83
 
41
- get '/api/?' do
42
- endpoints = %w{apps/total apps/top apps/top/NUMBER apps/day/DATE apps/before/DATE apps/since/DATE apps/range/DATE1/DATE2 find/APP_NAME/log bundle/BUNDLE_ID/log find/APP_NAME/total bundle/BUNDLE_ID/total find/APP_NAME/before/DATE bundle/BUNDLE_ID/before/DATE find/APP_NAME/since/DATE bundle/BUNDLE_ID/since/DATE find/APP_NAME/range/DATE1/DATE2 bundle/BUNDLE_ID/range/DATE1/DATE2 find/APP_NAME/split/DATE bundle/BUNDLE_ID/split/DATE}
43
- api_endpoints = endpoints.map {|url| "api/#{url}"}
44
- content_type :json
45
- {
46
- meta: {code: 200, message: 'Welcome to the Toki API!'},
47
- data: {
48
- api_endpoints: api_endpoints,
49
- web_endpoints: endpoints
50
- }
51
- }.to_json
52
- end
53
-
54
84
  get '/apps/total/?' do
55
- resp = JSON.parse tk.apps_total
56
- title = 'Totals'
57
- erb :apps_total, locals: {tk: tk, title: title, resp: resp}
85
+ data = JSON.parse(toki.apps_total)['data']
86
+ total = toki.helpers.calc_apps_total(data)
87
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: total }
58
88
  end
59
89
 
60
- get '/api/apps/total/?' do
61
- content_type :json
62
- tk.apps_total
90
+ get '/apps/top/?:number?' do
91
+ data = if params[:number]
92
+ max = params[:number].to_i
93
+ JSON.parse(toki.apps_top(max))['data']
94
+ else
95
+ JSON.parse(toki.apps_top)['data']
96
+ end
97
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: nil }
63
98
  end
64
99
 
65
- get '/apps/top/?' do
66
- resp = JSON.parse tk.apps_top
67
- top resp, tk
100
+ get '/apps/day/?:day?' do
101
+ day = params[:day]
102
+ data = JSON.parse(toki.apps_day(day))['data']
103
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: nil }
68
104
  end
69
105
 
70
- get '/api/apps/top/?' do
71
- content_type :json
72
- tk.apps_top
106
+ get '/apps/range/:day1/?:day2?' do
107
+ day1, day2 = params[:day1], params[:day2]
108
+ data = JSON.parse(toki.apps_range(day1, day2))['data']
109
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: nil }
73
110
  end
74
111
 
75
- get '/apps/top/:number/?' do
76
- number = params[:number].to_i
77
- resp = JSON.parse tk.apps_top number
78
- top resp, tk
112
+ get '/apps/since/?:day?' do
113
+ day = params[:day]
114
+ data = JSON.parse(toki.apps_since(day))['data']
115
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: nil }
79
116
  end
80
117
 
81
- get '/api/apps/top/:number/?' do
82
- number = params[:number].to_i
83
- content_type :json
84
- tk.apps_top number
118
+ get '/apps/before/?:day?' do
119
+ day = params[:day]
120
+ data = JSON.parse(toki.apps_before(day))['data']
121
+ erb :apps_total, locals: { toki: toki, title: 'Total', data: data, total: nil }
85
122
  end
86
123
 
87
- def top resp, tk
88
- if resp['meta']['code'] == 200
89
- title = 'Top'
90
- erb :apps_total, locals: {tk: tk, title: title, resp: resp}
91
- else
92
- erb :error, locals: {resp: resp, message: 'No data for this request'}
93
- end
124
+ get '/activity/?' do
125
+ data = JSON.parse(toki.log_since)['data']
126
+ erb :activity, locals: { toki: toki, title: 'Activity', data: data, total: nil, name: 'Recent activity', icon: nil }
94
127
  end
95
128
 
96
- get '/apps/before/:day/?' do
129
+ get '/activity/day/?:day?' do
97
130
  day = params[:day]
98
- resp = JSON.parse tk.apps_before day
99
- title = "Before #{day}"
100
- if resp['meta']['code'] == 200
101
- erb :apps_total, locals: {tk: tk, title: title, resp: resp}
102
- else
103
- erb :error, locals: {resp: resp, message: 'No data for this request'}
104
- end
131
+ data = JSON.parse(toki.log_day(day))['data']
132
+ total = toki.helpers.calc_logs_total(data)
133
+ erb :activity, locals: { toki: toki, title: 'Activity', data: data, total: total, name: "#{day} -", icon: nil }
105
134
  end
106
135
 
107
- get '/api/apps/before/:day/?' do
136
+ get '/activity/since/?:day?' do
108
137
  day = params[:day]
109
- content_type :json
110
- tk.apps_before day
138
+ data = JSON.parse(toki.log_since(day))['data']
139
+ total = toki.helpers.calc_logs_total(data)
140
+ erb :activity, locals: { toki: toki, title: 'Activity', data: data, total: total, name: "Since #{day} -", icon: nil }
111
141
  end
112
142
 
113
- get '/apps/since/:day/?' do
114
- day = params[:day]
115
- resp = JSON.parse tk.apps_since day
116
- title = "Since #{day}"
117
- if resp['meta']['code'] == 200
118
- erb :apps_total, locals: {tk: tk, title: title, resp: resp}
119
- else
120
- erb :error, locals: {resp: resp, message: 'No data for this request'}
121
- end
143
+ get '/logs/bundle/:bundle/total/?' do
144
+ bundle = params[:bundle]
145
+ data = JSON.parse(toki.bundle_log(bundle))['data']
146
+ name = toki.bundles[bundle]
147
+ icon_url = icons.grab_small(name)
148
+ erb :activity, locals: { toki: toki, title: 'Total', data: data, total: nil, name: "#{bundle} - #{name}", icon: icon_url }
122
149
  end
123
150
 
124
- get '/api/apps/since/:day/?' do
125
- day = params[:day]
126
- content_type :json
127
- tk.apps_since day
151
+ get '/logs/bundle/:bundle/day/?:day?' do
152
+ bundle, day = params[:bundle], params[:day]
153
+ data = JSON.parse(toki.bundle_log_day(bundle, day))['data']
154
+ total = toki.helpers.calc_logs_total(data)
155
+ name = toki.bundles[bundle]
156
+ icon_url = icons.grab_small(name)
157
+ erb :activity, locals: { toki: toki, title: 'Total', data: data, total: total, name: "#{bundle} - #{name} -", icon: icon_url }
128
158
  end
129
159
 
130
- get '/apps/day/:day/?' do
131
- day = params[:day]
132
- resp = JSON.parse tk.apps_day day
133
- if resp['meta']['code'] == 200
134
- erb :apps_total, locals: {tk: tk, title: day, resp: resp}
135
- else
136
- erb :error, locals: {resp: resp, message: 'No data for this request'}
137
- end
160
+ get '/logs/bundle/:bundle/since/?:day?' do
161
+ bundle, day = params[:bundle], params[:day]
162
+ data = JSON.parse(toki.bundle_log_since(bundle, day))['data']
163
+ total = toki.helpers.calc_logs_total(data)
164
+ name = toki.bundles[bundle]
165
+ icon_url = icons.grab_small(name)
166
+ erb :activity, locals: { toki: toki, title: 'Total', data: data, total: total, name: "#{bundle} - #{name} -", icon: icon_url }
138
167
  end
139
168
 
140
- get '/api/apps/day/:day/?' do
141
- day = params[:day]
142
- content_type :json
143
- tk.apps_day day
169
+ get '/logs/bundle/:bundle/before/?:day?' do
170
+ bundle, day = params[:bundle], params[:day]
171
+ data = JSON.parse(toki.bundle_log_before(bundle, day))['data']
172
+ total = toki.helpers.calc_logs_total(data)
173
+ name = toki.bundles[bundle]
174
+ icon_url = icons.grab_small(name)
175
+ erb :activity, locals: { toki: toki, title: 'Total', data: data, total: total, name: "#{bundle} - #{name} -", icon: icon_url }
144
176
  end
145
177
 
146
- get '/apps/range/:day1/:day2/?' do
147
- day1, day2 = params[:day1], params[:day2]
148
- begin
149
- d1, d2 = Time.parse(day1), Time.parse(day2)
150
- rescue ArgumentError => e
151
- redirect '/'
152
- end
153
- if d1 > d2
154
- title = "#{day2} #{day1}"
155
- resp = JSON.parse tk.apps_range day2, day1
156
- else
157
- title = "#{day1} #{day2}"
158
- resp = JSON.parse tk.apps_range day1, day2
159
- end
160
- if resp['meta']['code'] == 200
161
- erb :apps_total, locals: {tk: tk, title: title, resp: resp}
162
- else
163
- erb :error, locals: {resp: resp, message: 'No data for this request'}
164
- end
178
+ get '/logs/bundle/:bundle/range/?:day1/?:day2?' do
179
+ bundle, day1, day2 = params[:bundle], params[:day1], params[:day2]
180
+ data = JSON.parse(toki.bundle_log_range(bundle, day1, day2))['data']
181
+ total = toki.helpers.calc_logs_total(data)
182
+ name = toki.bundles[bundle]
183
+ icon_url = icons.grab_small(name)
184
+ erb :activity, locals: { toki: toki, title: 'Total', data: data, total: total, name: "#{bundle} - #{name} -", icon: icon_url }
165
185
  end
166
186
 
167
- get '/api/apps/range/:day1/:day2/?' do
168
- day1, day2 = params[:day1], params[:day2]
169
- content_type :json
170
- tk.apps_range day1, day2
171
- end
187
+ # /logs/app will be added when I find time to create an interactive page with fields, checks, apps list, etc
172
188
 
173
- get '/find/:name/total/?' do
174
- name = params[:name]
175
- resp = JSON.parse tk.name_total(name)
176
- if resp['meta']['code'] == 200
177
- #@icon_url = itunesgrabber.grab_big(name)
178
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
179
- else
180
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
181
- end
182
- end
183
189
 
184
- get '/api/find/:name/total/?' do
185
- name = params[:name]
186
- content_type :json
187
- tk.name_total name
188
- end
189
190
 
190
- get '/bundle/:bundle/total/?' do
191
- bundle = params[:bundle]
192
- resp = JSON.parse tk.bundle_total bundle
193
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
191
+
192
+ # API ROUTES
193
+
194
+ get '/api/?' do
195
+ content_type :json
196
+ {
197
+ 'meta' => {
198
+ 'code' => 200,
199
+ 'message' => 'Welcome to the Toki API server.'
200
+ },
201
+ 'data' => {
202
+ 'routes' => %w{/api/apps/total /api/apps/top/[number] api/apps/day/(iso8601_date) /api/apps/range/(iso8601_date)/(iso8601_date) /api/apps/since/(iso8601_date) /api/apps/before/(iso8601_date) /api/activity /api/activity/day/(iso8601_date) /api/activity/since/(iso8601_date) /api/logs/bundle/(bundle_id)/total /api/logs/bundle/(bundle_id)/day/(iso8601_date) /api/logs/bundle/(bundle_id)/since/(iso8601_date) /api/logs/bundle/(bundle_id)/before/(iso8601_date) /api/logs/bundle/(bundle_id)/range/(iso8601_date)/(iso8601_date) /api/logs/app/(app_name)/total /api/logs/app/(app_name)/day/(iso8601_date) /api/logs/app/(app_name)/since/(iso8601_date) /api/logs/app/(app_name)/before/(iso8601_date) /api/logs/app/(app_name)/range/(iso8601_date)/(iso8601_date) /api/user /api/bundles}
203
+ }
204
+ }.to_json
194
205
  end
195
206
 
196
- get '/api/bundle/:bundle/total/?' do
197
- bundle = params[:bundle]
207
+ get '/api/apps/total/?' do
198
208
  content_type :json
199
- tk.bundle_total bundle
209
+ toki.apps_total()
200
210
  end
201
211
 
202
- get '/find/:name/since/:day/?' do
203
- name, day = params[:name], params[:day]
204
- resp = JSON.parse tk.name_total_since name, day
205
- if resp['meta']['code'] == 200
206
- #@icon_url = itunesgrabber.grab_big(name)
207
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
212
+ get '/api/apps/top/?:number?' do
213
+ max = params[:number].to_i
214
+ content_type :json
215
+ if params[:number]
216
+ toki.apps_top(max)
208
217
  else
209
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
218
+ toki.apps_top()
210
219
  end
211
220
  end
212
221
 
213
- get '/api/find/:name/since/:day/?' do
214
- name, day = params[:name], params[:day]
222
+ get '/api/apps/day/?:day?' do
223
+ day = params[:day]
215
224
  content_type :json
216
- tk.name_total_since name, day
225
+ toki.apps_day(day)
217
226
  end
218
227
 
219
- get '/bundle/:name/since/:day/?' do
220
- name, day = params[:name], params[:day]
221
- resp = JSON.parse tk.bundle_total_since name, day
222
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
228
+ get '/api/apps/range/:day1/?:day2?' do
229
+ day1, day2 = params[:day1], params[:day2]
230
+ content_type :json
231
+ toki.apps_range(day1, day2)
223
232
  end
224
233
 
225
- get '/api/bundle/:name/since/:day/?' do
226
- name, day = params[:name], params[:day]
234
+ get '/api/apps/since/?:day?' do
235
+ day = params[:day]
227
236
  content_type :json
228
- tk.bundle_total_since name, day
237
+ toki.apps_since(day)
229
238
  end
230
239
 
231
- get '/find/:name/before/:day/?' do
232
- name, day = params[:name], params[:day]
233
- resp = JSON.parse tk.name_total_before name, day
234
- if resp['meta']['code'] == 200
235
- #@icon_url = itunesgrabber.grab_big(name)
236
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
237
- else
238
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
239
- end
240
+ get '/api/apps/before/?:day?' do
241
+ day = params[:day]
242
+ content_type :json
243
+ toki.apps_before(day)
240
244
  end
241
245
 
242
- get '/api/find/:name/before/:day/?' do
243
- name, day = params[:name], params[:day]
246
+ get '/api/activity/?' do
244
247
  content_type :json
245
- tk.name_total_before name, day
248
+ toki.log_since()
246
249
  end
247
250
 
248
- get '/bundle/:name/before/:day/?' do
249
- name, day = params[:name], params[:day]
250
- resp = JSON.parse tk.bundle_total_before name, day
251
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
251
+ get '/api/activity/recent/?' do
252
+ content_type :json
253
+ toki.log_since()
252
254
  end
253
255
 
254
- get '/api/bundle/:name/before/:day/?' do
255
- name, day = params[:name], params[:day]
256
+ get '/api/activity/today/?' do
256
257
  content_type :json
257
- tk.bundle_total_before name, day
258
+ toki.log_since()
258
259
  end
259
260
 
260
- get '/find/:name/range/:day1/:day2/?' do
261
- name, day1, day2 = params[:name], params[:day1], params[:day2]
262
- if Time.parse(day1) > Time.parse(day2)
263
- title = "#{day2} #{day1}"
264
- resp = JSON.parse tk.name_total_range name, day2, day1
265
- else
266
- title = "#{day1} #{day2}"
267
- resp = JSON.parse tk.name_total_range name, day1, day2
268
- end
269
- if resp['meta']['code'] == 200
270
- #@icon_url = itunesgrabber.grab_big(name)
271
- erb :name_total, locals: {tk: tk, title: title, resp: resp}
272
- else
273
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
274
- end
261
+ get '/api/activity/day/?:day?' do
262
+ day = params[:day]
263
+ content_type :json
264
+ toki.log_day(day)
275
265
  end
276
266
 
277
- get '/api/find/:name/range/:day1/:day2/?' do
278
- name, day1, day2 = params[:name], params[:day1], params[:day2]
267
+ get '/api/activity/since/?:day?' do
268
+ day = params[:day]
279
269
  content_type :json
280
- tk.name_total_range name, day1, day2
270
+ toki.log_since(day)
281
271
  end
282
272
 
283
- get '/bundle/:name/range/:day1/:day2/?' do
284
- name, day1, day2 = params[:name], params[:day1], params[:day2]
285
- if Time.parse(day1) > Time.parse(day2)
286
- title = "#{day2} #{day1}"
287
- resp = JSON.parse tk.bundle_total_range name, day2, day1
288
- else
289
- title = "#{day1} #{day2}"
290
- resp = JSON.parse tk.bundle_total_range name, day1, day2
291
- end
292
- erb :name_total, locals: {tk: tk, title: basic_title, resp: resp}
273
+ get '/api/logs/bundle/:bundle/total/?' do
274
+ bundle = params[:bundle]
275
+ content_type :json
276
+ toki.bundle_log(bundle)
293
277
  end
294
278
 
295
- get '/api/bundle/:name/range/:day1/:day2/?' do
296
- name, day1, day2 = params[:name], params[:day1], params[:day2]
279
+ get '/api/logs/bundle/:bundle/day/?:day?' do
280
+ bundle, day = params[:bundle], params[:day]
297
281
  content_type :json
298
- tk.bundle_total_range name, day1, day2
282
+ toki.bundle_log_day(bundle, day)
299
283
  end
300
284
 
301
- get '/find/:name/split/:day/?' do
302
- name, day = params[:name], params[:day]
303
- resp = JSON.parse tk.name_total_split name, day
304
- if resp['meta']['code'] == 200
305
- #@icon_url = itunesgrabber.grab_big(name)
306
- erb :name_split, locals: {tk: tk, title: day, resp: resp}
307
- else
308
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
309
- end
285
+ get '/api/logs/bundle/:bundle/since/?:day?' do
286
+ bundle, day = params[:bundle], params[:day]
287
+ content_type :json
288
+ toki.bundle_log_since(bundle, day)
289
+ end
290
+
291
+ get '/api/logs/bundle/:bundle/before/?:day?' do
292
+ bundle, day = params[:bundle], params[:day]
293
+ content_type :json
294
+ toki.bundle_log_before(bundle, day)
310
295
  end
311
296
 
312
- get '/api/find/:name/split/:day/?' do
313
- name, day = params[:name], params[:day]
297
+ get '/api/logs/bundle/:bundle/range/?:day1/?:day2?' do
298
+ bundle, day1, day2 = params[:bundle], params[:day1], params[:day2]
314
299
  content_type :json
315
- tk.name_total_split name, day
300
+ toki.bundle_log_range(bundle, day1, day2)
316
301
  end
317
302
 
318
- get '/bundle/:name/split/:day/?' do
319
- name, day = params[:name], params[:day]
320
- resp = JSON.parse tk.bundle_total_split name, day
321
- erb :name_split, locals: {tk: tk, title: day, resp: resp}
303
+ get '/api/logs/app/:app/total/?' do
304
+ app = params[:app]
305
+ content_type :json
306
+ candidates = getters.bundles_from_name(app)
307
+ candidates.map {|bundle| toki.bundle_log(bundle)}
322
308
  end
323
309
 
324
- get '/api/bundle/:name/split/:day/?' do
325
- name, day = params[:name], params[:day]
310
+ get '/api/logs/app/:app/day/?:day?' do
311
+ app, day = params[:app], params[:day]
326
312
  content_type :json
327
- tk.bundle_total_split name, day
313
+ candidates = getters.bundles_from_name(app)
314
+ candidates.map {|bundle| toki.bundle_log_day(bundle, day)}
328
315
  end
329
316
 
330
- get '/bundle/:name/log/?' do
331
- name = params[:name]
332
- resp = sort_log JSON.parse(tk.bundle_log(name))
333
- erb :name_log, locals: {tk: tk, title: 'Log', resp: resp}
317
+ get '/api/logs/app/:app/since/?:day?' do
318
+ app, day = params[:app], params[:day]
319
+ content_type :json
320
+ candidates = getters.bundles_from_name(app)
321
+ candidates.map {|bundle| toki.bundle_log_since(bundle, day)}
334
322
  end
335
323
 
336
- get '/api/bundle/:name/log/?' do
337
- name = params[:name]
324
+ get '/api/logs/app/:app/before/?:day?' do
325
+ app, day = params[:app], params[:day]
338
326
  content_type :json
339
- tk.bundle_log name
327
+ candidates = getters.bundles_from_name(app)
328
+ candidates.map {|bundle| toki.bundle_log_before(bundle, day)}
340
329
  end
341
330
 
342
- get '/find/:name/log/?' do
343
- name = params[:name]
344
- resp = sort_log JSON.parse(tk.name_log(name))
345
- if resp['meta']['code'] == 200
346
- #@icon_url = itunesgrabber.grab_big(name)
347
- erb :name_log, locals: {tk: tk, title: 'Log', resp: resp}
348
- else
349
- erb :error, locals: {resp: resp, message: "Unable to find '#{name}'"}
350
- end
331
+ get '/api/logs/app/:app/range/?:day1/?:day2?' do
332
+ app, day1, day2 = params[:app], params[:day1], params[:day2]
333
+ content_type :json
334
+ candidates = getters.bundles_from_name(app)
335
+ candidates.map {|bundle| toki.bundle_log_range(bundle, day1, day2)}
351
336
  end
352
337
 
353
- get '/api/find/:name/log/?' do
354
- name = params[:name]
338
+ get '/api/user/?' do
355
339
  content_type :json
356
- tk.name_log name
340
+ File.read(fileops.user_file)
357
341
  end
358
342
 
359
- def sort_log resp
360
- bucket, net = [], {}
361
- resp['data']['log'].each do |id, content|
362
- bucket << [id, content]
363
- end
364
- bucket.sort_by! {|id, content| content['start']}
365
- bucket.each do |id, content|
366
- net[id] = content
367
- end
368
- resp['data']['log'] = net
369
- resp
343
+ get '/api/bundles/?' do
344
+ content_type :json
345
+ fileops.bundles.to_json
370
346
  end
371
347
 
372
- def basic_title
373
- "Total"
348
+ get '/api/names/?' do
349
+ content_type :json
350
+ fileops.bundles.to_json
351
+ end
352
+
353
+ # ---
354
+
355
+ not_found do
356
+ message = request.env['sinatra.error'].to_s.split('::')[1]
357
+ if request.path_info =~ /\/api/
358
+ content_type :json
359
+ {
360
+ 'meta' => {
361
+ 'code' => 404,
362
+ 'message' => message,
363
+ 'processed_at' => Time.now
364
+ },
365
+ 'data' => request.env
366
+ }.to_json
367
+ else
368
+ erb :error, locals: { title: '404', data: request.env, error: message }
369
+ end
374
370
  end
375
371
 
376
372
  end
377
-