apipie-bindings 0.0.4 → 0.0.5
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.rb +1 -0
- data/lib/apipie_bindings/api.rb +49 -16
- data/lib/apipie_bindings/exceptions.rb +6 -0
- data/lib/apipie_bindings/version.rb +1 -1
- data/test/unit/api_test.rb +16 -0
- metadata +3 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a805b89ca19114aead114bef90692bb47509284b
|
4
|
+
data.tar.gz: 07cf88b87738018220613dfad3ff8311375947ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6230f046f2ead04118c213a12e0ca24189d45cef2d61eac2cec82fbc95e44906d24acf4a8fd17d758ff34f4cff4ea2592dced3ab4c4005b4bd4d628d722e8c3c
|
7
|
+
data.tar.gz: 0a89ea42ac4b7db3f29e6643f207aa59ea07135509be0afc64b090831eaa4e021d19605890e2f65ed1fc3498945493ea03371714438cad7757750fc7e3f671ef
|
data/lib/apipie_bindings.rb
CHANGED
data/lib/apipie_bindings/api.rb
CHANGED
@@ -36,6 +36,7 @@ module ApipieBindings
|
|
36
36
|
# *after* each API request
|
37
37
|
# @option config [Object] :logger (Logger.new(STDERR)) custom logger class
|
38
38
|
# @param [Hash] options params that are passed to ResClient as-is
|
39
|
+
# @raise [ApipieBindings::ConfigurationError] when no +:uri+ or +:apidoc_cache_dir+ is provided
|
39
40
|
# @example connect to a server
|
40
41
|
# ApipieBindings::API.new({:uri => 'http://localhost:3000/',
|
41
42
|
# :username => 'admin', :password => 'changeme',
|
@@ -44,6 +45,9 @@ module ApipieBindings
|
|
44
45
|
# ApipieBindings::API.new({:apidoc_cache_dir => 'test/unit/data',
|
45
46
|
# :apidoc_cache_name => 'architecture'})
|
46
47
|
def initialize(config, options={})
|
48
|
+
if config[:uri].nil? && config[:apidoc_cache_dir].nil?
|
49
|
+
raise ApipieBindings::ConfigurationError.new('Either :uri or :apidoc_cache_dir needs to be set')
|
50
|
+
end
|
47
51
|
@uri = config[:uri]
|
48
52
|
@api_version = config[:api_version] || 1
|
49
53
|
@apidoc_cache_dir = config[:apidoc_cache_dir] || File.join(Dir.tmpdir, 'apipie_bindings', @uri.tr(':/', '_'))
|
@@ -98,19 +102,6 @@ module ApipieBindings
|
|
98
102
|
end
|
99
103
|
end
|
100
104
|
|
101
|
-
def retrieve_apidoc
|
102
|
-
FileUtils.mkdir_p(@apidoc_cache_dir) unless File.exists?(@apidoc_cache_dir)
|
103
|
-
path = "/apidoc/v#{@api_version}.json"
|
104
|
-
begin
|
105
|
-
response = http_call('get', path, {},
|
106
|
-
{:accept => "application/json"}, {:response => :raw})
|
107
|
-
rescue
|
108
|
-
raise "Could not load data from #{@uri}#{path}"
|
109
|
-
end
|
110
|
-
File.open(apidoc_cache_file, "w") { |f| f.write(response.body) }
|
111
|
-
log.debug "New apidoc loaded from the server"
|
112
|
-
load_apidoc
|
113
|
-
end
|
114
105
|
|
115
106
|
def apidoc
|
116
107
|
@apidoc = @apidoc || load_apidoc || retrieve_apidoc
|
@@ -135,6 +126,22 @@ module ApipieBindings
|
|
135
126
|
apidoc[:docs][:resources].keys.map { |res| resource(res) }
|
136
127
|
end
|
137
128
|
|
129
|
+
|
130
|
+
# Call an action in the API.
|
131
|
+
# It finds most fitting route based on given parameters
|
132
|
+
# with other attributes neccessary to do an API call.
|
133
|
+
# If in dry_run mode {#initialize} it finds fake response data in examples
|
134
|
+
# or user provided data. At the end when the response format is JSON it
|
135
|
+
# is parsed and returned as ruby objects. If server supports checksum sending
|
136
|
+
# the internal cache with API description is checked and updated if needed
|
137
|
+
# @param [Symbol] resource_name name of the resource
|
138
|
+
# @param [Symbol] action_name name of the action
|
139
|
+
# @param [Hash] params parameters to be send in the request
|
140
|
+
# @param [Hash] headers extra headers to be sent with the request
|
141
|
+
# @param [Hash] options options to influence the how the call is processed
|
142
|
+
# * *:response* (Symbol) *:raw* - skip parsing JSON in response
|
143
|
+
# @example show user data
|
144
|
+
# call(:users, :show, :id => 1)
|
138
145
|
def call(resource_name, action_name, params={}, headers={}, options={})
|
139
146
|
check_cache if @aggressive_cache_checking
|
140
147
|
resource = resource(resource_name)
|
@@ -149,13 +156,22 @@ module ApipieBindings
|
|
149
156
|
headers, options)
|
150
157
|
end
|
151
158
|
|
152
|
-
|
159
|
+
# Low level call to the API. Suitable for calling actions not covered by
|
160
|
+
# apipie documentation. For all other cases use {#call}
|
161
|
+
# @param [String] http_method one of +get+, +put+, +post+, +destroy+, +patch+
|
162
|
+
# @param [String] path URL path that should be called
|
163
|
+
# @param [Hash] params parameters to be send in the request
|
164
|
+
# @param [Hash] headers extra headers to be sent with the request
|
165
|
+
# @param [Hash] options options to influence the how the call is processed
|
166
|
+
# * *:response* (Symbol) *:raw* - skip parsing JSON in response
|
167
|
+
# @example show user data
|
168
|
+
# http_call('get', '/api/users/1')
|
153
169
|
def http_call(http_method, path, params={}, headers={}, options={})
|
154
170
|
headers ||= { }
|
155
171
|
|
156
172
|
args = [http_method]
|
157
173
|
if %w[post put].include?(http_method.to_s)
|
158
|
-
args << params
|
174
|
+
args << params
|
159
175
|
else
|
160
176
|
headers[:params] = params if params
|
161
177
|
end
|
@@ -168,7 +184,7 @@ module ApipieBindings
|
|
168
184
|
if dry_run?
|
169
185
|
empty_response = ApipieBindings::Example.new('', '', '', 200, '')
|
170
186
|
ex = options[:fake_response ] || empty_response
|
171
|
-
response = RestClient::Response.create(ex.response, ex.status,
|
187
|
+
response = RestClient::Response.create(ex.response, ex.status, args)
|
172
188
|
else
|
173
189
|
response = @client[path].send(*args)
|
174
190
|
update_cache(response.headers[:apipie_checksum])
|
@@ -217,6 +233,23 @@ module ApipieBindings
|
|
217
233
|
|
218
234
|
private
|
219
235
|
|
236
|
+
def retrieve_apidoc
|
237
|
+
FileUtils.mkdir_p(@apidoc_cache_dir) unless File.exists?(@apidoc_cache_dir)
|
238
|
+
path = "/apidoc/v#{@api_version}.json"
|
239
|
+
begin
|
240
|
+
response = http_call('get', path, {},
|
241
|
+
{:accept => "application/json"}, {:response => :raw})
|
242
|
+
rescue
|
243
|
+
raise ApipieBindings::DocLoadingError.new(
|
244
|
+
"Could not load data from #{@uri}#{path}\n"\
|
245
|
+
" - is your server down?\n"\
|
246
|
+
" - was rake apipie:cache run when using apipie cache? (typical production settings)")
|
247
|
+
end
|
248
|
+
File.open(apidoc_cache_file, "w") { |f| f.write(response.body) }
|
249
|
+
log.debug "New apidoc loaded from the server"
|
250
|
+
load_apidoc
|
251
|
+
end
|
252
|
+
|
220
253
|
def find_match(fakes, resource, action, params)
|
221
254
|
resource = fakes[[resource, action]]
|
222
255
|
if resource
|
data/test/unit/api_test.rb
CHANGED
@@ -59,6 +59,16 @@ describe ApipieBindings::API do
|
|
59
59
|
result.must_be_kind_of Array
|
60
60
|
end
|
61
61
|
|
62
|
+
it "should preserve file in args (#2)" do
|
63
|
+
api = ApipieBindings::API.new({
|
64
|
+
:apidoc_cache_dir => 'test/unit/data',
|
65
|
+
:apidoc_cache_name => 'architecture',
|
66
|
+
:dry_run => true})
|
67
|
+
s = StringIO.new; s << 'foo'
|
68
|
+
RestClient::Response.expects(:create).with('', 200, [:post, {:file => s}, {}])
|
69
|
+
result = api.http_call(:post, '/api/path', {:file => s}, {}, {:response => :raw})
|
70
|
+
end
|
71
|
+
|
62
72
|
context "update_cache" do
|
63
73
|
|
64
74
|
before :each do
|
@@ -106,4 +116,10 @@ describe ApipieBindings::API do
|
|
106
116
|
end
|
107
117
|
end
|
108
118
|
|
119
|
+
context "configuration" do
|
120
|
+
it "should complain when no uri or cache dir is set" do
|
121
|
+
proc {ApipieBindings::API.new({})}.must_raise ApipieBindings::ConfigurationError
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
109
125
|
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.5
|
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-03-
|
11
|
+
date: 2014-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: i18n
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '>='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: awesome_print
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,6 +95,7 @@ files:
|
|
109
95
|
- lib/apipie_bindings/action.rb
|
110
96
|
- lib/apipie_bindings/api.rb
|
111
97
|
- lib/apipie_bindings/example.rb
|
98
|
+
- lib/apipie_bindings/exceptions.rb
|
112
99
|
- lib/apipie_bindings/indifferent_hash.rb
|
113
100
|
- lib/apipie_bindings/inflector.rb
|
114
101
|
- lib/apipie_bindings/param.rb
|