File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ GITHUB_ACCESS_TOKEN=github_access_token_for_octokit
1111GITHUB_KEY = github_key_for_oauth_application
1212GITHUB_SECRET = github_secret_for_oauth_application
1313GOOGLE_TAG_MANAGER_KEY = sample_gtm_key
14+ HUB_API_TOKEN = abcdef1234567890
15+ HUB_API_URL = https://hub.thoughtbot.com/api
1416KISSMETRICS_API_KEY = api_key
1517PAYPAL_PASSWORD = password
1618PAYPAL_SIGNATURE = signature
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ module Hub
2+ # https://github.com/thoughtbot/hub/blob/main/doc/api/restful/README.md
3+
4+ include ActiveSupport ::Configurable
5+ end
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments