-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First sketch of multi-provider search
- Loading branch information
1 parent
8ad506e
commit f7fda9e
Showing
6 changed files
with
97 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "faraday" | ||
|
||
class ArgyleService | ||
ENVIRONMENTS = { | ||
sandbox: { | ||
base_url: "https://api-sandbox.argyle.com/v2", | ||
api_key_id: ENV["ARGYLE_API_TOKEN_SANDBOX_ID"], | ||
api_key_secret: ENV["ARGYLE_API_TOKEN_SANDBOX_SECRET"] | ||
} | ||
} | ||
|
||
def initialize(environment, api_key_id = nil, api_key_secret = nil) | ||
@api_key_id = api_key_id || ENVIRONMENTS.fetch(environment.to_sym)[:api_key_id] | ||
@api_key_secret = api_key_secret || ENVIRONMENTS.fetch(environment.to_sym)[:api_key_secret] | ||
@environment = ENVIRONMENTS.fetch(environment.to_sym) { |env| raise KeyError.new("ArgyleService unknown environment: #{env}") } | ||
|
||
client_options = { | ||
request: { | ||
open_timeout: 5, | ||
timeout: 5, | ||
params_encoder: Faraday::FlatParamsEncoder | ||
}, | ||
url: @environment[:base_url] | ||
} | ||
@http = Faraday.new(client_options) do |conn| | ||
conn.set_basic_auth @api_key_id, @api_key_secret | ||
conn.response :raise_error | ||
conn.response :json, content_type: "application/json" | ||
conn.response :logger, | ||
Rails.logger, | ||
headers: true, | ||
bodies: true, | ||
log_level: :debug | ||
end | ||
end | ||
|
||
# Fetch all Argyle items | ||
def items(query = nil) | ||
@http.get("items", { q: query }).body | ||
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,35 @@ | ||
class ProviderSearchService | ||
PROVIDER_RESULT = Struct.new(:provider_name, :provider_options, :name, :logo_url, keyword_init: true) | ||
|
||
def initialize(client_agency_id) | ||
set_pinwheel(client_agency_id) | ||
set_argyle(client_agency_id) | ||
end | ||
|
||
def search(query = "") | ||
[ | ||
*@argyle.items(query)["results"].map do |result| | ||
PROVIDER_RESULT.new(provider_name: :argyle, provider_options: { response_type: result["kind"], provider_id: result["id"] }, name: result["name"], logo_url: result["logo_url"]) | ||
end, | ||
*@pinwheel.fetch_items(q: query)["data"].map do |result| | ||
PROVIDER_RESULT.new(provider_name: :pinwheel, provider_options: { response_type: result["response_type"], provider_id: result["id"] }, name: result["name"], logo_url: result["logo_url"]) | ||
end | ||
] | ||
end | ||
|
||
private | ||
|
||
def site_config | ||
Rails.application.config.sites | ||
end | ||
|
||
def set_pinwheel(client_agency_id) | ||
environment = site_config[client_agency_id].pinwheel_environment | ||
@pinwheel ||= PinwheelService.new(environment) | ||
end | ||
|
||
def set_argyle(client_agency_id) | ||
environment = site_config[client_agency_id].argyle_environment | ||
@argyle ||= ArgyleService.new(environment) | ||
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