Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/bls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@

require_relative "core/api_client"
require_relative "core/api/v1"
require_relative "core/api/v2"
require_relative "core/api/v1/authenticator"
require_relative "core/api/v2/authenticator"
require_relative "core/api_operations/retrieve"
require_relative "core/authorization/base"
require_relative "core/authorization/basic"
require_relative "core/authorization/blank"
require_relative "core/resources/api_error"
require_relative "core/authorization/bearer"
require_relative "core/resources/object"
require_relative "core/resources/objectv2"
require_relative "core/resources/allergen"
require_relative "core/resources/dietary_tag"
require_relative "core/resources/ingredient"
require_relative "core/resources/inventory_level"
require_relative "core/resources/nutrition_fact"
require_relative "core/resources/order"
require_relative "core/resources/order_item"
require_relative "core/serializers/order_item_serializer"
require_relative "core/resources/menu"
require_relative "core/resources/menu_item"
require_relative "core/serializers/menu_item_serializer"
require_relative "core/resources/product"
require_relative "core/resources/recipient"
require_relative "core/serializers/recipient_serializer"
require_relative "core/version"
Expand All @@ -32,7 +40,8 @@ module Core
class Error < StandardError; end

class << self
attr_accessor :client_id, :client_secret, :environment
attr_accessor :client_id, :client_secret, :environment, :username,
:password

def configure
yield self
Expand Down
2 changes: 1 addition & 1 deletion lib/bls/core/api/v1/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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)
factory(username: client_id, password: client_secret)
client = Core::ApiClient.new(authentication: auth)
response = client.post(path: path, body: body)
handle_authentication_response!(response: response.data)
Expand Down
70 changes: 70 additions & 0 deletions lib/bls/core/api/v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

module Bls
module Core
class V2
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://core.batchlinesolutions.#{tld}"
end

def authenticate!
return if authenticated?

authenticator = V2::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.username,
password: config.password)
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
24 changes: 24 additions & 0 deletions lib/bls/core/api/v2/authenticator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Bls
module Core
class V2::Authenticator
attr_accessor :authentication

def initialize(authentication:)
@authentication = authentication
end

def save!
authentication
end

def self.build(api)
authentication = Authorization::Basic.
factory(username: api.username,
password: api.password)
new(authentication: authentication)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/bls/core/authorization/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def header
"Basic #{token}"
end

def self.factory(client_id:, client_secret:)
encoded_token = Base64.strict_encode64("#{client_id}:#{client_secret}")
def self.factory(username:, password:)
encoded_token = Base64.strict_encode64("#{username}:#{password}")
new(token: encoded_token)
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/bls/core/resources/allergen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Bls
module Core
class Allergen < Bls::Core::ObjectV2; end
end
end
4 changes: 3 additions & 1 deletion lib/bls/core/resources/api_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def message

def self.build(response)
data = response.data
data => { status:, title:, details: }
status = data.fetch(:status, data[:type])
title = data.fetch(:title, data[:message])
details = data[:details]
new(status: status, title: title, details: details)
end

Expand Down
7 changes: 7 additions & 0 deletions lib/bls/core/resources/dietary_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Bls
module Core
class DietaryTag < Bls::Core::ObjectV2; end
end
end
7 changes: 7 additions & 0 deletions lib/bls/core/resources/ingredient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Bls
module Core
class Ingredient < Bls::Core::ObjectV2; end
end
end
7 changes: 7 additions & 0 deletions lib/bls/core/resources/nutrition_fact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Bls
module Core
class NutritionFact < Bls::Core::ObjectV2; end
end
end
23 changes: 23 additions & 0 deletions lib/bls/core/resources/objectv2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Bls
module Core
class ObjectV2 < Bls::Core::Object
def self.build_from_array(array)
array.map { |object| build(object) }
end

def resources_path
"#{api.base_url}/api/#{object_name.downcase}"
end

def resource_path
"#{api.base_url}/api/#{object_name.downcase}/#{id}"
end

def api
@api ||= Bls::Core::V2.build
end
end
end
end
30 changes: 30 additions & 0 deletions lib/bls/core/resources/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Bls
module Core
class Product < Bls::Core::ObjectV2
def initialize(args = {})
super
end

def object_name
"products"
end

def self.build_from_response(data)
product = build(data)
product.ingredients = Ingredient.build_from_array(data[:ingredients])
as_packaged = NutritionFact.
build_from_array(data[:nutrition_facts][:as_packaged])
as_cooked = NutritionFact.
build_from_array(data[:nutrition_facts].
fetch(:as_cooked, []))
product.allergens = Allergen.build_from_array(data[:allergens])
product.dietary_tags = DietaryTag.build_from_array(data[:dietary_tags])
product.nutrition_facts = { as_packaged: as_packaged,
as_cooked: as_cooked }
product
end
end
end
end
15 changes: 15 additions & 0 deletions lib/bls/core/testing/fake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module Testing
class Fake
def self.configure(*); end

class Allergen
def self.new(code: "MILK", source: "ANCHOVY")
OpenStruct.new(code: code, source: source)
end
end

class DistributionCenterInventory
def self.create(name: "WEST_COAST", quantity: 100)
OpenStruct.new(distribution_center: name, quantity: quantity)
Expand Down Expand Up @@ -90,6 +96,15 @@ def self.new(sku:, quantity:, protein_sku:)
end
end

class Product
def self.new(code:, name:, status: "DRAFT", nutrition_facts: [])
OpenStruct.new(code: code, name: name, status: status,
nutrition_facts: nutrition_facts,
allergens: [Fake::Allergen.new],
dietary_tags: ["NUTS"])
end
end

def self.client
Fake::Client.new
end
Expand Down
33 changes: 33 additions & 0 deletions spec/core/api/v2/authenticator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

RSpec.describe Bls::Core::V2::Authenticator do
describe "#save!" do
it "returns a authorization scheme" do
basic = Bls::Core::Authorization::Basic.new(token: "test")
auth = Bls::Core::V2::Authenticator.new(authentication: basic)

result = auth.save!

expect(result).to respond_to(:header)
end
end

describe ".build" do
it "sets the authentication" do
api = Bls::Core::V2.new
basic = Bls::Core::Authorization::Basic.new(token: "test")
allow(Bls::Core::Authorization::Basic).to receive(:factory).
and_return(basic)

result = Bls::Core::V2::Authenticator.build(api)

expect(result.authentication).to eq(basic)
end
end

def build_config(environment: "staging", client_id: "cid_123",
client_secret: "cs_123")
OpenStruct.new(environment: environment, client_id: client_id,
client_secret: client_secret)
end
end
Loading