Class: Skink::Client::Response
- Inherits:
-
Object
- Object
- Skink::Client::Response
show all
- Includes:
- Utils
- Defined in:
- lib/skink/client/response.rb
Instance Method Summary
collapse
Methods included from Utils
#normalize_header_name
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/skink/client/response.rb', line 114
def method_missing(name, *args)
if name.match(/has_(\w+)_header\?/)
[($1)] === args.first
else
super
end
end
|
Instance Method Details
#body ⇒ Object
43
44
45
|
# File 'lib/skink/client/response.rb', line 43
def body
raise NotImplementedError
end
|
22
23
24
25
26
27
28
29
|
# File 'lib/skink/client/response.rb', line 22
def
case
when String
[.keys.detect {|key| key.match(/^#{}$/i)}] rescue nil
when Symbol
[.keys.detect {|key| key.match(/^#{.to_s.gsub('_', '-')}$/i)}] rescue nil
end
end
|
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/skink/client/response.rb', line 31
def
if ()
true
elsif .respond_to? :keys
.all? do |key, value|
value === (key)
end
else
false
end
end
|
#has_jsonpath?(path, value = nil) ⇒ Boolean
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/skink/client/response.rb', line 76
def has_jsonpath? path, value = nil
elems = jsonpath(path)
if value
block = ->(elem) {value === elem}
else
block = nil
end
elems.any?(&block)
end
|
example match keys for the link header: :url => “example.com”, :rel => “search”
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/skink/client/response.rb', line 103
def (opts=nil)
if opts.nil?
links.any?
else
opts = Hash[opts.map{|(k,v)| [k.to_sym,v]}]
links.any? do |link|
link == link.deep_merge(opts)
end
end
end
|
#has_xpath?(path, value = nil) ⇒ Boolean
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/skink/client/response.rb', line 55
def has_xpath? path, value = nil
elems = xpath(path)
elems = [elems] unless elems.respond_to? :any?
if value
block = ->(elem) {value === elem.to_s}
else
block = nil
end
elems.any?(&block)
end
|
18
19
20
|
# File 'lib/skink/client/response.rb', line 18
def
raise NotImplementedError
end
|
#json_doc ⇒ Object
68
69
70
|
# File 'lib/skink/client/response.rb', line 68
def json_doc
@json_doc ||= MultiJson.load body
end
|
#jsonpath(path) ⇒ Object
72
73
74
|
# File 'lib/skink/client/response.rb', line 72
def jsonpath path
JsonPath.new(path).on json_doc
end
|
87
88
89
|
# File 'lib/skink/client/response.rb', line 87
def
raise NotImplementedError
end
|
#links ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/skink/client/response.rb', line 91
def links
if
LinkHeader.parse().to_a.map do |linkarr|
with_sym_keys = Hash[linkarr[1].map{|(k,v)| [k.to_sym,v]}]
{url: linkarr[0]}.merge with_sym_keys
end
end
end
|
#status_code ⇒ Object
14
15
16
|
# File 'lib/skink/client/response.rb', line 14
def status_code
raise NotImplementedError
end
|
#xml_doc ⇒ Object
47
48
49
|
# File 'lib/skink/client/response.rb', line 47
def xml_doc
@xml_doc ||= Nokogiri::XML.parse body
end
|
#xpath(path) ⇒ Object
51
52
53
|
# File 'lib/skink/client/response.rb', line 51
def xpath path
xml_doc.xpath path
end
|