Class: Appfuel::WebApi::HttpModel
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#app_container, #feature_name, included, #qualify_container_key
Constructor Details
#initialize(adapter = RestClient, config = self.class.load_config) ⇒ HttpModel
Returns a new instance of HttpModel.
34
35
36
37
38
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 34
def initialize(adapter = RestClient, config = self.class.load_config)
@config = validate_config(config)
@uri = create_uri(@config[:url])
@adapter = adapter
end
|
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
32
33
34
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 32
def adapter
@adapter
end
|
#config ⇒ Object
Returns the value of attribute config.
32
33
34
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 32
def config
@config
end
|
#content_type ⇒ Object
Returns the value of attribute content_type.
32
33
34
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 32
def content_type
@content_type
end
|
#uri ⇒ Object
Returns the value of attribute uri.
32
33
34
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 32
def uri
@uri
end
|
Class Method Details
.config_key(value = nil) ⇒ Object
14
15
16
17
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 14
def config_key(value = nil)
return @config_key if value.nil?
@config_key = value.to_sym
end
|
.container_class_type ⇒ Object
10
11
12
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 10
def container_class_type
'web_api'
end
|
.inherited(klass) ⇒ Object
27
28
29
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 27
def inherited(klass)
stage_class_for_registration(klass)
end
|
.load_config ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 19
def load_config
config = app_container[:config]
unless config.key?(config_key)
fail "[web_api] config key (#{config_key}) not found - #{self}"
end
config[config_key]
end
|
Instance Method Details
#handle_http_error(code, body, url, msg) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 75
def handle_http_error(code, body, url, msg)
if body.is_a?(Hash)
body = body.map{|k,v| "#{k}: #{v}"}.join('&')
end
str = "[#{url} #{code}] #{msg} #{body}"
raise str
end
|
#handle_response(response, headers = {}) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 83
def handle_response(response, = {})
if content_type == :json && [:content_type] == :json
data = response.body
return data.empty? ? {} : json(response.body)
end
response.body
end
|
#json(data) ⇒ Object
92
93
94
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 92
def json(data)
JSON.parse(data)
end
|
#request(method, path, options = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 48
def request(method, path, options = {})
add_content_type(options)
http_url = url(path)
if options[:relative_url] === false
http_url = path
options.delete(:relative_url)
end
begin
data = options.merge({method: method, url: http_url })
response = adapter::Request.execute(data)
response = handle_response(response, options[:headers])
rescue RestClient::ExceptionWithResponse => err
handle_method = "handle_#{err.http_code}"
if respond_to?(handle_method)
return send(handle_method, err)
else
handle_http_error(err.http_code, {}, http_url, err.message)
end
end
response
end
|
#url(path) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/appfuel/storage/web_api/http_model.rb', line 40
def url(path)
if path.start_with?("/")
path.slice!(0)
end
uri.to_s + "#{path}"
end
|