apipie-bindings 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/apipie_bindings/api.rb +68 -35
- data/lib/apipie_bindings/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fbf9178dc6e7698d91a6a0d9f57bb3ca00e6b40
|
4
|
+
data.tar.gz: 7ede66362f69586d304590dd6c449bc4fe5e5f26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25ba2db8f6dcae59ca171a0309fccca7a1bfb8978dfac0a860d62a5c33e4f62c65cbedd6c0b98d6d79da37196fd10beefb941d69ee26860a6dd9e50fd85008ed
|
7
|
+
data.tar.gz: 11194189f412fc4627b2d61abd9472f2a2543c56889c27c99d6c2dc91a92840f1bcfe4789c0f79e117cf4825bc01aba11ca721d47a06895fa7975979f23e78d4
|
data/lib/apipie_bindings/api.rb
CHANGED
@@ -9,7 +9,7 @@ module ApipieBindings
|
|
9
9
|
|
10
10
|
class API
|
11
11
|
|
12
|
-
attr_reader :apidoc_cache_name, :fake_responses
|
12
|
+
attr_reader :apidoc_cache_name, :fake_responses, :language
|
13
13
|
attr_writer :dry_run
|
14
14
|
|
15
15
|
# Creates new API bindings instance
|
@@ -23,7 +23,8 @@ module ApipieBindings
|
|
23
23
|
# * *:options* (Hash) options passed to OAuth
|
24
24
|
# @option config [Hash] :headers additional headers to send with the requests
|
25
25
|
# @option config [String] :api_version ('1') version of the API
|
26
|
-
# @option config [String] :
|
26
|
+
# @option config [String] :language prefered locale for the API description
|
27
|
+
# @option config [String] :apidoc_cache_dir ('~/.cache/apipie_bindings/<URI>') where
|
27
28
|
# to cache the JSON description of the API
|
28
29
|
# @option config [String] :apidoc_cache_name ('default.json') name of te cache file.
|
29
30
|
# If there is cache in the :apidoc_cache_dir, it is used.
|
@@ -51,12 +52,12 @@ module ApipieBindings
|
|
51
52
|
end
|
52
53
|
@uri = config[:uri]
|
53
54
|
@api_version = config[:api_version] || 1
|
54
|
-
@
|
55
|
+
@language = config[:language]
|
56
|
+
@apidoc_cache_dir = config[:apidoc_cache_dir] || File.join(File.expand_path('~/.cache'), 'apipie_bindings', @uri.tr(':/', '_'))
|
55
57
|
@apidoc_cache_name = config[:apidoc_cache_name] || set_default_name
|
56
58
|
@dry_run = config[:dry_run] || false
|
57
59
|
@aggressive_cache_checking = config[:aggressive_cache_checking] || false
|
58
60
|
@fake_responses = config[:fake_responses] || {}
|
59
|
-
|
60
61
|
@logger = config[:logger]
|
61
62
|
unless @logger
|
62
63
|
@logger = Logger.new(STDERR)
|
@@ -69,9 +70,12 @@ module ApipieBindings
|
|
69
70
|
:content_type => 'application/json',
|
70
71
|
:accept => "application/json;version=#{@api_version}"
|
71
72
|
}
|
73
|
+
headers.merge!({ "Accept-Language" => language }) if language
|
72
74
|
headers.merge!(config[:headers]) unless config[:headers].nil?
|
73
75
|
headers.merge!(options.delete(:headers)) unless options[:headers].nil?
|
74
76
|
|
77
|
+
log.debug "Global headers: #{headers.ai}"
|
78
|
+
|
75
79
|
resource_config = {
|
76
80
|
:user => config[:username],
|
77
81
|
:password => config[:password],
|
@@ -84,17 +88,9 @@ module ApipieBindings
|
|
84
88
|
@config = config
|
85
89
|
end
|
86
90
|
|
87
|
-
def set_default_name(default='default')
|
88
|
-
cache_file = Dir["#{@apidoc_cache_dir}/*.json"].first
|
89
|
-
if cache_file
|
90
|
-
File.basename(cache_file, ".json")
|
91
|
-
else
|
92
|
-
default
|
93
|
-
end
|
94
|
-
end
|
95
91
|
|
96
92
|
def apidoc_cache_file
|
97
|
-
File.join(@apidoc_cache_dir, "#{@apidoc_cache_name}
|
93
|
+
File.join(@apidoc_cache_dir, "#{@apidoc_cache_name}#{cache_extension}")
|
98
94
|
end
|
99
95
|
|
100
96
|
def load_apidoc
|
@@ -166,6 +162,7 @@ module ApipieBindings
|
|
166
162
|
# @param [Hash] headers extra headers to be sent with the request
|
167
163
|
# @param [Hash] options options to influence the how the call is processed
|
168
164
|
# * *:response* (Symbol) *:raw* - skip parsing JSON in response
|
165
|
+
# * *:reduce_response_log* (Bool) - do not show response content in the log.
|
169
166
|
# @example show user data
|
170
167
|
# http_call('get', '/api/users/1')
|
171
168
|
def http_call(http_method, path, params={}, headers={}, options={})
|
@@ -185,6 +182,7 @@ module ApipieBindings
|
|
185
182
|
|
186
183
|
log.info "#{http_method.to_s.upcase} #{path}"
|
187
184
|
log.debug "Params: #{params.ai}"
|
185
|
+
log.debug "Headers: #{headers.ai}"
|
188
186
|
|
189
187
|
args << headers if headers
|
190
188
|
|
@@ -193,24 +191,21 @@ module ApipieBindings
|
|
193
191
|
ex = options[:fake_response ] || empty_response
|
194
192
|
response = RestClient::Response.create(ex.response, ex.status, args)
|
195
193
|
else
|
196
|
-
|
197
|
-
|
194
|
+
begin
|
195
|
+
response = @client[path].send(*args)
|
196
|
+
update_cache(response.headers[:apipie_checksum])
|
197
|
+
rescue => e
|
198
|
+
log.error e.message + "\n" +
|
199
|
+
(e.respond_to?(:response) ? process_data(e.response).ai : e.ai)
|
200
|
+
raise
|
201
|
+
end
|
198
202
|
end
|
199
203
|
|
200
204
|
result = options[:response] == :raw ? response : process_data(response)
|
201
|
-
log.debug "Response
|
205
|
+
log.debug "Response %s" % (options[:reduce_response_log] ? "Received OK" : result.ai)
|
202
206
|
result
|
203
207
|
end
|
204
208
|
|
205
|
-
def process_data(response)
|
206
|
-
data = begin
|
207
|
-
JSON.parse(response.body)
|
208
|
-
rescue JSON::ParserError
|
209
|
-
response.body
|
210
|
-
end
|
211
|
-
# logger.debug "Returned data: #{data.inspect}"
|
212
|
-
return data
|
213
|
-
end
|
214
209
|
|
215
210
|
def update_cache(cache_name)
|
216
211
|
if !cache_name.nil? && (cache_name != @apidoc_cache_name)
|
@@ -222,7 +217,7 @@ module ApipieBindings
|
|
222
217
|
|
223
218
|
def clean_cache
|
224
219
|
@apidoc = nil
|
225
|
-
Dir["#{@apidoc_cache_dir}
|
220
|
+
Dir["#{@apidoc_cache_dir}/*#{cache_extension}"].each { |f| File.delete(f) }
|
226
221
|
end
|
227
222
|
|
228
223
|
def check_cache
|
@@ -242,21 +237,37 @@ module ApipieBindings
|
|
242
237
|
|
243
238
|
def retrieve_apidoc
|
244
239
|
FileUtils.mkdir_p(@apidoc_cache_dir) unless File.exists?(@apidoc_cache_dir)
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
240
|
+
if language
|
241
|
+
response = retrieve_apidoc_call("/apidoc/v#{@api_version}.#{language}.json", :safe => true)
|
242
|
+
language_family = language.split('_').first
|
243
|
+
if !response && language_family != language
|
244
|
+
response = retrieve_apidoc_call("/apidoc/v#{@api_version}.#{language_family}.json", :safe => true)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
unless response
|
248
|
+
begin
|
249
|
+
response = retrieve_apidoc_call("/apidoc/v#{@api_version}.json")
|
250
|
+
rescue
|
251
|
+
raise ApipieBindings::DocLoadingError.new(
|
252
|
+
"Could not load data from #{@uri}\n"\
|
253
|
+
" - is your server down?\n"\
|
254
|
+
" - was rake apipie:cache run when using apipie cache? (typical production settings)")
|
255
|
+
end
|
254
256
|
end
|
255
257
|
File.open(apidoc_cache_file, "w") { |f| f.write(response.body) }
|
256
258
|
log.debug "New apidoc loaded from the server"
|
257
259
|
load_apidoc
|
258
260
|
end
|
259
261
|
|
262
|
+
def retrieve_apidoc_call(path, options={})
|
263
|
+
begin
|
264
|
+
http_call('get', path, {},
|
265
|
+
{:accept => "application/json"}, {:response => :raw, :reduce_response_log => true})
|
266
|
+
rescue
|
267
|
+
raise unless options[:safe]
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
260
271
|
def find_match(fakes, resource, action, params)
|
261
272
|
resource = fakes[[resource, action]]
|
262
273
|
if resource
|
@@ -269,6 +280,28 @@ module ApipieBindings
|
|
269
280
|
return nil
|
270
281
|
end
|
271
282
|
|
283
|
+
def cache_extension
|
284
|
+
language ? ".#{language}.json" : ".json"
|
285
|
+
end
|
286
|
+
|
287
|
+
def set_default_name(default='default')
|
288
|
+
cache_file = Dir["#{@apidoc_cache_dir}/*#{cache_extension}"].first
|
289
|
+
if cache_file
|
290
|
+
File.basename(cache_file, cache_extension)
|
291
|
+
else
|
292
|
+
default
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def process_data(response)
|
297
|
+
data = begin
|
298
|
+
JSON.parse(response.body)
|
299
|
+
rescue JSON::ParserError
|
300
|
+
response.body
|
301
|
+
end
|
302
|
+
# logger.debug "Returned data: #{data.inspect}"
|
303
|
+
return data
|
304
|
+
end
|
272
305
|
end
|
273
306
|
|
274
307
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apipie-bindings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Bačovský
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.2.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rest-client
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
134
|
version: '0'
|
135
135
|
requirements: []
|
136
136
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
137
|
+
rubygems_version: 2.1.11
|
138
138
|
signing_key:
|
139
139
|
specification_version: 4
|
140
140
|
summary: The Ruby bindings for Apipie documented APIs
|