generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We now have two CERC APIs (forecast and subscriber), so we need to rename the other one to avoid confusion.
- Loading branch information
1 parent
62dac9a
commit 15f855d
Showing
13 changed files
with
203 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class CercForecastApiClient | ||
class << self | ||
def latest_forecasts(zone = nil) | ||
query = { | ||
"from" => Date.today, | ||
"numdays" => 3, | ||
"zone" => zone | ||
}.compact | ||
|
||
request("getforecast/all", query) | ||
end | ||
|
||
private | ||
|
||
def request(endpoint, query = {}) | ||
base_url = ENV.fetch("CERC_FORECAST_API_HOST_URL") | ||
query["key"] = ENV.fetch("CERC_FORECAST_API_KEY") | ||
HTTParty.get("#{base_url}/#{endpoint}", query: query) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class CercSubscriberApiClient | ||
class << self | ||
def find_subscriber(email:, phone:) | ||
# https://www.airtext.info/API/#/subscribers/get-subscriber | ||
query = { | ||
email: email, | ||
phone: phone | ||
} | ||
|
||
request("find-subscriber", :get, query) | ||
end | ||
|
||
def get_subscriptions(subscriber_id) | ||
# https://www.airtext.info/API/#/subscriptions/get-subscriptions | ||
request("subscriptions/#{subscriber_id}", :get) | ||
end | ||
|
||
def create_subscription(zone:, mode:, ampm:, subscriber_id: nil, phone: nil, email: nil, subscriber_details: nil) | ||
# https://www.airtext.info/API/#/subscriptions/store-airtext-subscriber | ||
query = { | ||
subscriberId: subscriber_id, | ||
zone: zone, | ||
mode: mode, | ||
phone: phone, | ||
email: email, | ||
ampm: ampm, | ||
subscriberDetails: subscriber_details | ||
} | ||
|
||
request("subscriptions", :post, query) | ||
end | ||
|
||
def delete_subscription(subscriber_id, subscription_id) | ||
# https://www.airtext.info/API/#/subscriptions/delete-subscription | ||
query = { | ||
subscriberId: subscriber_id, | ||
subscriptionId: subscription_id | ||
} | ||
|
||
request("subscriptions", :delete, query) | ||
end | ||
|
||
private | ||
|
||
def request(endpoint, method, query = {}) | ||
base_url = ENV.fetch("CERC_SUBSCRIBE_API_HOST_URL") | ||
query["key"] = ENV.fetch("CERC_SUBSCRIBE_API_KEY") | ||
|
||
if method == :post | ||
HTTParty.post("#{base_url}/#{endpoint}", body: query.compact) | ||
else | ||
HTTParty.get("#{base_url}/#{endpoint}", query: query.compact) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
RSpec.describe CercSubscriberApiClient do | ||
around do |example| | ||
env_vars = { | ||
CERC_SUBSCRIBE_API_HOST_URL: "https://example.com", | ||
CERC_SUBSCRIBE_API_KEY: "ABC123" | ||
} | ||
ClimateControl.modify(env_vars) { example.run } | ||
end | ||
|
||
describe ".find_subscriber" do | ||
let(:email) { "[email protected]" } | ||
let(:phone) { "555-555-5555" } | ||
|
||
it "makes a GET request to the find-subscriber endpoint" do | ||
query = {email: email, phone: phone} | ||
|
||
expect(CercSubscriberApiClient).to receive(:request).with("find-subscriber", :get, query) | ||
|
||
CercSubscriberApiClient.find_subscriber(email: email, phone: phone) | ||
end | ||
end | ||
|
||
describe ".get_subscriptions" do | ||
let(:subscriber_id) { 123 } | ||
|
||
it "makes a GET request to the subscriptions/:subscriber_id endpoint" do | ||
expect(CercSubscriberApiClient).to receive(:request).with("subscriptions/#{subscriber_id}", :get) | ||
|
||
CercSubscriberApiClient.get_subscriptions(subscriber_id) | ||
end | ||
end | ||
|
||
describe ".create_subscription" do | ||
let(:email) { "[email protected]" } | ||
let(:phone) { "555-555-5555" } | ||
let(:zone) { "zone" } | ||
let(:mode) { "mode" } | ||
let(:ampm) { "ampm" } | ||
let(:subscriber_id) { 123 } | ||
let(:subscriber_details) { {} } | ||
|
||
it "makes a POST request to the subscriptions endpoint" do | ||
query = { | ||
subscriberId: subscriber_id, | ||
zone: zone, | ||
mode: mode, | ||
phone: phone, | ||
email: email, | ||
ampm: ampm, | ||
subscriberDetails: subscriber_details | ||
} | ||
|
||
expect(CercSubscriberApiClient).to receive(:request).with("subscriptions", :post, query) | ||
|
||
CercSubscriberApiClient.create_subscription(subscriber_id: subscriber_id, zone: zone, mode: mode, ampm: ampm, phone: phone, email: email, subscriber_details: subscriber_details) | ||
end | ||
end | ||
|
||
describe ".delete_subscription" do | ||
it "makes a DELETE request to the subscriptions endpoint" do | ||
subscriber_id = 123 | ||
subscription_id = 456 | ||
query = {subscriberId: subscriber_id, subscriptionId: subscription_id} | ||
|
||
expect(CercSubscriberApiClient).to receive(:request).with("subscriptions", :delete, query) | ||
|
||
CercSubscriberApiClient.delete_subscription(subscriber_id, subscription_id) | ||
end | ||
end | ||
|
||
describe ".request" do | ||
it "makes a GET request to the base_url and endpoint" do | ||
endpoint = "find-subscriber" | ||
|
||
expect(HTTParty).to receive(:get).with("https://example.com/#{endpoint}", query: {"key" => "ABC123"}) | ||
|
||
CercSubscriberApiClient.send(:request, endpoint, :get) | ||
end | ||
|
||
it "makes a POST request to the base_url and endpoint" do | ||
endpoint = "subscriptions" | ||
|
||
expect(HTTParty).to receive(:post).with("https://example.com/#{endpoint}", body: {"key" => "ABC123"}) | ||
|
||
CercSubscriberApiClient.send(:request, endpoint, :post) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters