Skip to content

Commit c8914a4

Browse files
authored
Merge pull request #172 from joyofrails/feat/plausible-client
Add plausible api client for color scheme interaction
2 parents e13e01b + 8e14416 commit c8914a4

File tree

13 files changed

+965
-1
lines changed

13 files changed

+965
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ group :development, :test do
7777
gem "reek", require: false # Code smell detector for Ruby [https://github.com/troessner/reek]
7878
gem "rspec-rails" # RSpec for Rails [https://github.com/rspec/rspec-rails]
7979
gem "standard", require: false # Ruby style guide, linter, and formatter [https://github.com/testdouble/standard]
80+
gem "vcr", require: false # Record your test suite's HTTP interactions and replay them during future test runs [https://github.com/vcr/vcr]
8081
gem "w3c_validators", require: false # W3C HTML and CSS validators [https://github.com/w3c-validators/w3c_validators]
8182
end
8283

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ GEM
227227
globalid (1.2.1)
228228
activesupport (>= 6.1)
229229
hashdiff (1.1.0)
230-
honeybadger (5.8.0)
230+
honeybadger (5.14.1)
231231
i18n (1.14.5)
232232
concurrent-ruby (~> 1.0)
233233
importmap-rails (2.0.1)
@@ -507,6 +507,7 @@ GEM
507507
tzinfo-data (1.2024.1)
508508
tzinfo (>= 1.0.0)
509509
unicode-display_width (2.5.0)
510+
vcr (6.2.0)
510511
vite_rails (3.0.17)
511512
railties (>= 5.1, < 8)
512513
vite_ruby (~> 3.0, >= 3.2.2)
@@ -599,6 +600,7 @@ DEPENDENCIES
599600
stimulus-rails
600601
turbo-rails
601602
tzinfo-data
603+
vcr
602604
vite_rails
603605
w3c_validators
604606
warden

app/clients/README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# API Clients
2+
3+
This directory is for API clients.
4+
5+
Jumpstart Pro includes an API client for working with JSON APIs. It handles several things for you:
6+
7+
* The Authorization header with API token
8+
* Content-Type JSON header
9+
* JSON response bodies parsed automatically
10+
* Raises errors on 4XX or 5XX responses
11+
12+
Why would you need this?
13+
Often times Rubygems for APIs fall out of date or go unmaintained. Managing your own integration allows you to only implement the endpoints you need and easily maintain your integration.
14+
15+
### Usage
16+
17+
Use the generator to create a new API client:
18+
19+
```bash
20+
rails g api_client Sendfox
21+
```
22+
23+
This will generate `app/clients/sendfox_client.rb`
24+
25+
Edit the API client file to add the base URI and define your API endpoints
26+
27+
Here are some example API endpoints for the Sendfox API implemented with the API client:
28+
29+
```ruby
30+
class SendfoxClient < ApplicationClient
31+
BASE_URI = "https://api.sendfox.com"
32+
33+
def me
34+
get("/me")
35+
end
36+
37+
def lists
38+
get("/lists")
39+
end
40+
41+
def list(id)
42+
get("/lists/#{id}")
43+
end
44+
45+
def create_list(name:)
46+
post("/lists", name: name)
47+
end
48+
49+
def remove_contact(list_id:, contact_id:)
50+
delete("/lists/#{list_id}/contacts/#{contact_id}")
51+
end
52+
end
53+
```
54+
55+
### ApplicationClient
56+
57+
`ApplicationClient` is the base class and defines helpers for making HTTP requests and handles authorization headers and parsing JSON responses.
58+
59+
#### HTTP methods
60+
61+
There are helper methods for the standard HTTP request types for APIs:
62+
63+
`get`
64+
`post`
65+
`patch`
66+
`put`
67+
`delete`
68+
69+
#### Overrides
70+
71+
You can override the methods to match the APIs you're integrating with.
72+
73+
For example, you can override the default headers and query params. This example could be used to connect to an XML API that uses a query parameter to authenticate.
74+
75+
```ruby
76+
class XmlExampleClient < ApplicationClient
77+
def default_headers
78+
{
79+
"Content-Type" => "application/xml"
80+
}
81+
end
82+
83+
def default_query_params
84+
{
85+
token: token
86+
}
87+
end
88+
end
89+
```
90+
91+
### Multipart Form Data
92+
93+
When working with APIs that require file uploads or multipart form data, the `ApplicationClient` can also handle these types of requests. You can use the `Net::HTTPHeader#set_form` method to set form data, including file uploads.
94+
95+
#### Usage
96+
97+
To send a multipart form data request, you need to pass the `form_data` parameter when calling the `post`, `patch`, or `put` methods in your custom API client. The `form_data` parameter should be an Enumerable containing field names and their values. You can use a hash or an array for `form_data`.
98+
99+
For example, to upload a file:
100+
101+
```ruby
102+
class MyApiClient < ApplicationClient
103+
BASE_URI = "https://api.example.com"
104+
105+
def upload_file(file)
106+
form_data = {
107+
"field1" => "value1",
108+
"file" => file
109+
}
110+
111+
post("/upload", form_data: form_data)
112+
end
113+
end
114+
```
115+
116+
In this example, file should be a File or IO-like object.
117+
118+
If you need to set additional options for the file upload, such as the filename or content type, you can include them in the form data like this:
119+
120+
121+
```rb
122+
form_data = {
123+
"field1" => "value1",
124+
"file" => [file, { filename: "custom_filename.txt", content_type: "text/plain" }]
125+
}
126+
```

0 commit comments

Comments
 (0)