ts_json_api 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,15 +24,25 @@ This gem provides several configuration options exposed in an `ts_json_api.rb` i
24
24
  TsJsonApi::Configure.setup do |config|
25
25
  config.username = ""
26
26
  config.password = ""
27
- config.api_version = 2
28
27
  config.logging_enabled = true
29
28
  config.server_url = ""
30
29
  config.timestamped_logs = false
30
+
31
+ config.api_version = 2
32
+ config.api_version = ->(url) do
33
+ if url =~ /livepoints/
34
+ 'include_wins'
35
+ else
36
+ 2
37
+ end
38
+ end
31
39
  end
32
40
  ```
33
41
 
34
42
  The `username`, `password`, and `server_url` fields are all requried to specify which service to connect to. The `api_version` field allows you to specify a particular version or contract you have in place with the T&S team when authenticating with the service.
35
43
 
44
+ You can specify a lambda for the `api_version` setting, or any instance that responds to `#call` and accepts one argument of the URL being requested, to dynamically return the version for the request.
45
+
36
46
  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.
37
47
 
38
48
  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.
@@ -1,57 +1,65 @@
1
1
  module TsJsonApi
2
- class Requestor
3
- module PrivateMethods
2
+ class Requestor
3
+ module PrivateMethods
4
4
 
5
- extend ActiveSupport::Concern
5
+ extend ActiveSupport::Concern
6
6
 
7
- module ClassMethods
7
+ module ClassMethods
8
8
 
9
- def send_json_request_and_deliver_response(path, partial_url)
10
- url = "#{Configure.server_url}#{partial_url}"
9
+ def send_json_request_and_deliver_response(path, partial_url)
10
+ url = "#{Configure.server_url}#{partial_url}"
11
11
 
12
- response = perfom_request(partial_url)
13
- json = response.to_str
14
- json.gsub!(/[^\x20-\x7e]/,'')
15
- json.gsub! 'NaN', '"NaN"'
12
+ response = perfom_request(partial_url)
13
+ json = response.to_str
14
+ json.gsub!(/[^\x20-\x7e]/,'')
15
+ json.gsub! 'NaN', '"NaN"'
16
16
 
17
- log(path, url, json)
17
+ log(path, url, json)
18
18
 
19
- JSON.parse(json)
20
- end
19
+ JSON.parse(json)
20
+ end
21
21
 
22
- def api
23
- @@api ||= RestClient::Resource.new(Configure.server_url, user: Configure.username, password: Configure.password, timeout: 20, headers: { accept: "application/json;version=#{Configure.api_version}"})
24
- end
22
+ def api
23
+ @@api ||= RestClient::Resource.new(Configure.server_url, user: Configure.username, password: Configure.password, timeout: 20, headers: { accept: :json })
24
+ end
25
25
 
26
- def perfom_request(partial_url)
27
- begin
28
- api[partial_url].get
26
+ def perfom_request(partial_url)
27
+ begin
28
+ api[partial_url].get version: api_version(partial_url)
29
29
 
30
- rescue RestClient::Exceptions::EXCEPTIONS_MAP[400] => e
31
- raise ApiLimitExceededException.new exception: e
30
+ rescue RestClient::Exceptions::EXCEPTIONS_MAP[400] => e
31
+ raise ApiLimitExceededException.new exception: e
32
32
 
33
- rescue RestClient::Exceptions::EXCEPTIONS_MAP[404] => e
34
- raise ResourceNotFound.new exception: e
33
+ rescue RestClient::Exceptions::EXCEPTIONS_MAP[404] => e
34
+ raise ResourceNotFound.new exception: e
35
35
 
36
- rescue RestClient::ServerBrokeConnection, Errno::ECONNRESET => e
37
- raise ServerBrokeConnection.new exception: e
36
+ rescue RestClient::ServerBrokeConnection, Errno::ECONNRESET => e
37
+ raise ServerBrokeConnection.new exception: e
38
38
 
39
- rescue RestClient::Exceptions::EXCEPTIONS_MAP[401] => e
40
- raise AccessTokenRefused.new exception: e
39
+ rescue RestClient::Exceptions::EXCEPTIONS_MAP[401] => e
40
+ raise AccessTokenRefused.new exception: e
41
41
 
42
- rescue RestClient::Exception => e
43
- raise Exception.new exception: e
42
+ rescue RestClient::Exception => e
43
+ raise Exception.new exception: e
44
44
 
45
- end
46
- end
45
+ end
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
48
+ def log(path, url, str)
49
+ f = Logging::File.new(relative_path: path, url: url)
50
+ f.write str
51
+ end
52
52
 
53
- end
53
+ def api_version(partial_url)
54
+ if Configure.api_version.respond_to?(:call)
55
+ Configure.api_version.call(partial_url)
56
+ else
57
+ Configure.api_version
58
+ end
59
+ end
54
60
 
55
- end
56
- end
61
+ end
62
+
63
+ end
64
+ end
57
65
  end
@@ -1,3 +1,3 @@
1
1
  module TsJsonApi
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
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.8
4
+ version: 0.2.9
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-05-01 00:00:00.000000000 Z
12
+ date: 2013-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails