Skip to content

Commit

Permalink
Refactor generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Oct 2, 2024
1 parent 703d21c commit 881daf3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
11 changes: 8 additions & 3 deletions lib/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def generate
private

def initialize_directory
FileUtils.rm_rf(Config::OUTPUT_DIR)
FileUtils.mkdir_p(Config::OUTPUT_DIR)
output_dir = File.join(Config::BASE_PATH, "lib/peddler/api")
FileUtils.rm_rf(output_dir)
FileUtils.mkdir_p(output_dir)
end

def generate_apis
Expand All @@ -35,7 +36,11 @@ def run_rubocop
end

def apis
@apis ||= Config.api_model_files.map { |file| API.new(file) }.reject(&:obsolete?)
@apis ||= api_model_files.map { |file| API.new(file) }.reject(&:obsolete?)
end

def api_model_files
Dir.glob(File.join(Config::BASE_PATH, "bin/selling-partner-api-models/models/**/*.json"))
end
end
end
20 changes: 11 additions & 9 deletions lib/generator/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Generator
class API
include Utils

OBSOLETE_APIS = ["reports_2020_09_04", "feeds_2020_09_04"]

attr_reader :file

def initialize(file)
Expand All @@ -24,17 +26,17 @@ def generate
end

def operations
@operations ||= payload["paths"].flat_map do |path, methods|
@operations ||= model["paths"].flat_map do |path, methods|
Path.new(path, methods).operations
end.compact
end

def title
payload["info"]["title"]
model["info"]["title"]
end

def description
payload["info"]["description"]
model["info"]["description"]
end

def library_name
Expand All @@ -49,12 +51,8 @@ def name_with_version
[name, version].join("_")
end

def payload
@payload ||= JSON.parse(File.read(file))
end

def obsolete?
Config.obsolete?(name_with_version)
OBSOLETE_APIS.any? { |api| name_with_version.include?(api) }
end

private
Expand All @@ -76,7 +74,11 @@ def name
end

def version
payload["info"]["version"].tr("-", "_")
model["info"]["version"].tr("-", "_")
end

def model
@model ||= JSON.parse(File.read(file))
end
end
end
12 changes: 0 additions & 12 deletions lib/generator/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@
module Generator
module Config
BASE_PATH = File.expand_path("../..", __dir__)
OUTPUT_DIR = File.join(BASE_PATH, "lib/peddler/api")

HTTP_METHODS = ["delete", "get", "patch", "post", "put"].freeze
MAX_LINE_LENGTH = 120
OBSOLETE_APIS = ["reports_2020_09_04", "feeds_2020_09_04"]

class << self
def template_path(template_name)
File.join(BASE_PATH, "templates/#{template_name}.rb.erb")
end

def api_model_files
Dir.glob(File.join(BASE_PATH, "bin/selling-partner-api-models/models/**/*.json"))
end

def obsolete?(name_with_version)
OBSOLETE_APIS.any? { |api| name_with_version.include?(api) }
end
end
end
end
8 changes: 4 additions & 4 deletions lib/generator/entrypoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def generate
File.write(file_path, render_template)
end

def required_libraries
apis.map(&:library_name).append("peddler/marketplace", "peddler/token").sort
end

private

def render_template
Expand All @@ -32,9 +36,5 @@ def file_path
def template
File.read(Config.template_path("entrypoint"))
end

def generate_require_statements
apis.map { |api| "require \"#{api.library_name}\"" }.join("\n")
end
end
end
5 changes: 3 additions & 2 deletions lib/generator/path.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require "generator/config"
require "generator/utils"
require "generator/operation"

module Generator
class Path
include Utils

HTTP_METHODS = ["delete", "get", "patch", "post", "put"].freeze

attr_reader :methods

def initialize(path, methods)
Expand All @@ -16,7 +17,7 @@ def initialize(path, methods)
end

def operations
methods.select { |k, _| Config::HTTP_METHODS.include?(k) }.map do |method, operation|
methods.select { |k, _| HTTP_METHODS.include?(k) }.map do |method, operation|
Operation.new(self, method, operation)
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/peddler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
require "peddler/api/amazon_warehousing_and_distribution_2024_05_09"
require "peddler/api/aplus_content_2020_11_01"
require "peddler/api/application_management_2023_11_30"
require "peddler/api/catalog_items_v0"
require "peddler/api/catalog_items_2020_12_01"
require "peddler/api/catalog_items_2022_04_01"
require "peddler/api/catalog_items_v0"
require "peddler/api/data_kiosk_2023_11_15"
require "peddler/api/easy_ship_2022_03_23"
require "peddler/api/fba_inbound_eligibility_v1"
require "peddler/api/fba_inventory_v1"
require "peddler/api/feeds_2021_06_30"
require "peddler/api/finances_v0"
require "peddler/api/fulfillment_inbound_v0"
require "peddler/api/fulfillment_inbound_2024_03_20"
require "peddler/api/fulfillment_inbound_v0"
require "peddler/api/fulfillment_outbound_2020_07_01"
require "peddler/api/listings_items_2020_09_01"
require "peddler/api/listings_items_2021_08_01"
Expand All @@ -23,8 +23,8 @@
require "peddler/api/notifications_v1"
require "peddler/api/orders_v0"
require "peddler/api/product_fees_v0"
require "peddler/api/product_pricing_v0"
require "peddler/api/product_pricing_2022_05_01"
require "peddler/api/product_pricing_v0"
require "peddler/api/product_type_definitions_2020_09_01"
require "peddler/api/replenishment_2022_11_07"
require "peddler/api/reports_2021_06_30"
Expand All @@ -39,14 +39,14 @@
require "peddler/api/tokens_2021_03_01"
require "peddler/api/uploads_2020_11_01"
require "peddler/api/vendor_direct_fulfillment_inventory_v1"
require "peddler/api/vendor_direct_fulfillment_orders_v1"
require "peddler/api/vendor_direct_fulfillment_orders_2021_12_28"
require "peddler/api/vendor_direct_fulfillment_orders_v1"
require "peddler/api/vendor_direct_fulfillment_payments_v1"
require "peddler/api/vendor_direct_fulfillment_sandbox_test_data_2021_10_28"
require "peddler/api/vendor_direct_fulfillment_shipping_v1"
require "peddler/api/vendor_direct_fulfillment_shipping_2021_12_28"
require "peddler/api/vendor_direct_fulfillment_transactions_v1"
require "peddler/api/vendor_direct_fulfillment_shipping_v1"
require "peddler/api/vendor_direct_fulfillment_transactions_2021_12_28"
require "peddler/api/vendor_direct_fulfillment_transactions_v1"
require "peddler/api/vendor_invoices_v1"
require "peddler/api/vendor_orders_v1"
require "peddler/api/vendor_shipments_v1"
Expand Down
6 changes: 3 additions & 3 deletions templates/entrypoint.rb.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= generate_require_statements %>
require "peddler/marketplace"
require "peddler/token"
<% required_libraries.each do |required_library| -%>
require "<%= required_library %>"
<% end %>

0 comments on commit 881daf3

Please sign in to comment.