ts_json_api 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,3 +1,59 @@
1
1
  # Timing and Scoring JSON API
2
2
 
3
- Use this gem to consume the T&S JSON API provided for live & summary race data.
3
+ Use this gem to consume the T&S JSON API provided for live & summary race data.
4
+
5
+ ## Installation
6
+
7
+ Run the following rails generator to setup the gem
8
+
9
+ > rails g ts_json_api:install
10
+
11
+ This generator creates an initializer that you will need to configure for your access token to use the API.
12
+
13
+ ## Usage
14
+
15
+ This gem exposes several methods that map directly to API calls from the T&S team.
16
+
17
+ ### Syntax
18
+
19
+ You are offered two ways to initiate calls. The first is shorthand for the second.
20
+
21
+ ```ruby
22
+ TsJsonApi.weekend 4080
23
+ TsJsonApi::Requestor.weekend 4080
24
+ ```
25
+
26
+ ### Examples
27
+ Example calls follow:
28
+
29
+ ```ruby
30
+
31
+ # Drivers
32
+ TsJsonApi.get_drivers(year, series_id)
33
+ TsJsonApi.driver_summary(race_season, series_id, driver_id=nil)
34
+
35
+ # Live Feed
36
+ TsJsonApi.live_feed
37
+ TsJsonApi.live_flag
38
+ TsJsonApi.live_points
39
+
40
+ # Points
41
+ TsJsonApi.driver_points(race_season, series_id, race_id=nil)
42
+
43
+ # Races
44
+ TsJsonApi.get_race(race_id)
45
+ TsJsonApi.races_in_season(race_season, series_id, driver_id=nil)
46
+
47
+ # Schedule
48
+ TsJsonApi.schedule_for_race(race_id)
49
+ TsJsonApi.schedule_for_season(race_season, series_id)
50
+
51
+ # Series
52
+ TsJsonApi.series
53
+
54
+ # Tracks
55
+ TsJsonApi.get_track(track_id, series_id=nil)
56
+
57
+ # Weekend
58
+ TsJsonApi.weekend(race_id)
59
+ ```
@@ -0,0 +1,17 @@
1
+ module TsJsonApi
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ def create_initializer
6
+ initializer "ts_json_api.rb", <<-CONFIG
7
+ TsJsonApi::Configure.setup do |config|
8
+ config.username = ""
9
+ config.password = ""
10
+ config.logging_enabled = true
11
+ config.server_url = ""
12
+ end
13
+ CONFIG
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ module TsJsonApi
2
+ class Configure
3
+ class << self
4
+
5
+ def username=(u)
6
+ @@username = u
7
+ end
8
+
9
+ def username
10
+ raise "Must provide an username" if @@username.blank?
11
+ @@username
12
+ end
13
+
14
+ def password=(p)
15
+ @@password = p
16
+ end
17
+
18
+ def password
19
+ raise "Must provide a password" if @@password.blank?
20
+ @@password
21
+ end
22
+
23
+ def server_url=(s)
24
+ @@server_url = s
25
+ end
26
+
27
+ def server_url
28
+ raise "Must provide a server URL for the API" if @@server_url.blank?
29
+ @@server_url
30
+ end
31
+
32
+ def logging_enabled=(b)
33
+ @@logging_enabled = b
34
+ end
35
+
36
+ def logging_enabled?
37
+ @@logging_enabled ||= true
38
+ end
39
+
40
+ def setup(&block)
41
+ block.call(self)
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Drivers
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def get_drivers(race_season, series_id)
10
+ send_json_request_and_deliver_response "drivers_#{race_season}_series_#{series_id}", "driver?race_season=#{race_season}&series_id=#{series_id}"
11
+ end
12
+
13
+ def driver_summary(race_season, series_id, driver_id=nil)
14
+ url = "driversummary?race_season=#{race_season}&series_id=#{series_id}"
15
+ url << "&driver_id=#{driver_id}" unless driver_id.blank?
16
+ send_json_request_and_deliver_response "driver_summary_#{race_season}_#{series_id}_#{driver_id}", url
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module LiveFeed
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+
8
+ def live_feed
9
+ send_json_request_and_deliver_response :live_feed, "livefeed"
10
+ end
11
+
12
+ def live_flag
13
+ send_json_request_and_deliver_response :live_flag, "liveflag"
14
+ end
15
+
16
+ def live_points
17
+ send_json_request_and_deliver_response :live_points, "livepoints"
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Logging
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def log(type, url, str)
10
+ return unless Configure.logging_enabled?
11
+ create_dir_if_not_exists
12
+ File.open("#{Rails.root}/log/ts_json_api/#{type}.log", 'w') { |f| f.write "[TIME]: #{Time.now}\n[URL]: #{url}\n\n#{str}"}
13
+ end
14
+
15
+ private
16
+ def create_dir_if_not_exists
17
+ dir = "#{Rails.root}/log/ts_json_api"
18
+ Dir.mkdir dir unless Dir.exists?(dir)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Points
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+
8
+ def driver_points(race_season, series_id, race_id=nil)
9
+
10
+ url = "driverpoints?race_season=#{race_season}&series_id=#{series_id}"
11
+ file_type = "driver_points_#{race_season}_series_#{series_id}"
12
+
13
+ unless race_id.blank?
14
+ url << "&race_id=#{race_id}"
15
+ file_type << "_race_#{race_id}"
16
+ end
17
+
18
+ send_json_request_and_deliver_response file_type, url
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module PrivateMethods
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def send_json_request_and_deliver_response(file_type, partial_url, &block)
10
+ failure = false
11
+ reason = nil
12
+ begin
13
+
14
+ url = "#{Configure.server_url}#{partial_url}"
15
+
16
+ response = api[partial_url].get accept: :json
17
+ json = response.to_str
18
+ json.gsub!(/[^\x20-\x7e]/,'')
19
+
20
+ self.log file_type, url, json
21
+
22
+ json = JSON.parse(json)
23
+
24
+ if json.is_a?(Hash) && json.keys.include?('ResponseCode') && json['ResponseCode'].to_i != 200
25
+ failure = true
26
+ reason = "JSON feed failure with a response code: #{json['ResponseCode']}"
27
+ end
28
+ rescue RestClient::ResourceNotFound => e
29
+ failure = true
30
+ reason = "NASCAR JSON Unavailable"
31
+ rescue ActiveRecord::RecordInvalid => e
32
+ failure = true
33
+ reason = "NASCAR JSON Data prevented a database save as it did not meet validation requirements"
34
+ end
35
+ return block.call(failure, json, reason) if block_given?
36
+ json
37
+ end
38
+
39
+ def default_processor_block
40
+ @block ||= lambda do |failure, json, reason|
41
+ Rails.logger.error "block status: #{failure} -- reason: '#{reason}'"
42
+ failure ? nil : json
43
+ end
44
+ end
45
+
46
+ def api
47
+ @@api ||= RestClient::Resource.new(Configure.server_url, user: Configure.username, password: Configure.password, timeout: 20)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Races
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def get_race(race_id)
10
+ send_json_request_and_deliver_response "race_info_#{race_id}", "races/#{race_id}"
11
+ end
12
+
13
+ def races_in_season(race_season, series_id, driver_id=nil)
14
+ url = "races?race_season=#{race_season}&series_id=#{series_id}"
15
+ url << "&driver_id=#{driver_id}" unless driver_id.blank?
16
+ send_json_request_and_deliver_response "races_#{race_season}_#{series_id}_#{driver_id}", url
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Schedule
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+
8
+ def schedule_for_race(race_id)
9
+ send_json_request_and_deliver_response "schedule_for_race_#{race_id}", "weekendschedule/#{race_id}"
10
+ end
11
+
12
+ def schedule_for_season(race_season, series_id)
13
+ send_json_request_and_deliver_response "schedule_for_season_#{race_season}_series_#{series_id}", "weekendschedule/?race_season=#{race_season}&series_id=#{series_id}"
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Series
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+ def series
8
+ send_json_request_and_deliver_response :series, "series"
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Tracks
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+
8
+ def get_track(track_id, series_id=nil)
9
+
10
+ if series_id.blank?
11
+ url = "track/#{track_id}"
12
+ file_type = "track_info_#{track_id}"
13
+ else
14
+ url = "track/?track_id=#{track_id}&series_id=#{series_id}"
15
+ file_type = "track_info_#{track_id}_series_#{series_id}"
16
+ end
17
+
18
+ send_json_request_and_deliver_response file_type, url
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module UtilityMethods
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def parse_date(str)
10
+ Time.zone.at str.scan(/[0-9]+/)[0].to_i
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module TsJsonApi
2
+ class Requestor
3
+ module Weekend
4
+
5
+ extend ActiveSupport::Concern
6
+ module ClassMethods
7
+
8
+ def weekend(race_id)
9
+ send_json_request_and_deliver_response "weekend_#{race_id}", "weekend/#{race_id}"
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ require 'ts_json_api/requestor/private_methods'
2
+ require 'ts_json_api/requestor/drivers'
3
+ require 'ts_json_api/requestor/live_feed'
4
+ require 'ts_json_api/requestor/logging'
5
+ require 'ts_json_api/requestor/points'
6
+ require 'ts_json_api/requestor/races'
7
+ require 'ts_json_api/requestor/schedule'
8
+ require 'ts_json_api/requestor/series'
9
+ require 'ts_json_api/requestor/tracks'
10
+ require 'ts_json_api/requestor/utility_methods'
11
+ require 'ts_json_api/requestor/weekend'
12
+
13
+ module TsJsonApi
14
+ class Requestor
15
+
16
+ include PrivateMethods
17
+ include Logging
18
+ include UtilityMethods
19
+
20
+ include Drivers
21
+ include LiveFeed
22
+ include Points
23
+ include Races
24
+ include Schedule
25
+ include Series
26
+ include Tracks
27
+ include Weekend
28
+
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module TsJsonApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/ts_json_api.rb CHANGED
@@ -1,2 +1,31 @@
1
+ require 'rest-client'
2
+
1
3
  module TsJsonApi
2
- end
4
+
5
+ autoload :Configure, 'ts_json_api/configure'
6
+ autoload :Requestor, 'ts_json_api/requestor'
7
+
8
+ # Expose all the methods of the Requestor class directly on the module itself
9
+ #
10
+ # Example call:
11
+ # TsJsonApi.weekend
12
+ # is same as
13
+ # TsJsonApi::Requestor.weekend
14
+ #
15
+ class << self
16
+
17
+ def method_missing(method, *args, &block)
18
+ if Requestor.respond_to?(method)
19
+ Requestor.send method, *args
20
+ else
21
+ super
22
+ end
23
+ end
24
+
25
+ def respond_to?(method)
26
+ Requestor.respond_to? method
27
+ end
28
+
29
+ end
30
+
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ts_json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: sqlite3
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +66,21 @@ executables: []
50
66
  extensions: []
51
67
  extra_rdoc_files: []
52
68
  files:
69
+ - lib/generators/ts_json_api/install/install_generator.rb
53
70
  - lib/tasks/ts_json_api_tasks.rake
71
+ - lib/ts_json_api/configure.rb
72
+ - lib/ts_json_api/requestor/drivers.rb
73
+ - lib/ts_json_api/requestor/live_feed.rb
74
+ - lib/ts_json_api/requestor/logging.rb
75
+ - lib/ts_json_api/requestor/points.rb
76
+ - lib/ts_json_api/requestor/private_methods.rb
77
+ - lib/ts_json_api/requestor/races.rb
78
+ - lib/ts_json_api/requestor/schedule.rb
79
+ - lib/ts_json_api/requestor/series.rb
80
+ - lib/ts_json_api/requestor/tracks.rb
81
+ - lib/ts_json_api/requestor/utility_methods.rb
82
+ - lib/ts_json_api/requestor/weekend.rb
83
+ - lib/ts_json_api/requestor.rb
54
84
  - lib/ts_json_api/version.rb
55
85
  - lib/ts_json_api.rb
56
86
  - MIT-LICENSE
@@ -100,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
130
  version: '0'
101
131
  segments:
102
132
  - 0
103
- hash: 1839472513994288648
133
+ hash: 4083627172601262233
104
134
  required_rubygems_version: !ruby/object:Gem::Requirement
105
135
  none: false
106
136
  requirements:
@@ -109,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
139
  version: '0'
110
140
  segments:
111
141
  - 0
112
- hash: 1839472513994288648
142
+ hash: 4083627172601262233
113
143
  requirements: []
114
144
  rubyforge_project:
115
145
  rubygems_version: 1.8.24