api_recipes 0.6.2 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,165 +0,0 @@
1
- module ApiRecipes
2
- class Resource
3
-
4
- attr_accessor :authorization, :basic_auth
5
-
6
- def initialize(name, endpoint, routes = {})
7
- @name = name
8
- @routes = routes
9
- @endpoint = endpoint
10
-
11
- generate_routes
12
- end
13
-
14
- private
15
-
16
- def build_path(route_name, route_attributes, provided_params)
17
- path = route_attributes[:path] || ''
18
-
19
- required_params_for_path(path).each do |rp|
20
- unless p = provided_params.delete(rp)
21
- raise MissingRouteAttribute.new(@name, route_name, rp)
22
- end
23
- path.gsub! ":#{rp}", p.to_s
24
- end
25
- path = "#{settings[:base_path]}#{settings[:api_version]}/#{@name}#{path}"
26
- return path, provided_params
27
- end
28
-
29
- def build_request
30
-
31
-
32
- end
33
-
34
- def build_uri_from(path)
35
- attrs = {
36
- scheme: settings[:protocol],
37
- host: settings[:host],
38
- port: port,
39
- path: path
40
- }
41
- URI::Generic.build attrs
42
- end
43
-
44
- def check_response_code(route, route_attributes, response)
45
- # Check if :ok_code is present, check the response
46
- if ok_code = route_attributes[:ok_code]
47
- code = response.code
48
- # If the code does not match, apply the requested strategy (see FAIL_OPTIONS)
49
- unless code == ok_code
50
- case settings[:on_nok_code]
51
- when :do_nothing
52
- when :raise
53
- raise ResponseCodeNotAsExpected.new(nil, @name, route, ok_code, code, response.body)
54
- when :return_false
55
- return false
56
- end
57
- end
58
- end
59
- end
60
-
61
- def encode_residual_params(route_attributes, residual_params)
62
- # If :encode_params_as is specified and avalable use it
63
- if Settings::AVAILABLE_PARAMS_ENCODINGS.include? route_attributes[:encode_params_as].to_s
64
- { route_attributes[:encode_params_as].to_sym => residual_params }
65
- else
66
- # default to query string params (get) or json (other methods)
67
- case route_attributes[:method].to_sym
68
- when :get
69
- { params: residual_params }
70
- when :post, :put, :patch, :delete
71
- { json: residual_params }
72
- end
73
- end
74
- end
75
-
76
- def extract_headers
77
- settings[:default_headers] || {}
78
- end
79
-
80
- # Generate routes some_endpoint.some_resource.some_route methods
81
- # e.g. webapp.alarms.index
82
- def generate_routes
83
- @routes.each do |route, attrs|
84
- if route.eql? @name
85
- raise RouteNameClashError.new(route, @name)
86
- end
87
- unless respond_to? route.to_sym
88
- define_singleton_method route.to_sym do |*params, &block|
89
- start_request route, attrs, *params, &block
90
- end
91
- else
92
- raise RouteNameClashWithExistentMethod.new(@name, route)
93
- end
94
- end
95
- self
96
- end
97
-
98
- def per_kind_timeout
99
- settings.fetch(:timeout, ApiRecipes::Settings::GLOBAL_TIMEOUT)/3.0
100
- end
101
-
102
- def port
103
- settings[:port] || case settings[:protocol]
104
- when 'http'
105
- 80
106
- when 'https'
107
- 443
108
- end
109
- end
110
-
111
- def request_with_auth
112
- req = HTTP
113
- req = req.headers(extract_headers)
114
- .timeout(
115
- :global,
116
- write: per_kind_timeout,
117
- connect: per_kind_timeout,
118
- read: per_kind_timeout
119
- )
120
-
121
- if basic_auth
122
- req = req.basic_auth basic_auth
123
- elsif ba = @endpoint.basic_auth
124
- req = req.basic_auth ba
125
- end
126
- if authorization
127
- req = req.auth authorization
128
- elsif auth = @endpoint.authorization
129
- req = req.auth auth
130
- end
131
- req
132
- end
133
-
134
- def required_params_for_path(path)
135
- path.scan(/:(\w+)/).flatten.map { |p| p.to_sym }
136
- end
137
-
138
- def settings
139
- @endpoint.configs
140
- end
141
-
142
- def start_request(route, route_attributes, *pars)
143
- unless route_attributes
144
- route_attributes = {}
145
- end
146
- # Merge route attributes with defaults and deep clone route attributes
147
- route_attributes = Marshal.load(Marshal.dump(Settings::DEFAULT_ROUTE_ATTRIBUTES.merge(route_attributes).deep_symbolize_keys))
148
-
149
- params = pars.extract_options!
150
- path, residual_params = build_path(route, route_attributes, params)
151
- residual_params = nil unless residual_params.any?
152
- uri = build_uri_from path
153
-
154
- response = request_with_auth.send(route_attributes[:method], uri, encode_residual_params(route_attributes, residual_params))
155
- check_response_code route, route_attributes, response
156
-
157
- if block_given?
158
- body = Oj.load(response.body.to_s)
159
- yield body, response.code, response.reason
160
- else
161
- response
162
- end
163
- end
164
- end
165
- end