Class: Apical::Resource

Inherits:
Object
  • 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

#acceptObject



64
65
66
# File 'lib/apical/resource.rb', line 64

def accept
  @accept || @runner.accept
end

#after_blocksObject

Returns the value of attribute after_blocks.



15
16
17
# File 'lib/apical/resource.rb', line 15

def after_blocks
  @after_blocks
end

#before_blocksObject

Returns the value of attribute before_blocks.



15
16
17
# File 'lib/apical/resource.rb', line 15

def before_blocks
  @before_blocks
end

#content_typeObject



60
61
62
# File 'lib/apical/resource.rb', line 60

def content_type
  @content_type || @runner.content_type
end

#custom_headersObject

Returns the value of attribute custom_headers.



15
16
17
# File 'lib/apical/resource.rb', line 15

def custom_headers
  @custom_headers
end

#descObject

Returns the value of attribute desc.



15
16
17
# File 'lib/apical/resource.rb', line 15

def desc
  @desc
end

#methodObject

Returns the value of attribute method.



15
16
17
# File 'lib/apical/resource.rb', line 15

def method
  @method
end

#paramsObject

Returns the value of attribute params.



15
16
17
# File 'lib/apical/resource.rb', line 15

def params
  @params
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/apical/resource.rb', line 15

def path
  @path
end

Instance Method Details

#accept_headerObject



76
77
78
# File 'lib/apical/resource.rb', line 76

def accept_header
  accept.header
end

#auth_headerObject



68
69
70
# File 'lib/apical/resource.rb', line 68

def auth_header
  @auth_header || @runner.auth_header
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

#content_type_headerObject



72
73
74
# File 'lib/apical/resource.rb', line 72

def content_type_header
  content_type.header
end

#custom_header(name) ⇒ Object



122
123
124
# File 'lib/apical/resource.rb', line 122

def custom_header(name)
  call_if_proc @custom_headers[name]
end

#filtered_paramsObject



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

#formatted_paramsObject



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

#formatted_responseObject



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_pathObject



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_requestObject



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.header name, custom_header(name)
  end

  adapter.header "Accept", accept_header              if accept_header
  adapter.header "Content-Type", content_type_header  if content_type_header

  adapter.send(@method.downcase.intern, interpolated_path, formatted_params)
end

#path_capturesObject



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_bodyObject



102
103
104
# File 'lib/apical/resource.rb', line 102

def response_body
  @response.body
end

#runObject



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