Class: Hyraft::Rule::Base::InApiCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/rule/base/in_api_command.rb

Class Method Summary collapse

Class Method Details

.show_usageObject



195
196
197
198
199
200
201
202
203
204
# File 'lib/hyraft/rule/base/in_api_command.rb', line 195

def self.show_usage
  puts "Usage: hyraft-rule in-api <resource_name> [attributes...] [target_dir]"
  puts ""
  puts "Examples:"
  puts "  hyraft-rule in-api articles title content"
  puts "  hyraft-rule in-api admins username password email"
  puts "  hyraft-rule in-api comments body author_id"
  puts ""
  puts "This creates: base-adapters/in-api/<resource_name>_request.rb"
end

.start(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/hyraft/rule/base/in_api_command.rb', line 12

def self.start(args)
  resource_name = args[0]
  return show_usage unless resource_name

  plural_name = resource_name.downcase
  attributes = []

  if args.length > 1
    last_arg = args[-1]
    if last_arg.include?('/') || last_arg == '.' || last_arg == '..' || last_arg.start_with?('./')
      target_dir = last_arg
      attributes = args[1..-2] if args.length > 2
    else
      target_dir = "."
      attributes = args[1..-1]
    end
  else
    target_dir = "."
  end

  singular_name = plural_name.chomp('s')
  class_name = resource_name.split('_').map(&:capitalize).join
  resource_class = singular_name.capitalize

  filename = "#{plural_name}_request.rb"
  request_dir = File.join(target_dir, "base-adapters", "in-api")
  full_path = File.join(request_dir, filename)

  FileUtils.mkdir_p(request_dir)
  
  # Ruby 4.0 Box syntax for template
  template = %{
# base-adapters/in-api/#{plural_name}_request.rb

pathway 'connectors/cable/#{plural_name}_cable'

require 'json'

class #{class_name}Request
  include HtmlSafety

  def create(env)
    input = env['BODY']
    
    if input.nil? || input.empty?
      return json_response({ error: 'Empty body' }, 400)
    end

    begin
      data = JSON.parse(input, symbolize_names: true)
      #{attributes.map { |a| "#{a} = data[:#{a}].to_s" }.join("\n      ")}
      
      if #{attributes.map { |a| "#{a}.empty?" }.join(" || ")}
return json_response({ error: '#{attributes.map(&:capitalize).join(' and ')} required' }, 422)
      end
      
      #{attributes.map { |a| "safe_#{a} = safety(#{a})" }.join("\n      ")}
      
      result = #{class_name}Cable.create_#{singular_name}(#{attributes.map { |a| "safe_#{a}" }.join(", ")})
      
      safe_result = {
id: result[:id],
#{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n        ")}
created_at: result[:created_at],
updated_at: result[:updated_at]
      }
      
      json_response({ 
data: safe_result, 
message: '#{resource_class} created successfully!' 
      }, 201)
      
    rescue JSON::ParserError
      json_response({ error: 'Invalid JSON' }, 400)
    rescue StandardError => e
      json_response({ error: safety(e.message) }, 422)
    end
  end

  def index(env)
    items = #{class_name}Cable.list_#{plural_name}
    all_items = items.is_a?(Array) ? items.reverse : []
    
    safe_items = all_items.map do |item|
      {
id: item[:id],
#{attributes.map { |a| "#{a}: safety(item[:#{a}].to_s)," }.join("\n        ")}
created_at: item[:created_at],
updated_at: item[:updated_at]
      }
    end
    
    json_response({ data: safe_items }, 200)
  end

  def show(env, id)
    begin
      result = #{class_name}Cable.get_#{singular_name}(id)
      
      if result
safe_result = {
  id: result[:id],
  #{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n          ")}
  created_at: result[:created_at],
  updated_at: result[:updated_at]
}
json_response({ data: safe_result }, 200)
      else
json_response({ error: '#{resource_class} not found' }, 404)
      end
    rescue StandardError => e
      json_response({ error: safety(e.message) }, 404)
    end
  end

  def update(env, id)
    input = env['BODY']
    
    if input.nil? || input.empty?
      return json_response({ error: 'Empty body' }, 400)
    end

    begin
      data = JSON.parse(input, symbolize_names: true)
      #{attributes.map { |a| "#{a} = data[:#{a}].to_s" }.join("\n      ")}
      
      if #{attributes.map { |a| "#{a}.empty?" }.join(" || ")}
return json_response({ error: '#{attributes.map(&:capitalize).join(' and ')} required' }, 422)
      end
      
      #{attributes.map { |a| "safe_#{a} = safety(#{a})" }.join("\n      ")}
      
      result = #{class_name}Cable.update_#{singular_name}(id, #{attributes.map { |a| "safe_#{a}" }.join(", ")})
      
      safe_result = {
id: result[:id],
#{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n        ")}
created_at: result[:created_at],
updated_at: result[:updated_at]
      }
      
      json_response({ data: safe_result, message: '#{resource_class} updated successfully!' }, 200)
      
    rescue JSON::ParserError
      json_response({ error: 'Invalid JSON' }, 400)
    rescue StandardError => e
      if e.message == "Not found"
json_response({ error: safety(e.message) }, 404)
      else
json_response({ error: safety(e.message) }, 422)
      end
    end
  end

  def delete(env, id)
    begin
      #{class_name}Cable.delete_#{singular_name}(id)
      json_response({ data: { deleted: true, id: id, message: '#{resource_class} deleted successfully!' } }, 200)
    rescue StandardError => e
      if e.message == "Not found"
json_response({ error: safety(e.message) }, 404)
      else
json_response({ error: safety(e.message) }, 422)
      end
    end
  end

  private

  def json_response(data, status)
    body = data.to_json
    [status, { 'Content-Type' => 'application/json' }, [body]]
  end
end
}.strip

  File.write(full_path, template)

  puts "\e[94m✓ Created API request: #{full_path}\e[0m"
  puts "\e[38;5;214mResource: #{plural_name}"
  puts "Attributes: #{attributes.join(', ')}\e[0m" if attributes.any?
end