sofia 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +63 -12
- data/lib/sofia/response.rb +1 -1
- data/lib/sofia/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a7d9da1b3491c63c1c05616b6ccba0ba8a9355204dfdc6db8b0935943386d963
|
|
4
|
+
data.tar.gz: 98721c8043ae738d53b0014eea9b45918aa4f79fed17b37e5a8142d2549ba3a5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7a7a098c6299be5e9aec18f6cbecd5eaaac580c3051a13d13155f98c676475c86b10bc1f66a8c5ceb8502f50f5b8832c2d589148322cb75b1e2d169f5fcd959
|
|
7
|
+
data.tar.gz: 5ff87ca97606ceae67bef7e9fcfeb2188fe9b32df455ccb055a61eebe5b4944253a43774b7df6a88b7a23cbc72849becd35817d2772b607cd325bf6b6cbae654
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -1,24 +1,75 @@
|
|
|
1
1
|
# Sofia
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A simple HTTP client abstraction layer for Ruby, similar to `Faraday`. Sofia provides an abstraction layer so you can swap the underlying HTTP library without changing your application code.
|
|
4
|
+
|
|
5
|
+
Sofia is currently implemented in my integration with Espago Payment System [Shopik](https://github.com/KubaJadrzak/Shopik) alongside my own HTTP client library [Soren](https://github.com/KubaJadrzak/soren), and everything seems to be working pretty good :P
|
|
4
6
|
|
|
5
7
|
# How it works
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
Initialize a client with a `base_url` and an optional `adapter` (defaults to `:net_http`):
|
|
10
|
+
|
|
8
11
|
```rb
|
|
9
|
-
|
|
12
|
+
@client = Sofia.new(base_url: base_url, adapter: adapter)
|
|
10
13
|
```
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
Call the desired HTTP method via `send` with a configuration block. The supported methods are `get`, `post`, `put`, `patch`, and `delete`:
|
|
16
|
+
|
|
13
17
|
```rb
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
response = @client.send(method) do |req|
|
|
19
|
+
req.path = path
|
|
20
|
+
req.headers['Accept'] = 'application/json'
|
|
21
|
+
req.headers['Authorization'] = "Basic #{encoded_credentials}"
|
|
22
|
+
req.body = body if body
|
|
23
|
+
end
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
At the current moment Sofia supports only JSON!!!
|
|
27
|
+
|
|
28
|
+
# Errors
|
|
29
|
+
|
|
30
|
+
Response codes in the `400–499` and `500–599` ranges are not raised as errors — inspect `response.status`, `response.client_error?`, or `response.server_error?` yourself.
|
|
31
|
+
|
|
32
|
+
| Error | Cause |
|
|
33
|
+
|---|---|
|
|
34
|
+
| `Sofia::Error::TimeoutError` | Read, write, or connection timeout |
|
|
35
|
+
| `Sofia::Error::ConnectionFailed` | DNS failure, refused connection, network error |
|
|
36
|
+
| `Sofia::Error::SSLError` | TLS handshake failure |
|
|
37
|
+
| `Sofia::Error::ParserError` | Response body is not valid JSON |
|
|
38
|
+
|
|
39
|
+
## Adapters
|
|
40
|
+
|
|
41
|
+
Sofia supports two adapter: `:net_http` as well as `:soren`. [Soren](https://github.com/KubaJadrzak/soren) is my own simple HTTP client library :P.
|
|
42
|
+
|
|
43
|
+
```rb
|
|
44
|
+
# Default — Net::HTTP
|
|
45
|
+
client = Sofia.new(base_url: 'https://api.example.com', adapter: :net_http)
|
|
46
|
+
|
|
47
|
+
# Soren
|
|
48
|
+
client = Sofia.new(base_url: 'https://api.example.com', adapter: :soren)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Timeouts
|
|
52
|
+
|
|
53
|
+
Timeouts can be configured per client via the `options:` keyword:
|
|
54
|
+
|
|
55
|
+
```rb
|
|
56
|
+
client = Sofia.new(
|
|
57
|
+
base_url: 'https://api.example.com',
|
|
58
|
+
adapter: :net_http,
|
|
59
|
+
options: {
|
|
60
|
+
read_timeout: 60, # seconds — default 30
|
|
61
|
+
write_timeout: 30, # seconds — default 30
|
|
62
|
+
connection_timeout: 5, # seconds — default 10
|
|
63
|
+
},
|
|
64
|
+
)
|
|
20
65
|
```
|
|
21
|
-
|
|
66
|
+
|
|
67
|
+
Both adapters support all three timeout values.
|
|
68
|
+
|
|
69
|
+
## Example
|
|
70
|
+
|
|
71
|
+
This is an example of the entire flow from my [Shopik](https://github.com/KubaJadrzak/Shopik) project, where both `Sofia` HTTP client abstraction layer and `Soren` HTTP client library are implemented:
|
|
72
|
+
|
|
22
73
|
```rb
|
|
23
74
|
class EspagoClient
|
|
24
75
|
|
|
@@ -27,7 +78,7 @@ class EspagoClient
|
|
|
27
78
|
@user = Rails.application.credentials.dig(:espago, :app_id)
|
|
28
79
|
@password = Rails.application.credentials.dig(:espago, :password)
|
|
29
80
|
|
|
30
|
-
@client = Sofia.new(base_url: base_url)
|
|
81
|
+
@client = Sofia.new(base_url: base_url, adapter: :soren)
|
|
31
82
|
end
|
|
32
83
|
|
|
33
84
|
def send(path, body: nil, method: :get)
|
data/lib/sofia/response.rb
CHANGED
data/lib/sofia/version.rb
CHANGED