Skip to content

Commit a6660ca

Browse files
Hub API (#2818)
1 parent 17aacb1 commit a6660ca

7 files changed

Lines changed: 93 additions & 0 deletions

File tree

.sample.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ GITHUB_ACCESS_TOKEN=github_access_token_for_octokit
1111
GITHUB_KEY=github_key_for_oauth_application
1212
GITHUB_SECRET=github_secret_for_oauth_application
1313
GOOGLE_TAG_MANAGER_KEY=sample_gtm_key
14+
HUB_API_TOKEN=abcdef1234567890
15+
HUB_API_URL=https://hub.thoughtbot.com/api
1416
KISSMETRICS_API_KEY=api_key
1517
PAYPAL_PASSWORD=password
1618
PAYPAL_SIGNATURE=signature

config/initializers/hub.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require Rails.root.join("lib/hub")
2+
3+
Hub.configure do |config|
4+
config.url = ENV["HUB_API_URL"]
5+
config.token = ENV["HUB_API_TOKEN"]
6+
end

lib/hub.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Hub
2+
# https://github.com/thoughtbot/hub/blob/main/doc/api/restful/README.md
3+
4+
include ActiveSupport::Configurable
5+
end

lib/hub/opportunities.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Hub
2+
module Opportunities
3+
# @see https://github.com/thoughtbot/hub/blob/main/doc/api/restful/opportunities.md#create
4+
# @param attributes [Hash]
5+
# @return [Hash]
6+
def self.create(attributes = {})
7+
Request.new.post("opportunities", opportunity: attributes)
8+
end
9+
end
10+
end

lib/hub/request.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Hub
2+
class Request
3+
def post(...)
4+
response = connection.post(...)
5+
6+
unless response.success?
7+
raise ActionController::RoutingError, response.reason_phrase
8+
end
9+
10+
response.body
11+
end
12+
13+
private
14+
15+
def connection
16+
Faraday.new(
17+
url: Hub.config.url,
18+
# https://github.com/thoughtbot/hub/blob/main/doc/api/restful/README.md#all-requests
19+
headers: {
20+
"Accept" => "application/vnd.hub+json; version=1",
21+
"Authorization" => "Token token=#{Hub.config.token}"
22+
}
23+
) do |connection|
24+
connection.request :json
25+
connection.response :json
26+
end
27+
end
28+
end
29+
end

spec/lib/hub/opportunities_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "rails_helper"
2+
3+
describe Hub::Opportunities do
4+
describe ".create" do
5+
it "makes a POST request to the Hub API with the correct body" do
6+
allow(Hub.config).to receive(:url).and_return("https://hub.thoughtbot.com/api")
7+
expected_request =
8+
stub_request(:post, "https://hub.thoughtbot.com/api/opportunities")
9+
.with(body: {opportunity: {email: "example@example.com"}})
10+
.to_return_json(body: {"opportunity" => {"id" => 1}})
11+
12+
described_class.create({email: "example@example.com"})
13+
14+
expect(expected_request).to have_been_requested
15+
end
16+
end
17+
end

spec/lib/hub/request_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "rails_helper"
2+
3+
describe Hub::Request do
4+
describe "#post" do
5+
it "makes a POST request to the Hub API with the correct headers and body" do
6+
allow(Hub.config).to receive(:url).and_return("https://hub.thoughtbot.com/api")
7+
allow(Hub.config).to receive(:token).and_return("fake_token")
8+
expected_request =
9+
stub_request(:post, "https://hub.thoughtbot.com/api/books")
10+
.with(
11+
headers: {
12+
"Accept" => "application/vnd.hub+json; version=1",
13+
"Authorization" => "Token token=fake_token"
14+
},
15+
body: {book: {title: "Example Title"}}
16+
)
17+
.to_return_json(body: {"book" => {"id" => 1}})
18+
19+
described_class.new.post("books", {book: {title: "Example Title"}})
20+
21+
expect(expected_request).to have_been_requested
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)