regolith 0.1.15 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +1,32 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'json'
4
- require 'ostruct'
5
-
1
+ # lib/regolith/service_client.rb
6
2
  module Regolith
7
3
  class ServiceClient
8
4
  class << self
9
- def get(service_name, path, params = nil)
10
- make_request(service_name, :get, path, nil, params)
11
- end
12
-
13
- def post(service_name, path, body = nil, params = nil)
14
- make_request(service_name, :post, path, body, params)
5
+ def get(service_name, path)
6
+ make_request(service_name, :get, path)
15
7
  end
16
8
 
17
- def put(service_name, path, body = nil, params = nil)
18
- make_request(service_name, :put, path, body, params)
9
+ def post(service_name, path, body = nil)
10
+ make_request(service_name, :post, path, body)
19
11
  end
20
12
 
21
- def delete(service_name, path, params = nil)
22
- make_request(service_name, :delete, path, nil, params)
13
+ def put(service_name, path, body = nil)
14
+ make_request(service_name, :put, path, body)
23
15
  end
24
16
 
25
- def health(service_name)
26
- get(service_name, 'health')
27
- end
28
-
29
- def parsed_body(response)
30
- JSON.parse(response.body) rescue {}
17
+ def delete(service_name, path)
18
+ make_request(service_name, :delete, path)
31
19
  end
32
20
 
33
21
  private
34
22
 
35
- def make_request(service_name, method, path, body = nil, params = nil)
36
- with_retries do
37
- service_config = Regolith.service_registry[service_name.to_s]
38
-
39
- unless service_config
40
- raise "Service '#{service_name}' not found in registry"
41
- end
42
-
43
- uri = build_uri(service_name, path, params, service_config)
44
-
45
- http = Net::HTTP.new(uri.host, uri.port)
46
- http.read_timeout = Regolith.configuration.timeout
47
- http.open_timeout = Regolith.configuration.timeout
48
-
49
- request = build_request(method, uri, body)
50
-
51
- begin
52
- http.request(request)
53
- rescue => e
54
- # Return a mock response for connection errors
55
- OpenStruct.new(
56
- code: 500,
57
- body: { error: e.message, service: service_name }.to_json
58
- )
59
- end
23
+ def make_request(service_name, method, path, body = nil)
24
+ service_config = Regolith.service_registry[service_name.to_s]
25
+
26
+ unless service_config
27
+ raise "Service '#{service_name}' not found in registry"
60
28
  end
61
- end
62
29
 
63
- def build_uri(service_name, path, params, service_config)
64
30
  # In Docker Compose, services can be reached by service name
65
31
  # In development, use localhost with the mapped port
66
32
  host = in_docker? ? service_name.to_s : 'localhost'
@@ -68,56 +34,34 @@ module Regolith
68
34
 
69
35
  uri = URI("http://#{host}:#{port}/#{path}")
70
36
 
71
- # Add query parameters if provided
72
- if params && !params.empty?
73
- query_params = params.map { |k, v| "#{URI.encode_www_form_component(k)}=#{URI.encode_www_form_component(v)}" }
74
- uri.query = query_params.join('&')
75
- end
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ http.read_timeout = Regolith.configuration.timeout
76
39
 
77
- uri
78
- end
79
-
80
- def build_request(method, uri, body)
81
40
  case method
82
41
  when :get
83
- Net::HTTP::Get.new(uri)
42
+ request = Net::HTTP::Get.new(uri)
84
43
  when :post
85
44
  request = Net::HTTP::Post.new(uri)
86
- if body
87
- request.body = body
88
- request['Content-Type'] = 'application/json'
89
- end
90
- request
45
+ request.body = body if body
46
+ request['Content-Type'] = 'application/json'
91
47
  when :put
92
48
  request = Net::HTTP::Put.new(uri)
93
- if body
94
- request.body = body
95
- request['Content-Type'] = 'application/json'
96
- end
97
- request
49
+ request.body = body if body
50
+ request['Content-Type'] = 'application/json'
98
51
  when :delete
99
- Net::HTTP::Delete.new(uri)
52
+ request = Net::HTTP::Delete.new(uri)
100
53
  end
101
- end
102
54
 
103
- def with_retries(max_retries: 2, base_delay: 0.1)
104
- attempt = 0
105
55
  begin
106
- attempt += 1
107
- yield
56
+ http.request(request)
108
57
  rescue => e
109
- if attempt <= max_retries
110
- delay = base_delay * (2 ** (attempt - 1)) # Exponential backoff
111
- sleep(delay)
112
- retry
113
- else
114
- raise e
115
- end
58
+ # Return a mock response for connection errors
59
+ OpenStruct.new(code: '500', body: { error: e.message }.to_json)
116
60
  end
117
61
  end
118
62
 
119
63
  def in_docker?
120
- File.exist?('/.dockerenv') || ENV['REGOLITH_SERVICE_NAME']
64
+ File.exist?('/.dockerenv') || ENV['REGOLITH_SERVICE_NAME'].present?
121
65
  end
122
66
  end
123
67
  end
@@ -1,4 +1,4 @@
1
1
  # lib/regolith/version.rb
2
2
  module Regolith
3
- VERSION = "0.1.15"
3
+ VERSION = "0.1.19"
4
4
  end
data/lib/regolith.rb CHANGED
@@ -4,6 +4,7 @@ require 'active_model'
4
4
  require 'net/http'
5
5
  require 'json'
6
6
  require 'yaml'
7
+
7
8
  require_relative 'regolith/version'
8
9
  require_relative 'regolith/service_client'
9
10
  require_relative 'regolith/regolith_association'
@@ -15,17 +16,17 @@ module Regolith
15
16
  def configure
16
17
  yield(configuration)
17
18
  end
18
-
19
+
19
20
  def configuration
20
21
  @configuration ||= Configuration.new
21
22
  end
22
-
23
+
23
24
  def service_registry
24
25
  @service_registry ||= load_service_registry
25
26
  end
26
-
27
+
27
28
  private
28
-
29
+
29
30
  def load_service_registry
30
31
  if defined?(Rails) && Rails.application.config.respond_to?(:regolith)
31
32
  registry_path = Rails.application.config.regolith.service_registry
@@ -39,13 +40,12 @@ module Regolith
39
40
  end
40
41
  end
41
42
  end
42
-
43
+
43
44
  class Configuration
44
- attr_accessor :service_name, :service_port, :timeout, :default_port # Add default_port here
45
-
45
+ attr_accessor :service_name, :service_port, :timeout
46
+
46
47
  def initialize
47
48
  @timeout = 30
48
- @default_port = 3001 # Add this line
49
49
  end
50
50
  end
51
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regolith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Regolith Team