httparty_curl 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6326e21907094579b265b6058d10bbf324a232af78107014094f4f9302a664f
4
- data.tar.gz: 6ea21e2db0aadefeef5814acfcb1a70c88022c2917f00679ec97b958bf378314
3
+ metadata.gz: c4b9276e692736e8a2bdd94e26c802b5a1b41a0d617ace96f467f1d8ca4c3728
4
+ data.tar.gz: accf966bd2a76362b3f031547e80a5ebb2b46830909b73f92e249465b55fd06d
5
5
  SHA512:
6
- metadata.gz: fb447d219baa96116854968bdd9b1e7e47733c02548dd4f57a8f1444a403ec12dd559ed9ab0267e1ce722d94a1937c788d94e5c5e64ec4427191165a84c709e5
7
- data.tar.gz: 036730dc4ebf953d1aae54c7e7abfa0e1c9e899bdf184e9cd17e5893db0949ce05301b5911d9445bbb35f5e27456da93bdc683bc660e6540f34bc1a1e1c92381
6
+ metadata.gz: c04728ea33e9a1f020dff1974e8cf5ff17258f0f9cd46c01b7cc025281a09b2e55e414682b4d95f343879f26c619926288d30ebb971b69e8aa036682bb482af0
7
+ data.tar.gz: 5ceccaa74625648fa4f126e26cce69352211a422268291f0b6f611ebfbbc19f09cf18aa7ce6c7c0192d76fef7cc14dc97db54aafc6a4eed9463d75b20181dd92
data/README.md CHANGED
@@ -92,6 +92,26 @@ curl -X GET 'https://api.example.com/data' \
92
92
  -u 'username:password'
93
93
  ```
94
94
 
95
+ ## Environment-Based Logging
96
+
97
+ By default, `HTTPartyCurl` enables cURL logging in `development` and `test` environments when used within a Rails application. In other environments, logging is disabled by default.
98
+
99
+ ### Rails Applications
100
+
101
+ No additional configuration is needed. Logging is automatically enabled in `development` and `test` environments.
102
+
103
+ ### Non-Rails Applications
104
+
105
+ For non-Rails applications, you can manually set the environment or configure logging:
106
+
107
+ ```ruby
108
+ # Set the environment (e.g., :development, :test, :production)
109
+ HTTPartyCurl.configuration.environment = :development
110
+
111
+ # Or manually enable logging
112
+ HTTPartyCurl.configuration.curl_logging_enabled = true
113
+ ```
114
+
95
115
  ## Customization
96
116
 
97
117
  ### Supported HTTP Methods
@@ -2,5 +2,5 @@
2
2
 
3
3
  module HTTPartyCurl
4
4
  # Gem version number.
5
- VERSION = "0.1.0".freeze
5
+ VERSION = "0.1.1".freeze
6
6
  end
data/lib/httparty_curl.rb CHANGED
@@ -13,17 +13,39 @@ module HTTPartyCurl
13
13
 
14
14
  # Configuration class to store gem settings.
15
15
  class Configuration
16
- # @return [Boolean] whether cURL logging is enabled.
17
- attr_accessor :curl_logging_enabled
16
+ attr_accessor :curl_logging_enabled, :logger
17
+ attr_reader :environment
18
18
 
19
- # @return [Logger] the logger instance used for logging.
20
- attr_accessor :logger
21
-
22
- # Initializes the configuration with default values.
23
19
  def initialize
24
- @curl_logging_enabled = false
20
+ @environment = detect_environment
21
+ @curl_logging_enabled = default_logging_enabled?
25
22
  @logger = ::Logger.new($stdout)
26
23
  end
24
+
25
+ # Sets the environment
26
+ #
27
+ # @param env [tring|Symbol] environment
28
+ def environment=(env)
29
+ @environment = env&.to_sym
30
+ @curl_logging_enabled = default_logging_enabled?
31
+ end
32
+
33
+ private
34
+
35
+ # Detects the environment or sets it to :production by default
36
+ def detect_environment
37
+ if defined?(Rails)
38
+ Rails.env.to_sym
39
+ else
40
+ # Default to :production for non-Rails applications
41
+ :production
42
+ end
43
+ end
44
+
45
+ # Determines if logging should be enabled by default
46
+ def default_logging_enabled?
47
+ %i[development test].include?(@environment)
48
+ end
27
49
  end
28
50
 
29
51
  class << self
@@ -0,0 +1,242 @@
1
+ # test/httparty_curl_test.rb
2
+
3
+ require 'minitest/autorun'
4
+ require 'httparty_curl'
5
+ require 'stringio'
6
+
7
+ class HTTPartyCurlTest < Minitest::Test
8
+ def setup
9
+ # Reset the configuration before each test
10
+ HTTPartyCurl.configuration = nil
11
+ @original_rails = nil
12
+ end
13
+
14
+ def teardown
15
+ # Reset the configuration after each test
16
+ HTTPartyCurl.configuration = nil
17
+ restore_rails_constant
18
+ end
19
+
20
+ # Helper method to simulate a Rails environment
21
+ def simulate_rails_environment(env)
22
+ # Save the original Rails constant if it exists
23
+ @original_rails = Object.const_get(:Rails) if defined?(Rails)
24
+
25
+ # Create a mock Rails object with the specified environment
26
+ rails_mock = Minitest::Mock.new
27
+ rails_mock.expect(:env, env)
28
+
29
+ # Set the Rails constant to the mock
30
+ Object.const_set(:Rails, rails_mock)
31
+ end
32
+
33
+ # Helper method to remove the Rails constant if it exists
34
+ def remove_rails_constant
35
+ if defined?(Rails)
36
+ @original_rails = Object.const_get(:Rails)
37
+ Object.send(:remove_const, :Rails)
38
+ end
39
+ end
40
+
41
+ # Helper method to restore the original Rails constant
42
+ def restore_rails_constant
43
+ if @original_rails
44
+ Object.const_set(:Rails, @original_rails)
45
+ @original_rails = nil
46
+ else
47
+ Object.send(:remove_const, :Rails) if defined?(Rails)
48
+ end
49
+ end
50
+
51
+ def test_configuration_defaults_in_production_environment
52
+ remove_rails_constant
53
+
54
+ configuration = HTTPartyCurl::Configuration.new
55
+
56
+ assert_equal :production, configuration.environment
57
+ refute configuration.curl_logging_enabled
58
+ assert_instance_of ::Logger, configuration.logger
59
+ assert_equal $stdout, configuration.logger.instance_variable_get(:@logdev).dev
60
+ end
61
+
62
+ def test_configuration_defaults_in_development_environment
63
+ simulate_rails_environment('development')
64
+
65
+ configuration = HTTPartyCurl::Configuration.new
66
+
67
+ assert_equal :development, configuration.environment
68
+ assert configuration.curl_logging_enabled
69
+ assert_instance_of ::Logger, configuration.logger
70
+ end
71
+
72
+ def test_configuration_defaults_in_test_environment
73
+ simulate_rails_environment('test')
74
+
75
+ configuration = HTTPartyCurl::Configuration.new
76
+
77
+ assert_equal :test, configuration.environment
78
+ assert configuration.curl_logging_enabled
79
+ assert_instance_of ::Logger, configuration.logger
80
+ end
81
+
82
+ def test_configuration_defaults_in_other_rails_environment
83
+ simulate_rails_environment('staging')
84
+
85
+ configuration = HTTPartyCurl::Configuration.new
86
+
87
+ assert_equal :staging, configuration.environment
88
+ refute configuration.curl_logging_enabled
89
+ end
90
+
91
+ def test_configuration_can_set_curl_logging_enabled
92
+ remove_rails_constant
93
+
94
+ HTTPartyCurl.configure do |config|
95
+ config.curl_logging_enabled = true
96
+ end
97
+
98
+ assert HTTPartyCurl.configuration.curl_logging_enabled
99
+ end
100
+
101
+ def test_configuration_can_set_logger
102
+ custom_logger = Logger.new(StringIO.new)
103
+
104
+ HTTPartyCurl.configure do |config|
105
+ config.logger = custom_logger
106
+ end
107
+
108
+ assert_equal custom_logger, HTTPartyCurl.configuration.logger
109
+ end
110
+
111
+ def test_configuration_environment_can_be_set_in_non_rails_app
112
+ remove_rails_constant
113
+
114
+ configuration = HTTPartyCurl::Configuration.new
115
+ configuration.environment = :development
116
+
117
+ assert_equal :development, configuration.environment
118
+ assert configuration.curl_logging_enabled
119
+ end
120
+
121
+ def test_configuration_default_environment_in_non_rails_app
122
+ remove_rails_constant
123
+
124
+ configuration = HTTPartyCurl::Configuration.new
125
+
126
+ assert_equal :production, configuration.environment
127
+ refute configuration.curl_logging_enabled
128
+ end
129
+
130
+ def test_error_class_exists
131
+ assert_kind_of Class, HTTPartyCurl::Error
132
+ assert HTTPartyCurl::Error < StandardError
133
+ end
134
+
135
+ def test_configure_block_sets_configuration
136
+ custom_logger = Logger.new(StringIO.new)
137
+
138
+ HTTPartyCurl.configure do |config|
139
+ config.curl_logging_enabled = true
140
+ config.logger = custom_logger
141
+ config.environment = :test
142
+ end
143
+
144
+ assert HTTPartyCurl.configuration.curl_logging_enabled
145
+ assert_equal custom_logger, HTTPartyCurl.configuration.logger
146
+ assert_equal :test, HTTPartyCurl.configuration.environment
147
+ end
148
+
149
+ def test_configuration_respects_environment_change
150
+ remove_rails_constant
151
+
152
+ configuration = HTTPartyCurl::Configuration.new
153
+ assert_equal :production, configuration.environment
154
+ refute configuration.curl_logging_enabled
155
+
156
+ configuration.environment = :development
157
+ assert_equal :development, configuration.environment
158
+ assert configuration.curl_logging_enabled
159
+ end
160
+
161
+ def test_environment_setter_updates_environment
162
+ remove_rails_constant
163
+
164
+ configuration = HTTPartyCurl::Configuration.new
165
+ assert_equal :production, configuration.environment
166
+ refute configuration.curl_logging_enabled
167
+
168
+ configuration.environment = :development
169
+ assert_equal :development, configuration.environment
170
+ end
171
+
172
+ def test_environment_setter_updates_curl_logging_enabled
173
+ remove_rails_constant
174
+
175
+ configuration = HTTPartyCurl::Configuration.new
176
+ assert_equal :production, configuration.environment
177
+ refute configuration.curl_logging_enabled
178
+
179
+ configuration.environment = :development
180
+ assert_equal :development, configuration.environment
181
+ assert configuration.curl_logging_enabled
182
+
183
+ configuration.environment = :test
184
+ assert_equal :test, configuration.environment
185
+ assert configuration.curl_logging_enabled
186
+
187
+ configuration.environment = :production
188
+ assert_equal :production, configuration.environment
189
+ refute configuration.curl_logging_enabled
190
+
191
+ configuration.environment = :staging
192
+ assert_equal :staging, configuration.environment
193
+ refute configuration.curl_logging_enabled
194
+ end
195
+
196
+ def test_environment_setter_with_string_argument
197
+ remove_rails_constant
198
+
199
+ configuration = HTTPartyCurl::Configuration.new
200
+ configuration.environment = 'development'
201
+ assert_equal :development, configuration.environment
202
+ # Assuming default_logging_enabled? handles strings
203
+ assert configuration.curl_logging_enabled
204
+ end
205
+
206
+ def test_environment_setter_with_invalid_argument
207
+ remove_rails_constant
208
+
209
+ configuration = HTTPartyCurl::Configuration.new
210
+ configuration.environment = nil
211
+ assert_nil configuration.environment
212
+ refute configuration.curl_logging_enabled
213
+ end
214
+
215
+ def test_environment_setter_does_not_affect_logger
216
+ remove_rails_constant
217
+
218
+ configuration = HTTPartyCurl::Configuration.new
219
+ original_logger = configuration.logger
220
+
221
+ configuration.environment = :development
222
+ assert_equal original_logger, configuration.logger
223
+ end
224
+
225
+ def test_environment_setter_updates_curl_logging_enabled_correctly
226
+ remove_rails_constant
227
+
228
+ configuration = HTTPartyCurl::Configuration.new
229
+ configuration.curl_logging_enabled = false
230
+
231
+ configuration.environment = :development
232
+ assert_equal :development, configuration.environment
233
+ # Since environment changed, curl_logging_enabled should be updated
234
+ assert configuration.curl_logging_enabled
235
+
236
+ # Manually disable logging again
237
+ configuration.curl_logging_enabled = false
238
+ configuration.environment = :production
239
+ assert_equal :production, configuration.environment
240
+ refute configuration.curl_logging_enabled
241
+ end
242
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty_curl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TheScubaGeek
@@ -63,7 +63,8 @@ files:
63
63
  - lib/httparty_curl.rb
64
64
  - lib/httparty_curl/logger.rb
65
65
  - lib/httparty_curl/version.rb
66
- - test/httparty_curl/logger_test.rb
66
+ - test/lib/httparty_curl/logger_test.rb
67
+ - test/lib/httparty_curl_test.rb
67
68
  - test/test_helper.rb
68
69
  homepage: https://github.com/thescubageek/httparty_curl
69
70
  licenses:
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  requirements: []
87
- rubygems_version: 3.5.11
88
+ rubygems_version: 3.5.20
88
89
  signing_key:
89
90
  specification_version: 4
90
91
  summary: A gem to log HTTParty requests as cURL commands.