Class: Apical::Resource
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/apical/resource.rb
Defined Under Namespace
Classes: Blueprint
Constant Summary
collapse
- PATH_CAPTURE_REGEXP =
/:[^\.\/]+/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(runner, path, &block) ⇒ Resource
Returns a new instance of Resource.
5
6
7
8
9
10
11
12
13
|
# File 'lib/apical/resource.rb', line 5
def initialize(runner, path, &block)
@runner = runner
@path = path
@params = nil
@before_blocks = []
@after_blocks = []
@custom_headers = {}
Blueprint.new self, &block
end
|
Instance Attribute Details
#accept ⇒ Object
64
65
66
|
# File 'lib/apical/resource.rb', line 64
def accept
@accept || @runner.accept
end
|
#after_blocks ⇒ Object
Returns the value of attribute after_blocks.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def after_blocks
@after_blocks
end
|
#before_blocks ⇒ Object
Returns the value of attribute before_blocks.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def before_blocks
@before_blocks
end
|
#content_type ⇒ Object
60
61
62
|
# File 'lib/apical/resource.rb', line 60
def content_type
@content_type || @runner.content_type
end
|
Returns the value of attribute custom_headers.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def
@custom_headers
end
|
#desc ⇒ Object
Returns the value of attribute desc.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def desc
@desc
end
|
#method ⇒ Object
Returns the value of attribute method.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def method
@method
end
|
#params ⇒ Object
Returns the value of attribute params.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def params
@params
end
|
#path ⇒ Object
Returns the value of attribute path.
15
16
17
|
# File 'lib/apical/resource.rb', line 15
def path
@path
end
|
Instance Method Details
76
77
78
|
# File 'lib/apical/resource.rb', line 76
def
accept.
end
|
68
69
70
|
# File 'lib/apical/resource.rb', line 68
def
@auth_header || @runner.
end
|
#call_if_proc(proc_or_obj) ⇒ Object
118
119
120
|
# File 'lib/apical/resource.rb', line 118
def call_if_proc(proc_or_obj)
proc_or_obj.is_a?(Proc) ? instance_eval(&proc_or_obj) : proc_or_obj
end
|
72
73
74
|
# File 'lib/apical/resource.rb', line 72
def
content_type.
end
|
122
123
124
|
# File 'lib/apical/resource.rb', line 122
def (name)
call_if_proc @custom_headers[name]
end
|
#filtered_params ⇒ Object
87
88
89
|
# File 'lib/apical/resource.rb', line 87
def filtered_params
params.reject {|k, v| path_captures.include?(k) or path_captures.include?(k.to_s)}
end
|
91
92
93
94
95
|
# File 'lib/apical/resource.rb', line 91
def formatted_params
return {} unless params
return filtered_params unless accept
accept.generate(filtered_params)
end
|
97
98
99
100
|
# File 'lib/apical/resource.rb', line 97
def formatted_response
return response_body unless content_type
content_type.generate(content_type.parse(response_body))
end
|
#interpolated_path ⇒ Object
112
113
114
115
116
|
# File 'lib/apical/resource.rb', line 112
def interpolated_path
@path.gsub(PATH_CAPTURE_REGEXP) do |match|
params[match.gsub(':', '').intern]
end
end
|
#make_request ⇒ Object
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/apical/resource.rb', line 126
def make_request
@custom_headers.each do |name, value|
adapter. name, (name)
end
adapter. "Accept", if
adapter. "Content-Type", if
adapter.send(@method.downcase.intern, interpolated_path, formatted_params)
end
|
#path_captures ⇒ Object
108
109
110
|
# File 'lib/apical/resource.rb', line 108
def path_captures
@path.match(PATH_CAPTURE_REGEXP).to_a.map {|m| m.gsub(':', '').intern }
end
|
#response_body ⇒ Object
102
103
104
|
# File 'lib/apical/resource.rb', line 102
def response_body
@response.body
end
|
#run ⇒ Object
80
81
82
83
84
85
|
# File 'lib/apical/resource.rb', line 80
def run
@before_blocks.each {|block| instance_eval &block }
@params = instance_eval(&@params) if @params.is_a?(Proc)
@response = make_request
@after_blocks.each {|block| instance_eval &block }
end
|