Skip to content

Commit

Permalink
Creates Ioki::Support module (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
eikes committed Mar 23, 2022
1 parent 065eec6 commit d26913f
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 74 deletions.
1 change: 1 addition & 0 deletions lib/ioki.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'ioki/support'
require 'ioki/configuration'
require 'ioki/model'
require 'ioki/error'
Expand Down
21 changes: 5 additions & 16 deletions lib/ioki/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module Ioki
module Model
class Base
extend Ioki::Support::ModuleMixins

class << self
def class_instance_attribute_definitions
@class_instance_attribute_definitions ||= {}
Expand Down Expand Up @@ -58,12 +60,6 @@ def attribute_definitions
collect(&:class_instance_attribute_definitions).
reduce(&:merge)
end

def descendants
ObjectSpace.each_object(singleton_class).reject do |k|
k.singleton_class? || k == self
end
end
end

attr_accessor :_raw_attributes, :_attributes, :_etag
Expand Down Expand Up @@ -147,7 +143,7 @@ def serialize(usecase = :read)

next if definition.key?(:omit_if_blank_on) &&
Array(definition[:omit_if_blank_on]).include?(usecase) &&
empty?(value)
Ioki::Support.blank?(value)

data[attribute] = if definition[:type] == :object && value.is_a?(Ioki::Model::Base)
value.serialize(usecase)
Expand All @@ -174,16 +170,9 @@ def reset_attributes!
end

def constantize_in_module(class_name)
return nil if empty?(class_name)

module_names = self.class.name.split('::')
module_names.slice!(-1, 1)
module_names << class_name
Object.const_get(module_names.join('::'))
end
return nil if Ioki::Support.blank?(class_name)

def empty?(value)
value.respond_to?(:empty?) ? !!value.empty? : !value
self.class.module_parent.const_get class_name
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/ioki/model/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
module Ioki
module Model
module Driver
def self.valid_models
constants.
select { |constant| const_get(constant).is_a?(Class) }.
reject { |constant| const_get(constant).unvalidated? }.
map { |model| const_get(model) } - [Base]
end

class Base < Ioki::Model::Base
def self.specification_scope
'driver_api'
Expand Down
7 changes: 0 additions & 7 deletions lib/ioki/model/operator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
module Ioki
module Model
module Operator
def self.valid_models
constants.
select { |constant| const_get(constant).is_a?(Class) }.
reject { |constant| const_get(constant).unvalidated? }.
map { |model| const_get(model) } - [Base]
end

class Base < Ioki::Model::Base
attribute :id, on: :read, type: :string
attribute :type, on: :read, type: :string
Expand Down
7 changes: 0 additions & 7 deletions lib/ioki/model/passenger/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
module Ioki
module Model
module Passenger
def self.valid_models
constants.
select { |constant| const_get(constant).is_a?(Class) }.
reject { |constant| const_get(constant).unvalidated? }.
map { |model| const_get(model) } - [Base]
end

class Base < Ioki::Model::Base
def self.specification_scope
'passenger_api'
Expand Down
7 changes: 0 additions & 7 deletions lib/ioki/model/platform/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
module Ioki
module Model
module Platform
def self.valid_models
constants.
select { |constant| const_get(constant).is_a?(Class) }.
reject { |constant| const_get(constant).unvalidated? }.
map { |model| const_get(model) } - [Base]
end

class Base < Ioki::Model::Base
def self.specification_scope
'platform_api--v20210101'
Expand Down
50 changes: 50 additions & 0 deletions lib/ioki/support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Ioki
# These are methods which are adapted from ActiveSupport
module Support
# activesupport/lib/active_support/core_ext/string/inflections.rb
# But it doesn't support uppercase_first_letter argument and inflections
def camelize(term)
string = term.to_s.sub(/^[a-z\d]*/) { |match| match.capitalize! || match }
string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
word = Regexp.last_match(2)
substituted = word.capitalize! || word
Regexp.last_match(1) ? "::#{substituted}" : substituted
end
string
end

# activesupport/lib/active_support/core_ext/object/blank.rb
def blank?(value)
value.respond_to?(:empty?) ? !!value.empty? : !value
end

module_function :camelize, :blank?

module ModuleMixins
# activesupport/lib/active_support/core_ext/module/introspection.rb
def module_parent_name
if defined?(@parent_name)
@parent_name
else
parent_name = name =~ /::[^:]+\z/ ? -Regexp.last_match.pre_match : nil
@parent_name = parent_name unless frozen?
parent_name
end
end

# activesupport/lib/active_support/core_ext/module/introspection.rb
def module_parent
module_parent_name ? Object.const_get(module_parent_name) : Object
end

# activesupport/lib/active_support/core_ext/class/subclasses.rb
def descendants
ObjectSpace.each_object(singleton_class).reject do |k|
k.singleton_class? || k == self
end
end
end
end
end
14 changes: 1 addition & 13 deletions lib/ioki/webhooks/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,13 @@ def model_class
end

def model_class_name
camelize data['type']
Ioki::Support.camelize data['type']
end

def action
event_type&.split('.')&.last
end

private

def camelize(term)
string = term.to_s.sub(/^[a-z\d]*/) { |match| match.capitalize! || match }
string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
word = Regexp.last_match(2)
substituted = word.capitalize! || word
Regexp.last_match(1) ? "::#{substituted}" : substituted
end
string
end

end
end
end
10 changes: 3 additions & 7 deletions lib/ioki/webhooks/signature_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def initialize(body:, signature:)
end

def call
raise Ioki::Error::WebhookSignatureMissing if blank? @signature
raise Ioki::Error::WebhookBodyMissing if blank? @body
raise Ioki::Error::WebhookSignatureKeyMissing if blank? ENV['WEBHOOK_SIGNATURE_KEY']
raise Ioki::Error::WebhookSignatureMissing if Ioki::Support.blank? @signature
raise Ioki::Error::WebhookBodyMissing if Ioki::Support.blank? @body
raise Ioki::Error::WebhookSignatureKeyMissing if Ioki::Support.blank? ENV['WEBHOOK_SIGNATURE_KEY']
raise Ioki::Error::WebhookSignatureInvalid unless OpenSSL.secure_compare @signature, calculated_signature
end

Expand All @@ -39,10 +39,6 @@ def calculated_signature
@body # Second: The data to verify
)
end

def blank?(val)
val.nil? || val == ''
end
end
end
end
12 changes: 9 additions & 3 deletions spec/ioki/model/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
model = example_class.new({})
expect(model.foo).to be_nil
model.foo = { bar: 42 }
expect(model.foo).to be_kind_of(described_class)
expect(model.foo).to be_kind_of(example_referenced_class)
expect(model.foo.bar).to eq(42)
end
end
Expand Down Expand Up @@ -181,9 +181,9 @@
expect(model.foo).to be_nil
model.foo = [{ bar: '42' }, { bar: '43' }]
expect(model.foo).to be_kind_of(Array)
expect(model.foo.first).to be_kind_of(described_class)
expect(model.foo.first).to be_kind_of(example_referenced_class)
expect(model.foo.first.bar).to eq(42)
expect(model.foo.last).to be_kind_of(described_class)
expect(model.foo.last).to be_kind_of(example_referenced_class)
expect(model.foo.last.bar).to eq(43)
end
end
Expand Down Expand Up @@ -639,4 +639,10 @@
end
end
end

describe '#constantize_in_module' do
it 'returns a module of the given name in the same parent module' do
expect(Ioki::Model::Platform::Pause.new.send(:constantize_in_module, 'Place')).to eq Ioki::Model::Platform::Place
end
end
end
6 changes: 3 additions & 3 deletions spec/ioki/model/openapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe 'OpenApi schema definitions' do
if API_SPECIFICATIONS[:platform_api]
describe 'the platform_api definition' do
Ioki::Model::Platform.valid_models.each do |model|
Ioki::Model::Platform::Base.descendants.reject(&:unvalidated?).each do |model|
it "ensures #{model} matches the published API specifications" do
expect(model).to match_open_api_definition('platform_api', model)
end
Expand All @@ -15,7 +15,7 @@

if API_SPECIFICATIONS[:passenger_api]
describe 'the passenger_api definition' do
Ioki::Model::Passenger.valid_models.each do |model|
Ioki::Model::Passenger::Base.descendants.reject(&:unvalidated?).each do |model|
it "ensures #{model} matches the published API specifications" do
expect(model).to match_open_api_definition('passenger_api', model)
end
Expand All @@ -25,7 +25,7 @@

if API_SPECIFICATIONS[:driver_api]
describe 'the driver_api definition' do
Ioki::Model::Driver.valid_models.each do |model|
Ioki::Model::Driver::Base.descendants.reject(&:unvalidated?).each do |model|
it "ensures #{model} matches the published API specifications" do
expect(model).to match_open_api_definition('driver_api', model)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/ioki/support_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ioki::Support do
describe 'camelize' do
it 'works for lowercase strings' do
expect(described_class.camelize('task')).to eq('Task')
end

it 'works for underscored strings' do
expect(described_class.camelize('task_list')).to eq('TaskList')
end

it 'works for multiple underscored strings' do
expect(described_class.camelize('driver_vehicle_connection')).to eq('DriverVehicleConnection')
end

it 'works for empty strings' do
expect(described_class.camelize('')).to eq('')
end

it 'works for nil' do
expect(described_class.camelize(nil)).to eq('')
end
end
end
4 changes: 0 additions & 4 deletions spec/ioki/webhooks/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
expect(event.action).to eq 'created'
end

it 'can camelize strings' do
expect(event.send(:camelize, 'task_list')).to eq('TaskList')
end

it 'has the correct id' do
expect(event.id).to eq params['id']
end
Expand Down

0 comments on commit d26913f

Please sign in to comment.