ts_json_api 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -113,4 +113,27 @@ rescue TsJsonApi::AccessTokenRefused
113
113
 
114
114
  rescue TsJsonApi::Exception => e
115
115
  puts "some exception bubbled up from RestClient: #{e}, or a catch-all for all TsJsonApi exceptions"
116
- ```
116
+ ```
117
+
118
+ ### Logging
119
+
120
+ There are settings mentioned above to enable logging so that the gem will log files automatically when a request is made. However, there is also a simple API to read and utilize the information stored in the log files. For example, to get the contents of the response from the most recent `TsJsonApi.series` call that was made:
121
+
122
+ ```ruby
123
+ f = TsJsonApi::Logging::File.new relative_path "series.log", readonly: true
124
+ f.contents[:content]
125
+ # => Hash/Array, actual JSON response made earlier
126
+ f.contents[:time]
127
+ # => Time object
128
+ f.contents[:url]
129
+ # => "http://server.url/api/series"
130
+ ```
131
+
132
+ If you have the absolute path to the file:
133
+
134
+ ```ruby
135
+ f = TsJsonApi::Logging::File.new path: my_path, readonly: true
136
+ ```
137
+
138
+ ## Testing
139
+ The test suite is written in rpsec, so just run `rake spec` to see the status of the tests.
data/lib/ts_json_api.rb CHANGED
@@ -5,6 +5,7 @@ require 'ts_json_api/railtie'
5
5
  module TsJsonApi
6
6
 
7
7
  autoload :Configure, 'ts_json_api/configure'
8
+ autoload :Logging, 'ts_json_api/logging'
8
9
  autoload :Requestor, 'ts_json_api/requestor'
9
10
 
10
11
  # Expose all the methods of the Requestor class directly on the module itself
@@ -0,0 +1,5 @@
1
+ module TsJsonApi
2
+ module Logging
3
+ autoload :File, 'ts_json_api/logging/file'
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ module TsJsonApi
2
+ module Logging
3
+ class File
4
+
5
+ attr_reader :path, :options
6
+
7
+ def initialize(options={})
8
+
9
+ @options = options
10
+ @url = options[:url]
11
+
12
+ relative_path = options[:relative_path] || ''
13
+ if relative_path.blank?
14
+ @path = options[:path] unless options[:path].blank?
15
+ else
16
+ unless relative_path.end_with? '.log'
17
+
18
+ relative_path.gsub! ".log", ''
19
+ if Configure.timestamped_logs?
20
+ relative_path << "_t#{Time.now.to_i}.log"
21
+ else
22
+ relative_path << ".log"
23
+ end
24
+
25
+ end
26
+
27
+ @path = Configure::LOG_FILE_DIRECTORY.join(relative_path)
28
+ end
29
+
30
+ raise ArgumentError, 'need help finding a path!' if @path.blank?
31
+
32
+ end
33
+
34
+ def write(content)
35
+ raise 'Cannot write to log file if opened as readonly' if @options[:readonly]
36
+ create_dir_if_not_exists
37
+ ::File.open(@path, 'w') { |f| f.write content_string(content) }
38
+ end
39
+
40
+ def contents
41
+ raise 'Only available when reading a file' unless @options[:readonly]
42
+ json = JSON.parse(IO.read path).symbolize_keys
43
+ json[:content] = JSON.parse(json[:content])
44
+ json
45
+ end
46
+
47
+ private
48
+
49
+ def content_string(str)
50
+ { time: Time.now, url: options[:url], content: str }.to_json
51
+ end
52
+
53
+ def create_dir_if_not_exists
54
+ dir = ::File.dirname(path)
55
+ FileUtils.mkpath dir unless Dir.exists?(dir)
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -1,21 +1,17 @@
1
1
  require 'ts_json_api/requestor/private_methods'
2
2
  require 'ts_json_api/requestor/drivers'
3
3
  require 'ts_json_api/requestor/live_feed'
4
- require 'ts_json_api/requestor/logging'
5
4
  require 'ts_json_api/requestor/points'
6
5
  require 'ts_json_api/requestor/races'
7
6
  require 'ts_json_api/requestor/schedule'
8
7
  require 'ts_json_api/requestor/series'
9
8
  require 'ts_json_api/requestor/tracks'
10
- require 'ts_json_api/requestor/utility_methods'
11
9
  require 'ts_json_api/requestor/weekend'
12
10
 
13
11
  module TsJsonApi
14
12
  class Requestor
15
13
 
16
14
  include PrivateMethods
17
- include Logging
18
- include UtilityMethods
19
15
 
20
16
  include Drivers
21
17
  include LiveFeed
@@ -45,6 +45,11 @@ module TsJsonApi
45
45
  end
46
46
  end
47
47
 
48
+ def log(path, url, str)
49
+ f = Logging::File.new(relative_path: path, url: url)
50
+ f.write str
51
+ end
52
+
48
53
  end
49
54
 
50
55
  end
@@ -1,3 +1,3 @@
1
1
  module TsJsonApi
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -2,61 +2,47 @@ require 'spec_helper'
2
2
 
3
3
  describe TsJsonApi::Configure do
4
4
 
5
- context "Throw exceptions when values are not set" do
5
+ context "Throw exceptions when values are not set" do
6
6
 
7
- it "should raise exception when accessing blank username" do
8
- expect { TsJsonApi::Configure.username }.to raise_error(RuntimeError, "Must provide an username")
9
- end
7
+ it "should raise exception when accessing blank username" do
8
+ expect { TsJsonApi::Configure.username }.to raise_error(RuntimeError, "Must provide an username")
9
+ end
10
10
 
11
- it "should raise exception when accessing blank password" do
12
- expect { TsJsonApi::Configure.password }.to raise_error(RuntimeError, "Must provide a password")
13
- end
11
+ it "should raise exception when accessing blank password" do
12
+ expect { TsJsonApi::Configure.password }.to raise_error(RuntimeError, "Must provide a password")
13
+ end
14
14
 
15
- it "should raise exception when accessing blank server_url" do
16
- expect { TsJsonApi::Configure.server_url }.to raise_error(RuntimeError, "Must provide a server URL for the API")
17
- end
15
+ it "should raise exception when accessing blank server_url" do
16
+ expect { TsJsonApi::Configure.server_url }.to raise_error(RuntimeError, "Must provide a server URL for the API")
17
+ end
18
18
 
19
- end
19
+ end
20
20
 
21
- context "store values appropriately" do
21
+ context "default settings" do
22
+ subject { TsJsonApi::Configure }
23
+ its(:api_version) { should == 2}
24
+ it { should be_logging_enabled }
25
+ it { should_not be_timestamped_logs }
26
+ end
22
27
 
23
- it "should set username & return it" do
24
- TsJsonApi::Configure.username = "username"
25
- TsJsonApi::Configure.username.should == "username"
26
- end
28
+ context "store values appropriately" do
27
29
 
28
- it "should set password & return it" do
29
- TsJsonApi::Configure.password = "password"
30
- TsJsonApi::Configure.password.should == "password"
31
- end
30
+ subject { TsJsonApi::Configure }
32
31
 
33
- it "should have default API version" do
34
- TsJsonApi::Configure.api_version.should == 2
35
- end
32
+ before do
33
+ TsJsonApi::Configure.username = "username"
34
+ TsJsonApi::Configure.password = "password"
35
+ TsJsonApi::Configure.api_version = 1
36
+ TsJsonApi::Configure.logging_enabled = false
37
+ TsJsonApi::Configure.timestamped_logs = true
38
+ end
36
39
 
37
- it "should store & return API version" do
38
- TsJsonApi::Configure.api_version = 1
39
- TsJsonApi::Configure.api_version.should == 1
40
- end
40
+ its(:username) { should == "username" }
41
+ its(:password) { should == "password" }
42
+ its(:api_version) { should == 1 }
43
+ it { should_not be_logging_enabled }
44
+ it { should be_timestamped_logs }
41
45
 
42
- it "logging should be enabled by default" do
43
- TsJsonApi::Configure.logging_enabled?.should == true
44
- end
46
+ end
45
47
 
46
- it "should store logging enabled value" do
47
- TsJsonApi::Configure.logging_enabled = false
48
- TsJsonApi::Configure.logging_enabled?.should == false
49
- end
50
-
51
- it "should have timestamp logging disabled by default" do
52
- TsJsonApi::Configure.timestamped_logs?.should be_false
53
- end
54
-
55
- it "should store timestamped logging setting" do
56
- TsJsonApi::Configure.timestamped_logs = true
57
- TsJsonApi::Configure.timestamped_logs?.should be_true
58
- end
59
-
60
- end
61
-
62
- end
48
+ end
@@ -2,41 +2,36 @@ require 'spec_helper'
2
2
 
3
3
  describe TsJsonApi::Exception do
4
4
 
5
- before :each do
6
- @exception = TsJsonApi::Exception.new
7
- end
8
-
9
- it "should default to class name for no message" do
10
- @exception.message.should == @exception.class.name
11
- end
12
-
13
- it "should store message" do
14
- @exception.message = "lorem ipsum"
15
- @exception.message.should == "lorem ipsum"
16
- end
17
-
18
- it "should store rest client exception" do
19
- @exception.rest_client_exception.should be_nil
20
- @exception.rest_client_exception = RestClient::Exception.new
21
- @exception.rest_client_exception.should be_an_instance_of(RestClient::Exception)
22
- end
23
-
5
+ context "defaults" do
6
+ its(:message) { should == 'TsJsonApi::Exception' }
7
+ end
8
+
9
+ context "custom exceptions" do
10
+ before { subject.message = "lorem ipsum" }
11
+ its(:message) { should == "lorem ipsum" }
12
+
13
+ it "should store rest client exception" do
14
+ subject.rest_client_exception.should be_nil
15
+ subject.rest_client_exception = RestClient::Exception.new
16
+ subject.rest_client_exception.should be_an_instance_of(RestClient::Exception)
17
+ end
18
+ end
24
19
  end
25
20
 
26
21
  describe TsJsonApi::ApiLimitExceededException do
27
- it "should have 400 HTTP status code" do
28
- TsJsonApi::ApiLimitExceededException.new.http_code.should == 400
29
- end
22
+ it "should have 400 HTTP status code" do
23
+ TsJsonApi::ApiLimitExceededException.new.http_code.should == 400
24
+ end
30
25
  end
31
26
 
32
27
  describe TsJsonApi::ServerBrokeConnection do
33
- it "should have 0 HTTP status code" do
34
- TsJsonApi::ServerBrokeConnection.new.http_code.should == 0
35
- end
28
+ it "should have 0 HTTP status code" do
29
+ TsJsonApi::ServerBrokeConnection.new.http_code.should == 0
30
+ end
36
31
  end
37
32
 
38
33
  describe TsJsonApi::ResourceNotFound do
39
- it "should have 404 HTTP status code" do
40
- TsJsonApi::ResourceNotFound.new.http_code.should == 404
41
- end
42
- end
34
+ it "should have 404 HTTP status code" do
35
+ TsJsonApi::ResourceNotFound.new.http_code.should == 404
36
+ end
37
+ end
data/spec/spec_helper.rb CHANGED
@@ -14,4 +14,4 @@ RSpec.configure do |config|
14
14
  config.use_transactional_fixtures = true
15
15
  config.color_enabled = true
16
16
  config.formatter = 'documentation'
17
- end
17
+ 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.6
4
+ version: 0.2.7
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: 2013-01-24 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -86,6 +86,8 @@ files:
86
86
  - lib/tasks/ts_json_api_tasks.rake
87
87
  - lib/ts_json_api/configure.rb
88
88
  - lib/ts_json_api/exceptions.rb
89
+ - lib/ts_json_api/logging/file.rb
90
+ - lib/ts_json_api/logging.rb
89
91
  - lib/ts_json_api/railtie.rb
90
92
  - lib/ts_json_api/requestor/drivers.rb
91
93
  - lib/ts_json_api/requestor/live_feed.rb
@@ -96,7 +98,6 @@ files:
96
98
  - lib/ts_json_api/requestor/schedule.rb
97
99
  - lib/ts_json_api/requestor/series.rb
98
100
  - lib/ts_json_api/requestor/tracks.rb
99
- - lib/ts_json_api/requestor/utility_methods.rb
100
101
  - lib/ts_json_api/requestor/weekend.rb
101
102
  - lib/ts_json_api/requestor.rb
102
103
  - lib/ts_json_api/version.rb
@@ -135,18 +136,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
136
  - - ! '>='
136
137
  - !ruby/object:Gem::Version
137
138
  version: '0'
138
- segments:
139
- - 0
140
- hash: 1665902326267123739
141
139
  required_rubygems_version: !ruby/object:Gem::Requirement
142
140
  none: false
143
141
  requirements:
144
142
  - - ! '>='
145
143
  - !ruby/object:Gem::Version
146
144
  version: '0'
147
- segments:
148
- - 0
149
- hash: 1665902326267123739
150
145
  requirements: []
151
146
  rubyforge_project:
152
147
  rubygems_version: 1.8.24
@@ -174,3 +169,4 @@ test_files:
174
169
  - spec/dummy/db/schema.rb
175
170
  - spec/exceptions_spec.rb
176
171
  - spec/spec_helper.rb
172
+ has_rdoc:
@@ -1,17 +0,0 @@
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