Skip to content

Commit

Permalink
[Misc] Metaprogramming attempt
Browse files Browse the repository at this point in the history
### Why?

Currently, we have to keep creating new classes for each endpoint that spacex api launches

These new classes are ***very** similar to each other

This is an attempt to avoid writing the main classes.

While this undeniably comes with the downsides of metaprogramming, I am wondering if we can alleviate some of that pain by continuing to write the individual specs and updating the readme file.
  • Loading branch information
invacuo committed Oct 17, 2021
1 parent ea16c89 commit cb7dabc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
13 changes: 9 additions & 4 deletions lib/spacex/models.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module SPACEX
module V4
autoload :Capsules, 'spacex/v4/capsules'
autoload :Company, 'spacex/v4/company'
autoload :Cores, 'spacex/v4/cores'
autoload :Rockets, 'spacex/v4/rockets'
CLASS_NAMES = %w[Capsules Company Cores Rockets].freeze
CLASS_NAMES.each do |class_name|
SPACEX::V4.const_set(class_name, Class.new do |class_name|
def self.info(id = nil, _query = {})
api_name = self.to_s.gsub("SPACEX::V4::", "").downcase
SPACEX::BaseRequest.info("#{api_name}/#{id}", nil, 'v4')
end
end)
end
end
end
4 changes: 4 additions & 0 deletions lib/spacex/v4/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SPACEX
module V4
end
end
21 changes: 0 additions & 21 deletions lib/spacex/v4/capsules.rb

This file was deleted.

10 changes: 5 additions & 5 deletions spec/fixtures/spacex/v4/company.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion spec/spacex/v4/cores_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'last_update',
'launches',
'serial',
'id'
'id',
'status'
)
end
end
Expand Down

0 comments on commit cb7dabc

Please sign in to comment.