ts_json_api 0.2.0 → 0.2.1

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
@@ -27,6 +27,7 @@ TsJsonApi::Configure.setup do |config|
27
27
  config.api_version = 2
28
28
  config.logging_enabled = true
29
29
  config.server_url = ""
30
+ config.timestamped_logs = false
30
31
  end
31
32
  ```
32
33
 
@@ -34,6 +35,14 @@ The `username`, `password`, and `server_url` fields are all requried to specify
34
35
 
35
36
  You can disable logging by setting `logging_enabled = false`. Logging will log the raw response from the T&S service into the Rails `tmp/ts_json_api/` directory for you to review.
36
37
 
38
+ If you wish to keep a log file for every transaction with the API, you will want to enabled `timestamped_logs`. This feature will keep a unique file instead of overwriting the previous response from the API every time.
39
+
40
+ If you use this feature, we highly encourage you to run the included rake task:
41
+
42
+ ```shell
43
+ > bundle exec rake ts_json_api:remove_old_log_files
44
+ ```
45
+
37
46
  ## Usage
38
47
 
39
48
  This gem exposes several methods that map directly to API calls from the T&S team.
@@ -10,6 +10,7 @@ TsJsonApi::Configure.setup do |config|
10
10
  config.api_version = 2
11
11
  config.logging_enabled = true
12
12
  config.server_url = ""
13
+ config.timestamped_logs = false
13
14
  end
14
15
  CONFIG
15
16
  end
@@ -1,4 +1,14 @@
1
- # desc "Explaining what the task does"
2
- # task :ts_json_api do
3
- # # Task goes here
4
- # end
1
+ namespace :ts_json_api do
2
+
3
+ desc "will remove timestamped log files older than 24 hours"
4
+ desc remove_old_log_files: :environment do
5
+ old_timestamp = 1.day.ago.to_i
6
+ Dir["#{TsJsonApi::Configure::LOG_FILE_DIRECTORY}/**/*.log"].each do |file|
7
+ if file =~ /_t(\d+)\.log$/
8
+ file_timestamp = Time.at($1.to_i)
9
+ File.delete file if file_timestamp < old_timestamp
10
+ end
11
+ end
12
+ end
13
+
14
+ end
@@ -1,10 +1,15 @@
1
1
  module TsJsonApi
2
2
  class Configure
3
+
4
+ LOG_FILE_DIRECTORY = Rails.root.join('log', 'ts_json_api')
5
+
3
6
  class << self
4
7
 
5
- attr_accessor :api_version, :logging_enabled
8
+ attr_accessor :api_version, :logging_enabled, :timestamped_logs
6
9
  attr_writer :username, :password, :server_url
10
+
7
11
  alias_method :logging_enabled?, :logging_enabled
12
+ alias_method :timestamped_logs?, :timestamped_logs
8
13
 
9
14
  def username
10
15
  raise "Must provide an username" if @username.blank?
@@ -29,6 +34,7 @@ module TsJsonApi
29
34
 
30
35
  @api_version = 2
31
36
  @logging_enabled = true
37
+ @timestamped_logs = false
32
38
 
33
39
  end
34
40
  end
@@ -7,13 +7,13 @@ module TsJsonApi
7
7
  module ClassMethods
8
8
 
9
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}"
10
+ send_json_request_and_deliver_response "#{race_season}/#{series_id}/drivers", "driver?race_season=#{race_season}&series_id=#{series_id}"
11
11
  end
12
12
 
13
13
  def driver_summary(race_season, series_id, driver_id=nil)
14
14
  url = "driversummary?race_season=#{race_season}&series_id=#{series_id}"
15
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
16
+ send_json_request_and_deliver_response "#{race_season}/#{series_id}/#{driver_id}-summary", url
17
17
  end
18
18
 
19
19
  end
@@ -6,15 +6,15 @@ module TsJsonApi
6
6
  module ClassMethods
7
7
 
8
8
  def live_feed
9
- send_json_request_and_deliver_response :live_feed, "livefeed"
9
+ send_json_request_and_deliver_response "live/live_feed", "livefeed"
10
10
  end
11
11
 
12
12
  def live_flag
13
- send_json_request_and_deliver_response :live_flag, "liveflag"
13
+ send_json_request_and_deliver_response "live/live_flag", "liveflag"
14
14
  end
15
15
 
16
16
  def live_points
17
- send_json_request_and_deliver_response :live_points, "livepoints"
17
+ send_json_request_and_deliver_response "live/live_points", "livepoints"
18
18
  end
19
19
 
20
20
  end
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  module TsJsonApi
2
4
  class Requestor
3
5
  module Logging
@@ -6,16 +8,23 @@ module TsJsonApi
6
8
 
7
9
  module ClassMethods
8
10
 
9
- def log(type, url, str)
11
+ def log(path, url, str)
10
12
  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
+
14
+ if Configure.timestamped_logs?
15
+ path << "_t#{Time.now.to_i}.log"
16
+ else
17
+ path << ".log"
18
+ end
19
+
20
+ create_dir_if_not_exists File.dirname(path)
21
+ File.open(Configure::LOG_FILE_DIRECTORY.join(path), 'w') { |f| f.write "[TIME]: #{Time.now}\n[URL]: #{url}\n\n#{str}" }
13
22
  end
14
23
 
15
24
  private
16
- def create_dir_if_not_exists
17
- dir = "#{Rails.root}/log/ts_json_api"
18
- Dir.mkdir dir unless Dir.exists?(dir)
25
+ def create_dir_if_not_exists(path="")
26
+ dir = Configure::LOG_FILE_DIRECTORY.join path
27
+ FileUtils.mkpath dir unless Dir.exists?(dir)
19
28
  end
20
29
 
21
30
  end
@@ -8,14 +8,14 @@ module TsJsonApi
8
8
  def points_per_race(race_season, series_id, race_id=nil)
9
9
 
10
10
  url = "driverpoints?race_season=#{race_season}&series_id=#{series_id}"
11
- file_type = "driver_points_#{race_season}_series_#{series_id}"
11
+ path = "#{race_season}/#{series_id}/points"
12
12
 
13
13
  unless race_id.blank?
14
14
  url << "&race_id=#{race_id}"
15
- file_type << "_race_#{race_id}"
15
+ path.gsub! "points.log", "#{race_id}-points"
16
16
  end
17
17
 
18
- send_json_request_and_deliver_response file_type, url
18
+ send_json_request_and_deliver_response path, url
19
19
 
20
20
  end
21
21
 
@@ -6,14 +6,14 @@ module TsJsonApi
6
6
 
7
7
  module ClassMethods
8
8
 
9
- def send_json_request_and_deliver_response(file_type, partial_url)
9
+ def send_json_request_and_deliver_response(path, partial_url)
10
10
  url = "#{Configure.server_url}#{partial_url}"
11
11
 
12
12
  response = perfom_request(partial_url)
13
13
  json = response.to_str
14
14
  json.gsub!(/[^\x20-\x7e]/,'')
15
15
 
16
- log(file_type, url, json)
16
+ log(path, url, json)
17
17
 
18
18
  JSON.parse(json)
19
19
  end
@@ -7,13 +7,14 @@ module TsJsonApi
7
7
  module ClassMethods
8
8
 
9
9
  def get_race(race_id)
10
- send_json_request_and_deliver_response "race_info_#{race_id}", "races/#{race_id}"
10
+ send_json_request_and_deliver_response "races/#{race_id}", "races/#{race_id}"
11
11
  end
12
12
 
13
13
  def races_in_season(race_season, series_id, driver_id=nil)
14
14
  url = "races?race_season=#{race_season}&series_id=#{series_id}"
15
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
16
+ path = driver_id.nil? ? "schedule" : "#{driver_id}-races"
17
+ send_json_request_and_deliver_response "#{race_season}/#{series_id}/#{path}", url
17
18
  end
18
19
 
19
20
  end
@@ -6,11 +6,11 @@ module TsJsonApi
6
6
  module ClassMethods
7
7
 
8
8
  def schedule_for_race(race_id)
9
- send_json_request_and_deliver_response "schedule_for_race_#{race_id}", "weekendschedule/#{race_id}"
9
+ send_json_request_and_deliver_response "races/#{race_id}-schedule", "weekendschedule/#{race_id}"
10
10
  end
11
11
 
12
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}"
13
+ send_json_request_and_deliver_response "#{race_season}/#{series_id}/schedule", "weekendschedule/?race_season=#{race_season}&series_id=#{series_id}"
14
14
  end
15
15
 
16
16
  end
@@ -5,7 +5,7 @@ module TsJsonApi
5
5
  extend ActiveSupport::Concern
6
6
  module ClassMethods
7
7
  def series
8
- send_json_request_and_deliver_response :series, "series"
8
+ send_json_request_and_deliver_response "series", "series"
9
9
  end
10
10
  end
11
11
 
@@ -9,13 +9,13 @@ module TsJsonApi
9
9
 
10
10
  if series_id.blank?
11
11
  url = "track/#{track_id}"
12
- file_type = "track_info_#{track_id}"
12
+ path = "tracks/#{track_id}"
13
13
  else
14
14
  url = "track/?track_id=#{track_id}&series_id=#{series_id}"
15
- file_type = "track_info_#{track_id}_series_#{series_id}"
15
+ path = "tracks/#{series_id}/#{track_id}"
16
16
  end
17
17
 
18
- send_json_request_and_deliver_response file_type, url
18
+ send_json_request_and_deliver_response path, url
19
19
  end
20
20
 
21
21
  end
@@ -6,7 +6,7 @@ module TsJsonApi
6
6
  module ClassMethods
7
7
 
8
8
  def weekend(race_id)
9
- send_json_request_and_deliver_response "weekend_#{race_id}", "weekend/#{race_id}"
9
+ send_json_request_and_deliver_response "races/#{race_id}-weekend", "weekend/#{race_id}"
10
10
  end
11
11
 
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module TsJsonApi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  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.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-12 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  segments:
119
119
  - 0
120
- hash: -3004862939807243004
120
+ hash: -1078779843776586039
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  none: false
123
123
  requirements:
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: -3004862939807243004
129
+ hash: -1078779843776586039
130
130
  requirements: []
131
131
  rubyforge_project:
132
132
  rubygems_version: 1.8.24