Class: DSLInterpreter
- Inherits:
-
Object
- Object
- DSLInterpreter
- Defined in:
- lib/dsl_interpreter.rb
Overview
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
To access data after interpreting.
Instance Method Summary collapse
-
#initialize ⇒ DSLInterpreter
constructor
A new instance of DSLInterpreter.
-
#method_missing(method_name, *args, &block) ⇒ Object
Capturing top-level DSL methods.
-
#process(base_path, input_file, output_file) ⇒ Object
Reading file and evaluating as Ruby.
-
#process_args(args, block) ⇒ Object
A method to handle parameters and nested blocks.
-
#send_to_endpoint ⇒ Object
Method to send data to an endpoint.
-
#to_hash ⇒ Object
Convert to hash or JSON as required.
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize ⇒ DSLInterpreter
Returns a new instance of DSLInterpreter.
9 10 11 |
# File 'lib/dsl_interpreter.rb', line 9 def initialize @data = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Capturing top-level DSL methods
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dsl_interpreter.rb', line 14 def method_missing(method_name, *args, &block) key = method_name value = process_args(args, block) # Append key-value to the current context of @data if @data[key] @data[key] = [@data[key]] unless @data[key].is_a?(Array) @data[key] << value else @data[key] = value end end |
Instance Attribute Details
#data ⇒ Object (readonly)
To access data after interpreting
62 63 64 |
# File 'lib/dsl_interpreter.rb', line 62 def data @data end |
Instance Method Details
#process(base_path, input_file, output_file) ⇒ Object
Reading file and evaluating as Ruby
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dsl_interpreter.rb', line 65 def process(base_path, input_file, output_file) file_path = File.join(base_path, input_file) content = File.read(file_path) # begin instance_eval(content) # rescue SyntaxError => e # puts "Syntax error in DSL file: #{input_file}" # puts "Error message: #{e.message}" # puts "Error occurred at line: #{e.backtrace.first}" # return false # Indicate that processing failed # rescue StandardError => e # puts "Error processing DSL file: #{input_file}" # puts "Error message: #{e.message}" # puts "Error occurred at: #{e.backtrace.first}" # return false # Indicate that processing failed # end output_path = File.join(base_path, output_file) File.write(output_path, JSON.pretty_generate(to_hash)) true # Indicate that processing succeeded end |
#process_args(args, block) ⇒ Object
A method to handle parameters and nested blocks
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dsl_interpreter.rb', line 28 def process_args(args, block) data = {} # Handling positional and named parameters separately positional_args = [] named_args = {} args.each do |arg| if arg.is_a?(Hash) named_args.merge!(arg) else positional_args << arg end end # Assign positional parameters generically positional_args.each_with_index do |arg, index| data[:"param#{index + 1}"] = arg end # Merge named parameters directly data.merge!(named_args) # Handling a nested block if block interpreter = DSLInterpreter.new interpreter.instance_eval(&block) data.merge!(interpreter.data) end data.empty? ? nil : data end |
#send_to_endpoint ⇒ Object
Method to send data to an endpoint
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dsl_interpreter.rb', line 98 def send_to_endpoint root_key = @data.keys.first action_type = root_key.to_s uri = URI.parse("http://localhost:4567/dsl/#{action_type}") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' }) payload = { action_type: action_type, data: @data } request.body = payload.to_json response = http.request(request) puts "Response: #{response.code} - #{response.}" puts "Endpoint: #{uri}" end |
#to_hash ⇒ Object
Convert to hash or JSON as required
89 90 91 |
# File 'lib/dsl_interpreter.rb', line 89 def to_hash @data end |
#to_json(*_args) ⇒ Object
93 94 95 |
# File 'lib/dsl_interpreter.rb', line 93 def to_json(*_args) @data.to_json end |