-
Notifications
You must be signed in to change notification settings - Fork 0
Rename Gem for BatchlineSolutions #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imRohan
wants to merge
4
commits into
main
Choose a base branch
from
rl-rename-gem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "base64" | ||
| require "json" | ||
| require "net/http" | ||
| require "ostruct" | ||
|
|
||
| require "core/api_client" | ||
| require "core/api/v1" | ||
| require "core/api/v1/authenticator" | ||
| require "core/api_operations/retrieve" | ||
| require "core/authorization/base" | ||
| require "core/authorization/basic" | ||
| require "core/authorization/blank" | ||
| require "core/resources/api_error" | ||
| require "core/authorization/bearer" | ||
| require "core/resources/object" | ||
| require "core/resources/inventory_level" | ||
| require "core/resources/order" | ||
| require "core/resources/order_item" | ||
| require "core/serializers/order_item_serializer" | ||
| require "core/resources/menu" | ||
| require "core/resources/menu_item" | ||
| require "core/serializers/menu_item_serializer" | ||
| require "core/resources/recipient" | ||
| require "core/serializers/recipient_serializer" | ||
| require "core/version" | ||
| require "core/errors" | ||
|
|
||
| module Bls | ||
| module Core | ||
| class Error < StandardError; end | ||
|
|
||
| class << self | ||
| attr_accessor :client_id, :client_secret, :environment | ||
|
|
||
| def configure | ||
| yield self | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,70 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class V1 | ||
| attr_accessor :authentication | ||
| attr_reader :environment, :username, :password | ||
|
|
||
| PRODUCTION = "production" | ||
| STAGING = "staging" | ||
|
|
||
| def initialize(username: nil, password: nil, authentication: nil, | ||
| environment: STAGING) | ||
| @authentication = authentication | ||
| @environment = environment | ||
| @username = username | ||
| @password = password | ||
| end | ||
|
|
||
| def base_url | ||
| "https://api.sunbasket.#{tld}/partner/v1" | ||
| end | ||
|
|
||
| def authenticate! | ||
| return if authenticated? | ||
|
|
||
| authenticator = V1::Authenticator.build(self) | ||
| @authentication = authenticator.save! | ||
| end | ||
|
|
||
| def client | ||
| build_api_client! | ||
| end | ||
|
|
||
| def authenticated? | ||
| authentication.present? | ||
| end | ||
|
|
||
| def reset_authentication | ||
| @authentication = nil | ||
| self | ||
| end | ||
|
|
||
| def self.build(config: Bls::Core) | ||
| new(environment: config.environment, | ||
| username: config.client_id, | ||
| password: config.client_secret) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def build_api_client! | ||
| authenticate! | ||
| Bls::Core::ApiClient.build(self) | ||
| end | ||
|
|
||
| def tld | ||
| @tld ||= determine_tld | ||
| end | ||
|
|
||
| def determine_tld | ||
| if environment == PRODUCTION | ||
| "com" | ||
| else | ||
| "dev" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class V1::Authenticator | ||
| attr_accessor :base_url, :client_id, :client_secret | ||
|
|
||
| def initialize(base_url:, client_id:, client_secret:) | ||
| @base_url = base_url | ||
| @client_id = client_id | ||
| @client_secret = client_secret | ||
| end | ||
|
|
||
| def save! | ||
| path = "#{base_url}/token" | ||
| body = { client_id: client_id, client_secret: client_secret } | ||
| auth = Authorization::Basic. | ||
| factory(client_id: client_id, client_secret: client_secret) | ||
| client = Core::ApiClient.new(authentication: auth) | ||
| response = client.post(path: path, body: body) | ||
| handle_authentication_response!(response: response.data) | ||
| end | ||
|
|
||
| def self.build(api) | ||
| new(base_url: api.base_url, | ||
| client_id: api.username, | ||
| client_secret: api.password) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def handle_authentication_response!(response:) | ||
| if response_has_errors?(response) | ||
| handle_errors! | ||
| else | ||
| handle_successful_authentication_response(response) | ||
| end | ||
| end | ||
|
|
||
| def response_has_errors?(response) | ||
| response.has_key?(:error) | ||
| end | ||
|
|
||
| def handle_successful_authentication_response(response) | ||
| access_token = response[:access_token] | ||
| Core::Authorization::Bearer.new(token: access_token) | ||
| end | ||
|
|
||
| def handle_errors! | ||
| raise AuthenticationError.new(status: 401, title: "Authentication Failed") | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,91 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class ApiClient | ||
| attr_reader :api, :authentication | ||
|
|
||
| def initialize(api: nil, authentication: nil) | ||
| @api = api | ||
| @authentication = authentication || Authorization::Blank.new | ||
| end | ||
|
|
||
| def post(path:, body:) | ||
| uri = URI(path) | ||
| request = build_request_with_body(uri: uri, body: body) | ||
| execute_request(request: request, uri: request.uri) | ||
| end | ||
|
|
||
| def delete(path:, **) | ||
| uri = URI(path) | ||
| request = Net::HTTP::Delete.new(uri) | ||
| execute_request(request: request, uri: request.uri) | ||
| end | ||
|
|
||
| def get(path:, **) | ||
| uri = URI(path) | ||
| request = Net::HTTP::Get.new(uri) | ||
| execute_request(request: request, uri: request.uri) | ||
| end | ||
|
|
||
| def execute_request(request:, uri:) | ||
| Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | ||
| request["Authorization"] = authentication.header | ||
| response = http.request(request) | ||
| handle_response(response: response) | ||
| end | ||
| end | ||
|
|
||
| def self.build(api) | ||
| new(api: api, authentication: api.authentication) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def build_request_with_body(uri:, body:) | ||
| request = Net::HTTP::Post.new(uri) | ||
| request.body = body.to_json | ||
| request["content-type"] = "application/json" | ||
| request | ||
| end | ||
|
|
||
| def handle_response(response:) | ||
| if authentication_failed?(response.code) | ||
| handle_authentication_error! | ||
| else | ||
| body = parse_response_body(response) | ||
| OpenStruct.new(data: body, success?: request_successful?(response.code)) | ||
| end | ||
| end | ||
|
|
||
| def handle_authentication_error! | ||
| api.reset_authentication | ||
| raise AuthenticationError.new(status: 401, | ||
| title: "Authentication Failed") | ||
| end | ||
|
|
||
| def parse_response_body(response) | ||
| return {} if empty_response?(response.code) | ||
| return {} if redirection?(response.code) | ||
|
|
||
| JSON.parse(response.body, symbolize_names: true) | ||
| end | ||
|
|
||
| def empty_response?(response_code) | ||
| response_code.to_i == 204 | ||
| end | ||
|
|
||
| def redirection?(response_code) | ||
| response_code.to_i.between?(300, 399) | ||
| end | ||
|
|
||
| def request_successful?(response_code) | ||
| response_code.to_i.between?(200, 299) | ||
| end | ||
|
|
||
| def authentication_failed?(response_code) | ||
| response_code.to_i == 401 | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| module ApiOperations | ||
| module Retrieve | ||
| def retrieve(id) | ||
| resource = new(id: id) | ||
| api_client = resource.client | ||
| response = api_client.get(path: resource.resource_path) | ||
| if response.success? | ||
| build_from_response(response.data) | ||
| else | ||
| raise ResourceRetrievalError.build(response) | ||
| end | ||
| end | ||
|
|
||
| def retrieve_all | ||
| resource = new | ||
| api_client = resource.client | ||
| response = api_client.get(path: resource.resources_path) | ||
| if response.success? | ||
| response.data.map { |id| new(id: id) } | ||
| else | ||
| raise ResourceRetrievalError.build(response) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this is
BLS::Coreshould the insertion point be renamed too?