regolith 0.1.6 → 0.1.8

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,32 +1,66 @@
1
- # lib/regolith/service_client.rb
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'ostruct'
5
+
2
6
  module Regolith
3
7
  class ServiceClient
4
8
  class << self
5
- def get(service_name, path)
6
- make_request(service_name, :get, path)
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)
7
15
  end
8
16
 
9
- def post(service_name, path, body = nil)
10
- make_request(service_name, :post, path, body)
17
+ def put(service_name, path, body = nil, params = nil)
18
+ make_request(service_name, :put, path, body, params)
11
19
  end
12
20
 
13
- def put(service_name, path, body = nil)
14
- make_request(service_name, :put, path, body)
21
+ def delete(service_name, path, params = nil)
22
+ make_request(service_name, :delete, path, nil, params)
15
23
  end
16
24
 
17
- def delete(service_name, path)
18
- make_request(service_name, :delete, path)
25
+ def health(service_name)
26
+ get(service_name, 'health')
27
+ end
28
+
29
+ def parsed_body(response)
30
+ JSON.parse(response.body) rescue {}
19
31
  end
20
32
 
21
33
  private
22
34
 
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"
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
28
60
  end
61
+ end
29
62
 
63
+ def build_uri(service_name, path, params, service_config)
30
64
  # In Docker Compose, services can be reached by service name
31
65
  # In development, use localhost with the mapped port
32
66
  host = in_docker? ? service_name.to_s : 'localhost'
@@ -34,34 +68,56 @@ module Regolith
34
68
 
35
69
  uri = URI("http://#{host}:#{port}/#{path}")
36
70
 
37
- http = Net::HTTP.new(uri.host, uri.port)
38
- http.read_timeout = Regolith.configuration.timeout
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
39
76
 
77
+ uri
78
+ end
79
+
80
+ def build_request(method, uri, body)
40
81
  case method
41
82
  when :get
42
- request = Net::HTTP::Get.new(uri)
83
+ Net::HTTP::Get.new(uri)
43
84
  when :post
44
85
  request = Net::HTTP::Post.new(uri)
45
- request.body = body if body
46
- request['Content-Type'] = 'application/json'
86
+ if body
87
+ request.body = body
88
+ request['Content-Type'] = 'application/json'
89
+ end
90
+ request
47
91
  when :put
48
92
  request = Net::HTTP::Put.new(uri)
49
- request.body = body if body
50
- request['Content-Type'] = 'application/json'
93
+ if body
94
+ request.body = body
95
+ request['Content-Type'] = 'application/json'
96
+ end
97
+ request
51
98
  when :delete
52
- request = Net::HTTP::Delete.new(uri)
99
+ Net::HTTP::Delete.new(uri)
53
100
  end
101
+ end
54
102
 
103
+ def with_retries(max_retries: 2, base_delay: 0.1)
104
+ attempt = 0
55
105
  begin
56
- http.request(request)
106
+ attempt += 1
107
+ yield
57
108
  rescue => e
58
- # Return a mock response for connection errors
59
- OpenStruct.new(code: '500', body: { error: e.message }.to_json)
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
60
116
  end
61
117
  end
62
118
 
63
119
  def in_docker?
64
- File.exist?('/.dockerenv') || ENV['REGOLITH_SERVICE_NAME'].present?
120
+ File.exist?('/.dockerenv') || ENV['REGOLITH_SERVICE_NAME']
65
121
  end
66
122
  end
67
123
  end
@@ -1,4 +1,4 @@
1
1
  # lib/regolith/version.rb
2
2
  module Regolith
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.8"
4
4
  end
data/lib/regolith.rb CHANGED
@@ -4,7 +4,6 @@ require 'active_model'
4
4
  require 'net/http'
5
5
  require 'json'
6
6
  require 'yaml'
7
-
8
7
  require_relative 'regolith/version'
9
8
  require_relative 'regolith/service_client'
10
9
  require_relative 'regolith/regolith_association'
@@ -16,17 +15,17 @@ module Regolith
16
15
  def configure
17
16
  yield(configuration)
18
17
  end
19
-
18
+
20
19
  def configuration
21
20
  @configuration ||= Configuration.new
22
21
  end
23
-
22
+
24
23
  def service_registry
25
24
  @service_registry ||= load_service_registry
26
25
  end
27
-
26
+
28
27
  private
29
-
28
+
30
29
  def load_service_registry
31
30
  if defined?(Rails) && Rails.application.config.respond_to?(:regolith)
32
31
  registry_path = Rails.application.config.regolith.service_registry
@@ -40,12 +39,13 @@ module Regolith
40
39
  end
41
40
  end
42
41
  end
43
-
42
+
44
43
  class Configuration
45
- attr_accessor :service_name, :service_port, :timeout
46
-
44
+ attr_accessor :service_name, :service_port, :timeout, :default_port # Add default_port here
45
+
47
46
  def initialize
48
47
  @timeout = 30
48
+ @default_port = 3001 # Add this line
49
49
  end
50
50
  end
51
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regolith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Regolith Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-08-06 00:00:00.000000000 Z
11
+ date: 2025-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport