api_recipes 0.7.1 → 2.6.2

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