|
1 | | -require 'faraday' |
2 | | -require 'faraday/request/json' |
3 | | -require 'faraday/response/json' |
4 | | -require 'faraday/response/raise_error' |
5 | | - |
6 | 1 | module TestTrack::Resource |
7 | 2 | extend ActiveSupport::Concern |
8 | 3 |
|
9 | 4 | include ActiveModel::API |
10 | 5 | include ActiveModel::Attributes |
11 | 6 |
|
12 | | - class << self |
13 | | - attr_writer :connection |
14 | | - |
15 | | - # FIXME: Raise UnrecoverableConnectivityError when we have a server error |
16 | | - # FIXME: Remove `content_type` option and respect `Content-Type` header |
17 | | - def connection |
18 | | - @connection ||= Faraday.new(url: ENV['TEST_TRACK_API_URL']) do |conn| |
19 | | - conn.use Faraday::Request::Json |
20 | | - conn.use Faraday::Response::Json, content_type: [] |
21 | | - conn.use Faraday::Response::RaiseError |
22 | | - # Number of seconds to wait for the connection to open. |
23 | | - conn.options[:open_timeout] = (ENV['TEST_TRACK_OPEN_TIMEOUT'] || 2).to_i |
24 | | - # Number of seconds to wait for one block to be read (via one read(2) call). |
25 | | - conn.options[:timeout] = (ENV['TEST_TRACK_TIMEOUT'] || 4).to_i |
26 | | - end |
27 | | - end |
28 | | - end |
29 | | - |
30 | | - module Helpers |
31 | | - private |
32 | | - |
33 | | - def fake_requests? |
34 | | - !TestTrack.enabled? |
35 | | - end |
36 | | - |
37 | | - def request(method:, path:, fake:, body: nil, headers: nil) |
38 | | - # Ensure that fake responses are consistent with real responses |
39 | | - return JSON.parse(JSON.generate(fake)) if fake_requests? |
40 | | - |
41 | | - response = TestTrack::Resource.connection.run_request(method, path, body, headers) |
42 | | - response.body |
43 | | - end |
44 | | - end |
45 | | - |
46 | | - include Helpers |
47 | | - |
48 | | - module ClassMethods |
49 | | - include Helpers |
50 | | - end |
51 | | - |
52 | 7 | private |
53 | 8 |
|
54 | 9 | def _assign_attribute(name, value) |
|
0 commit comments