version-one 0.0.5 → 0.0.6

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.
@@ -9,13 +9,14 @@ module VersionOne
9
9
 
10
10
  def get(path, *fields)
11
11
  uri = path_uri(path)
12
+ options = fields.last.is_a?(Hash) ? fields.pop : {}
12
13
 
13
14
  unless fields.empty?
14
15
  fields.concat(Query::REQUIRED_FIELDS)
15
16
  uri.query = "sel=#{fields.join(',')}"
16
17
  end
17
18
 
18
- get_uri uri
19
+ get_uri uri, options
19
20
  end
20
21
 
21
22
  def post_xml(path, xml)
@@ -28,16 +29,22 @@ module VersionOne
28
29
  end
29
30
  end
30
31
 
31
- def post(path)
32
+ def post(path, &block)
32
33
  uri = path_uri(path)
33
- request :post, uri do |r|
34
- r.body = ''
35
- r.content_type = XML_CONTENT_TYPE
34
+
35
+ if block_given?
36
+ request :post, uri, &block
37
+ else
38
+ request :post, uri do |r|
39
+ r.body = ''
40
+ r.content_type = XML_CONTENT_TYPE
41
+ end
36
42
  end
43
+
37
44
  end
38
45
 
39
- def get_uri(uri)
40
- request :get, uri
46
+ def get_uri(uri, options={})
47
+ request :get, uri, options
41
48
  end
42
49
 
43
50
  def cache_store
@@ -45,7 +52,7 @@ module VersionOne
45
52
  end
46
53
 
47
54
  def can_cache?
48
- @cache && cache_store
55
+ !!cache_store
49
56
  end
50
57
 
51
58
  def self.service_uri
@@ -74,8 +81,34 @@ module VersionOne
74
81
  end
75
82
  end
76
83
 
77
- def request(type, uri)
84
+ def request(type, uri, options={}, &block)
85
+ if (type == :get)
86
+ cached_request type, uri, options, &block
87
+ else
88
+ uncached_request type, uri, options, &block
89
+ end
90
+ end
91
+
92
+ def cached_request(type, uri, options={})
93
+ cache_options = options[:cache]
78
94
  xml = nil
95
+
96
+ if can_cache? && cache_options
97
+ xml = cache_store.read(cache_options[:key])
98
+
99
+ if xml
100
+ xml = XML::Document.string(xml).root
101
+ VersionOne.logger.debug('%s CACHE %s' % [type.to_s.upcase, uri.to_s])
102
+ end
103
+ end
104
+
105
+ xml ||= uncached_request type, uri, options
106
+ xml
107
+ end
108
+
109
+ def uncached_request(type, uri, options={})
110
+ xml = nil
111
+ xml_body = nil
79
112
  VersionOne.logger.debug('%s %s' % [type.to_s.upcase, uri.to_s])
80
113
 
81
114
  http = Net::HTTP.new(uri.host, uri.port)
@@ -99,12 +132,15 @@ module VersionOne
99
132
  response = http.request(request)
100
133
 
101
134
  if response.content_type == XML_CONTENT_TYPE
102
- #VersionOne.logger.debug response.body
103
- xml = XML::Document.string(response.body).root
135
+ xml_body = response.body.to_s
136
+ xml = XML::Document.string(xml_body).root
104
137
  end
105
138
 
106
139
  if (response.code.to_i / 100) != 2
107
140
  handle_error(xml, response, request)
141
+ elsif can_cache? && options[:cache]
142
+ cache_store.write(options[:cache][:key], xml_body, options[:cache][:options])
143
+ VersionOne.logger.debug "CACHED #{options[:cache][:key]}"
108
144
  end
109
145
  end
110
146
 
@@ -126,4 +162,5 @@ module VersionOne
126
162
  end
127
163
 
128
164
  end
129
- end
165
+
166
+ end
@@ -12,25 +12,19 @@ module VersionOne
12
12
  end
13
13
 
14
14
  def self.get(asset_type, client=nil)
15
- xml = get_cached(asset_type)
15
+ client ||= VersionOne::Client.new
16
16
 
17
- unless xml
18
- client ||= VersionOne::Client.new
19
- xml = client.get('/meta.v1/' + asset_type)
20
- set_cached(asset_type, xml)
21
- end
17
+ xml = client.get('/meta.v1/' + asset_type, cache: {
18
+ key: "VersionOne/meta/#{asset_type}",
19
+ options: {
20
+ expires_in: 60 * 60 * 24,
21
+ race_condition_ttl: 5
22
+ }
23
+ })
22
24
 
23
25
  new(xml)
24
26
  end
25
27
 
26
- def self.cache
27
- @@cache ||= nil
28
- end
29
-
30
- def self.cache=(c)
31
- @@cache = c
32
- end
33
-
34
28
  def [](name)
35
29
  @named_attributes[name]
36
30
  end
@@ -56,19 +50,5 @@ module VersionOne
56
50
 
57
51
  end
58
52
 
59
- def self.get_cached(asset_type)
60
- if cache
61
- xml = XML::Document.string(cache.read("VersionOne/Meta/#{asset_type}")).root rescue nil
62
- VersionOne.logger.debug("Loaded #{asset_type} meta from cache") if xml
63
- xml
64
- else
65
- nil
66
- end
67
- end
68
-
69
- def self.set_cached(asset_type, meta)
70
- cache.write("VersionOne/Meta/#{asset_type}", meta.to_s) if cache
71
- end
72
-
73
53
  end
74
- end
54
+ end
@@ -55,10 +55,7 @@ module VersionOne
55
55
  end
56
56
 
57
57
  def find_by_url(url)
58
- xml = nil
59
- #xml = cache_store.read(@cache[:key]) if can_cache?
60
- xml ||= @client.get(url)
61
- #cache_store.write(@cache[:key], xml, @cache[:options]) if can_cache?
58
+ xml = @client.get(url, cache: @cache)
62
59
 
63
60
  if xml.name == 'Error'
64
61
  msg = 'VersionOne Error: %s (%s)' % [xml.find_first('Message').content, xml.attributes['href']]
@@ -176,6 +173,7 @@ module VersionOne
176
173
 
177
174
  def cache(key, options={})
178
175
  dup do
176
+ options[:namespace] ||= 'VersionOne'
179
177
  @cache = {
180
178
  :key => key,
181
179
  :options => options
@@ -193,35 +191,35 @@ module VersionOne
193
191
 
194
192
  private unless ENV['RAILS_ENV'] == 'test'
195
193
 
196
- def http_get(uri)
197
- xml = nil
198
-
199
- http = Net::HTTP.new(uri.host, uri.port)
200
- http.use_ssl = true
201
-
202
- request_path = uri.path
203
- request_path += '?' + uri.query unless uri.query.blank?
204
- Rails.logger.debug("Uri path = #{request_path}")
205
-
206
- http.start do
207
- request = Net::HTTP::Get.new(request_path)
208
- request.basic_auth(VersionOne.user, VersionOne.password)
209
- response = http.request(request)
210
-
211
- xml = response.body
212
- Rails.logger.debug(xml)
213
- end
214
-
215
- xml
216
- end
217
-
218
- def cache_store
219
- Rails.cache
220
- end
221
-
222
- def can_cache?
223
- @cache && cache_store
224
- end
194
+ # def http_get(uri)
195
+ # xml = nil
196
+ #
197
+ # http = Net::HTTP.new(uri.host, uri.port)
198
+ # http.use_ssl = true
199
+ #
200
+ # request_path = uri.path
201
+ # request_path += '?' + uri.query unless uri.query.blank?
202
+ # Rails.logger.debug("Uri path = #{request_path}")
203
+ #
204
+ # http.start do
205
+ # request = Net::HTTP::Get.new(request_path)
206
+ # request.basic_auth(VersionOne.user, VersionOne.password)
207
+ # response = http.request(request)
208
+ #
209
+ # xml = response.body
210
+ # Rails.logger.debug(xml)
211
+ # end
212
+ #
213
+ # xml
214
+ # end
215
+ #
216
+ # def cache_store
217
+ # Rails.cache
218
+ # end
219
+ #
220
+ # def can_cache?
221
+ # @cache && cache_store
222
+ # end
225
223
 
226
224
  def select_query
227
225
  if @select.empty?
@@ -1,3 +1,3 @@
1
1
  module VersionOne
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: version-one
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2014-10-03 00:00:00.000000000 Z
12
+ date: 2014-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: libxml-ruby