ja 0.1.1 → 0.2.0

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: bcd4bf55cd3c814844254a793f143790c3e8ea942be0f2a1cef41bc746448890
4
- data.tar.gz: bbc1a6801f7d8eb280859f244a1b0b72f76460745e23d4201434aab13a6be499
3
+ metadata.gz: 79797ea17317f08a380c22f55d19e1785ae3f9721951927d9d508b1ab280cac7
4
+ data.tar.gz: c776feb143950eb918018b80bfc04741c55f2efd11dffbb195e4429991a2f637
5
5
  SHA512:
6
- metadata.gz: 44a3b3358fd8724ca95b84006690cb6965a6f5b0a7d83ca4231fa773c80035487d4dc3c3a852fc1aca310836e2b32a28bea34eda0df65023db801f3ad3a10f88
7
- data.tar.gz: d977da21532c1c40079d92615a13185abc09b17e43f3b1ea66620e40872445d6c8ee6ef8ca7383deee93483439893d0911ec08627b164bb7a645ccf92e5ad248
6
+ metadata.gz: 89b61d3719c61eb835213a6bfb99e21895e43b079efd24cf49a0df00b18c16ea3e2690c124c5422d52acde7e0b9502dd5b855cd1e2c60053de954ca920c18dbf
7
+ data.tar.gz: 77c1184f4b5ace74384d365fc142e90f17dc309efd705ac25fbb58c4255dd5fe99a527734510350b7e7bb19d980d40b363860e80b9c691b3ffea9da9f3535451
data/README.md CHANGED
@@ -28,9 +28,18 @@ respoonse = my_service.get("widgets")
28
28
  Or by setting your own HTTP options:
29
29
 
30
30
  ``` ruby
31
- client = HTTP.basic_auth(user: "alice", pass: "secret")
32
- my_authenticated_service = Ja.api(client: client)
33
- my_authenticated_service.get("my-private-widgets")
31
+ client = HTTP.headers("Content-Type" => "application/json")
32
+ my_service = Ja.api(client: client)
33
+ my_service.get("widgets")
34
+ ```
35
+
36
+ ### Authentication
37
+
38
+ Ja will automatically recognize basic authentication in the URL, so you don't have to call `HTTP.basic_auth` manually.
39
+
40
+ ``` ruby
41
+ my_authenticated_service = Ja.api(url: "https://username:secret@my-service.com")
42
+ my_authenticated_service.get("settings")
34
43
  ```
35
44
 
36
45
  ### Raising errors
@@ -69,6 +78,8 @@ Ja.logger = Logger.new("log/http.log")
69
78
 
70
79
  To log the full request and full response, we need to do some monkey patching, so it is disabled by default. To enable it, call `Ja.enable_debug_logging!`. You may want to do this only for development/test but not on production, because it might mess with streaming responses. Full request logging will always log in `debug` log level and will always use the globally configered logger.
71
80
 
81
+ If you have a logger that can accept hashes for some rich logging, you can enable that style by setting `Ja.enable_semantic_logging = true`
82
+
72
83
  ### Request ID
73
84
 
74
85
  One very helpful way to manage multiple services is to pass along a "request id". If you tag your logs with that value, you can use a centralized logging service to track a request as it propagates through your fleet of microservices.
@@ -115,6 +126,13 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
115
126
 
116
127
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
117
128
 
129
+ ### Todo
130
+
131
+ Some things that are on my mind of adding:
132
+
133
+ * Add ja-style functionality via [generic features](https://github.com/httprb/http/pull/482)
134
+ * Use native [logging and instrumation](https://github.com/httprb/http/pull/499)
135
+
118
136
  ## Contributing
119
137
 
120
138
  Bug reports and pull requests are welcome on GitHub at https://github.com/iain/ja.
data/lib/ja.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "http"
2
4
  require "logger"
3
5
 
@@ -18,6 +20,14 @@ module Ja
18
20
  @logger = logger
19
21
  end
20
22
 
23
+ def self.enable_semantic_logging=(bool)
24
+ @enable_semantic_logging = bool
25
+ end
26
+
27
+ def self.enable_semantic_logging?
28
+ !!@enable_semantic_logging
29
+ end
30
+
21
31
  def self.api(*args, &block)
22
32
  API.new(*args, &block)
23
33
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ja
2
4
  class API
3
5
 
4
- LOG_LINE = "%{verb} %{url} responded with %{status} %{reason}"
5
-
6
6
  include Methods
7
7
 
8
+ LOG_LINE = "%{verb} %{url} responded with %{status} %{reason}"
9
+
8
10
  def initialize(client: HTTP,
9
11
  url: nil,
10
12
  logger: Ja.logger,
@@ -14,6 +16,16 @@ module Ja
14
16
  @logger = logger
15
17
  @log_line = log_line
16
18
  @url = url
19
+ if url
20
+ uri = URI.parse(url)
21
+ if uri.user || uri.password
22
+ @client = @client.basic_auth(uri.user, uri.password)
23
+ uri.user = nil
24
+ uri.password = nil
25
+ @url = uri.to_s
26
+ end
27
+ end
28
+ @semantic_logging = Ja.enable_semantic_logging? || (defined?(SemanticLogger) && logger.is_a?(SemanticLogger::Logger))
17
29
  end
18
30
 
19
31
  attr_reader :client, :logger, :log_line, :url
@@ -67,7 +79,7 @@ module Ja
67
79
 
68
80
  message = log_line % payload
69
81
 
70
- if defined?(SemanticLogger) && logger.is_a?(SemanticLogger::Logger)
82
+ if @semantic_logging
71
83
  logger.public_send(log_level, message: message, duration: duration, payload: payload)
72
84
  else
73
85
  logger.public_send(log_level, "(%.2fms) %s" % [ duration, message ])
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ja
2
4
  module DebugLogger
3
5
 
@@ -1,6 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ja
2
4
  class Error < StandardError
3
5
 
6
+ # Base class for all errors
7
+ ResponseError = Class.new(Error)
8
+
9
+ # Base class for errors in the 4xx range
10
+ ClientError = Class.new(ResponseError)
11
+
12
+ # Base class for errors in the 5xx range
13
+ ServerError = Class.new(ResponseError)
14
+
4
15
  def self.to_exception(verb, uri, response)
5
16
  Error.fetch_error_class(response.status).new(verb, uri, response)
6
17
  end
@@ -35,15 +46,6 @@ module Ja
35
46
  response.status
36
47
  end
37
48
 
38
- # Base class for all errors
39
- ResponseError = Class.new(Error)
40
-
41
- # Base class for errors in the 4xx range
42
- ClientError = Class.new(ResponseError)
43
-
44
- # Base class for errors in the 5xx range
45
- ServerError = Class.new(ResponseError)
46
-
47
49
  HTTP::Response::Status::REASONS.each do |status, name|
48
50
  parent = status >= 500 ? ServerError : ClientError
49
51
  const_set(name.gsub(/\W/, ""), Class.new(parent))
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ja
2
4
  module Methods
3
5
 
@@ -1,3 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ja
2
- VERSION = "0.1.1"
4
+
5
+ VERSION = "0.2.0"
6
+ public_constant :VERSION
7
+
3
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iain
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-31 00:00:00.000000000 Z
11
+ date: 2019-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  - !ruby/object:Gem::Version
165
165
  version: '0'
166
166
  requirements: []
167
- rubygems_version: 3.0.2
167
+ rubygems_version: 3.0.3
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Opinionated helpers for making JSON calls with the http.rb gem