almodovar 1.5.5 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/almodovar/errors.rb +4 -2
- data/lib/almodovar/http_accessor.rb +6 -8
- data/lib/almodovar/http_client.rb +18 -11
- data/lib/almodovar/resource.rb +1 -1
- data/lib/almodovar/resource_collection.rb +9 -9
- data/lib/almodovar/single_resource.rb +2 -2
- data/lib/almodovar/to_xml.rb +3 -3
- data/lib/almodovar/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc546c375c4b0197063352e38e055f4c51366768
|
4
|
+
data.tar.gz: 929c83c4e93004697e980ec92688598c681640ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40563773b89f684fc4564c77d436ded0d58e479ecdcb622d5742ce49a430cbea6d1864d634caab50967efd0fb2d35b39c2ac5521652312c200de2c8d87794a12
|
7
|
+
data.tar.gz: b939721c3185ac43dc70b6d667ddfe00552877a7712e4d785876111148962597320893144b458bd35991423d3dcdea435ba6908de93374cee5a66daf0c82f7bb
|
data/lib/almodovar/errors.rb
CHANGED
@@ -3,10 +3,12 @@ module Almodovar
|
|
3
3
|
attr_reader :response_status, :response_body
|
4
4
|
|
5
5
|
# Children of this class must not override the initialize method
|
6
|
-
def initialize(response, url)
|
6
|
+
def initialize(response, url, query_params = {})
|
7
7
|
@response_status = response.status
|
8
8
|
@response_body = response.body
|
9
|
-
|
9
|
+
message = "Status code #{response.status} on resource #{url}"
|
10
|
+
message += " with params: #{query_params.inspect}" if query_params.present?
|
11
|
+
super(message)
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
@@ -2,17 +2,15 @@ module Almodovar
|
|
2
2
|
module HttpAccessor
|
3
3
|
def xml
|
4
4
|
@xml ||= begin
|
5
|
-
response = http.get(
|
6
|
-
check_errors(response,
|
5
|
+
response = http.get(@url, query_params)
|
6
|
+
check_errors(response, @url, query_params)
|
7
7
|
Nokogiri::XML.parse(response.body).root
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
11
|
+
def query_params
|
12
12
|
@options[:expand] = @options[:expand].join(",") if @options[:expand].is_a?(Array)
|
13
|
-
|
14
|
-
params = "?#{params}" unless params.empty?
|
15
|
-
@url + params
|
13
|
+
@options
|
16
14
|
end
|
17
15
|
|
18
16
|
def http
|
@@ -31,10 +29,10 @@ module Almodovar
|
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
|
-
def check_errors(response, url)
|
32
|
+
def check_errors(response, url, query_params = {})
|
35
33
|
if response.status >= 400
|
36
34
|
http_error_klass = Almodovar::HTTP_ERRORS[response.status] || Almodovar::HttpError
|
37
|
-
raise http_error_klass.new(response, url)
|
35
|
+
raise http_error_klass.new(response, url, query_params)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'addressable/uri'
|
1
2
|
require 'httpclient'
|
2
3
|
|
3
4
|
module Almodovar
|
@@ -13,26 +14,26 @@ module Almodovar
|
|
13
14
|
:send_timeout=,
|
14
15
|
:receive_timeout=,
|
15
16
|
:force_basic_auth=,
|
16
|
-
:
|
17
|
+
to: :client
|
17
18
|
|
18
19
|
def initialize
|
19
20
|
@client = HTTPClient.new
|
20
21
|
end
|
21
22
|
|
22
|
-
def get(uri, headers = {})
|
23
|
-
request(:get, uri, :headers
|
23
|
+
def get(uri, query = {}, headers = {})
|
24
|
+
request(:get, uri, query: query, headers: merge_headers(headers))
|
24
25
|
end
|
25
26
|
|
26
|
-
def post(uri, data, headers = {})
|
27
|
-
request(:post, uri, :
|
27
|
+
def post(uri, data, query = {}, headers = {})
|
28
|
+
request(:post, uri, body: data, query: query, headers: merge_headers(headers))
|
28
29
|
end
|
29
30
|
|
30
|
-
def put(uri, data, headers = {})
|
31
|
-
request(:put, uri, :
|
31
|
+
def put(uri, data, query = {}, headers = {})
|
32
|
+
request(:put, uri, body: data, query: query, headers: merge_headers(headers))
|
32
33
|
end
|
33
34
|
|
34
|
-
def delete(uri, headers = {})
|
35
|
-
request(:delete, uri, :headers
|
35
|
+
def delete(uri, query = {}, headers = {})
|
36
|
+
request(:delete, uri, query: query, headers: merge_headers(headers))
|
36
37
|
end
|
37
38
|
|
38
39
|
private
|
@@ -68,12 +69,18 @@ module Almodovar
|
|
68
69
|
end
|
69
70
|
|
70
71
|
def request(method, uri, options = {})
|
71
|
-
uri = URI.parse(
|
72
|
+
uri = Addressable::URI.parse(uri)
|
72
73
|
if (requires_auth?)
|
73
74
|
domain = domain_for(uri)
|
74
75
|
set_client_auth(domain)
|
75
76
|
end
|
76
|
-
|
77
|
+
request_options = {
|
78
|
+
body: options[:body],
|
79
|
+
header: options[:headers].stringify_keys || {},
|
80
|
+
follow_redirect: true
|
81
|
+
}
|
82
|
+
request_options[:query] = options[:query] if options[:query].present?
|
83
|
+
client.request(method, uri, request_options)
|
77
84
|
rescue HTTPClient::SendTimeoutError => e
|
78
85
|
raise SendTimeoutError.new(e)
|
79
86
|
rescue HTTPClient::ReceiveTimeoutError => e
|
data/lib/almodovar/resource.rb
CHANGED
@@ -5,7 +5,7 @@ module Almodovar
|
|
5
5
|
undef_method :id if instance_methods.include?("id")
|
6
6
|
undef_method :type if instance_methods.include?("type")
|
7
7
|
|
8
|
-
delegate :inspect, :
|
8
|
+
delegate :inspect, to: :get!
|
9
9
|
|
10
10
|
def self.from_xml(xml, auth = nil)
|
11
11
|
new(nil, auth, Nokogiri::XML.parse(xml).root)
|
@@ -2,10 +2,10 @@ module Almodovar
|
|
2
2
|
class ResourceCollection
|
3
3
|
include HttpAccessor
|
4
4
|
include Enumerable
|
5
|
-
|
5
|
+
|
6
6
|
PAGINATION_ENTITIES = ["self::total-entries", "self::link[@rel='next']", "self::link[@rel='prev']"].join('|').freeze
|
7
7
|
|
8
|
-
delegate :inspect, :
|
8
|
+
delegate :inspect, to: :resources
|
9
9
|
|
10
10
|
def initialize(url, auth, xml = nil, options = {})
|
11
11
|
@url = url
|
@@ -13,12 +13,12 @@ module Almodovar
|
|
13
13
|
@xml = xml if options.empty?
|
14
14
|
@options = options
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def create(attrs = {})
|
18
18
|
raise ArgumentError.new("You must specify one only root element which is the type of resource (e.g. `:project => { :name => 'Wadus' }` instead of just `:name => 'Wadus'`)") if attrs.size > 1
|
19
19
|
root, body = attrs.first
|
20
|
-
response = http.post(
|
21
|
-
check_errors(response,
|
20
|
+
response = http.post(@url, body.to_xml(root: root, convert_links: true, skip_links_one_level: true), query_params, { "Content-Type" => "application/xml" })
|
21
|
+
check_errors(response, @url, query_params)
|
22
22
|
Resource.new(nil, @auth, Nokogiri::XML.parse(response.body).root)
|
23
23
|
end
|
24
24
|
|
@@ -41,18 +41,18 @@ module Almodovar
|
|
41
41
|
def prev_page
|
42
42
|
Resource.new(prev_url, @auth) if prev_url
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
private
|
46
|
-
|
46
|
+
|
47
47
|
def resources
|
48
48
|
@resources ||= begin
|
49
49
|
xml.xpath("./*[not(#{PAGINATION_ENTITIES})]").
|
50
50
|
map { |subnode| Resource.new(subnode.at_xpath("./link[@rel='self']").try(:[], "href"), @auth, subnode, @options) }
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def method_missing(meth, *args, &blk)
|
55
55
|
resources.send(meth, *args, &blk)
|
56
56
|
end
|
57
57
|
end
|
58
|
-
end
|
58
|
+
end
|
@@ -15,7 +15,7 @@ module Almodovar
|
|
15
15
|
def update(attrs = {})
|
16
16
|
raise ArgumentError.new("You must specify one only root element which is the type of resource (e.g. `:project => { :name => 'Wadus' }` instead of just `:name => 'Wadus'`)") if attrs.size > 1
|
17
17
|
root, body = attrs.first
|
18
|
-
response = http.put(@url, body.to_xml(:
|
18
|
+
response = http.put(@url, body.to_xml(root: root), {}, { "Content-Type" => "application/xml" })
|
19
19
|
check_errors(response, @url)
|
20
20
|
@xml = Nokogiri::XML.parse(response.body).root
|
21
21
|
end
|
@@ -41,7 +41,7 @@ module Almodovar
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
delegate :to_xml, :
|
44
|
+
delegate :to_xml, to: :xml
|
45
45
|
alias_method :inspect, :to_xml
|
46
46
|
|
47
47
|
def [](key) # for resources with type "document"
|
data/lib/almodovar/to_xml.rb
CHANGED
@@ -6,15 +6,15 @@ module Almodovar
|
|
6
6
|
|
7
7
|
def to_xml_with_links(options = {}, &block)
|
8
8
|
return to_xml_without_links(options, &block) if !options[:convert_links] || options.delete(:skip_links_one_level)
|
9
|
-
options[:builder].tag!(:link, :
|
10
|
-
to_xml_without_links options.merge(:
|
9
|
+
options[:builder].tag!(:link, rel: options[:root]) do |xml|
|
10
|
+
to_xml_without_links options.merge(skip_links_one_level: self.is_a?(Array)), &block
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
class Resource
|
16
16
|
def to_xml(options = {})
|
17
|
-
options[:builder].tag!(:link, :
|
17
|
+
options[:builder].tag!(:link, rel: options[:root], href: url)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/almodovar/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: almodovar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BeBanjo S.L.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: builder
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: addressable
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.3.6
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.3.6
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: json
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
148
|
version: '0'
|
135
149
|
requirements: []
|
136
150
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
151
|
+
rubygems_version: 2.5.1
|
138
152
|
signing_key:
|
139
153
|
specification_version: 4
|
140
154
|
summary: BeBanjo API client
|