spyke 1.4.1 → 1.5.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 +4 -4
- data/README.md +18 -18
- data/lib/spyke.rb +1 -1
- data/lib/spyke/http.rb +10 -7
- data/lib/spyke/instrumentation.rb +8 -0
- data/lib/spyke/instrumentation/controller_runtime.rb +38 -0
- data/lib/spyke/instrumentation/log_subscriber.rb +26 -0
- data/lib/spyke/version.rb +1 -1
- metadata +5 -3
- data/lib/spyke/middleware/rails_logger.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d5dfe590d65f64f97fda8760b3c75ea3900d494
|
4
|
+
data.tar.gz: be9546662e362944f6ea66373c76821e23d1acb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af83b406e410df06ea8fcb0ab4eee9c811ec1a38e3ebaefefd0d83bcac4ced8436130b4f49ed370264795d21a11bdfe32f6abc0bad3443d25d4fcc061f5f68a5
|
7
|
+
data.tar.gz: 10a68a38089bf195ace08b6a9e90010e383113dba3e6c6b11e7bf49079bdbde93fb94367a3e8333400ce269b967b3a096ebab641f038b7a9d30294aeb99c9c49
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
|
15
15
|
---
|
16
16
|
|
17
|
-
Spyke basically ~~rips off~~ takes inspiration :innocent: from [Her](https://github.com/remiprev/her), a gem which we sadly had to abandon as it
|
17
|
+
Spyke basically ~~rips off~~ takes inspiration :innocent: from [Her](https://github.com/remiprev/her), a gem which we sadly had to abandon as it gave us some performance problems and maintenance seemed to had gone stale.
|
18
18
|
|
19
19
|
We therefore made Spyke which adds a few fixes/features that we needed for our projects:
|
20
20
|
|
@@ -34,7 +34,7 @@ gem 'spyke'
|
|
34
34
|
Like Her, Spyke uses Faraday to handle requests and expects it to return a hash in the following format:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
{ data: { id: 1, name: 'Bob' }, metadata: {} }
|
37
|
+
{ data: { id: 1, name: 'Bob' }, metadata: {}, errors: [] }
|
38
38
|
```
|
39
39
|
|
40
40
|
The simplest possible configuration that can work is something like this:
|
@@ -69,8 +69,19 @@ Adding a class and inheriting from `Spyke::Base` will allow you to interact with
|
|
69
69
|
```ruby
|
70
70
|
class User < Spyke::Base
|
71
71
|
has_many :posts
|
72
|
+
|
73
|
+
scope :active, -> { where(active: true) }
|
72
74
|
end
|
73
75
|
|
76
|
+
User.all
|
77
|
+
# => GET http://api.com/users
|
78
|
+
|
79
|
+
User.active
|
80
|
+
# => GET http://api.com/users?active=true
|
81
|
+
|
82
|
+
User.where(age: 3).active
|
83
|
+
# => GET http://api.com/users?active=true&age=3
|
84
|
+
|
74
85
|
user = User.find(3)
|
75
86
|
# => GET http://api.com/users/3
|
76
87
|
|
@@ -108,28 +119,17 @@ user.posts # => GET http://api.com/posts/for_user/3
|
|
108
119
|
Post.find(4) # => GET http://api.com/posts/4
|
109
120
|
```
|
110
121
|
|
111
|
-
###
|
122
|
+
### Log output
|
112
123
|
|
113
|
-
|
114
|
-
ActiveRecord-like
|
115
|
-
request/responses in `/log/faraday.log`.
|
124
|
+
When used with Rails, Spyke will automatically output helpful
|
125
|
+
ActiveRecord-like messages to the main log:
|
116
126
|
|
117
127
|
```bash
|
118
128
|
Started GET "/posts" for 127.0.0.1 at 2014-12-01 14:31:20 +0000
|
119
129
|
Processing by PostsController#index as HTML
|
120
130
|
Parameters: {}
|
121
|
-
GET http://api.com/posts [200]
|
122
|
-
|
123
|
-
|
124
|
-
To use it, simply add it to the stack of middleware:
|
125
|
-
|
126
|
-
```ruby
|
127
|
-
Spyke::Config.connection = Faraday.new(url: 'http://api.com') do |c|
|
128
|
-
c.request :json
|
129
|
-
c.use JSONParser
|
130
|
-
c.use Spyke::Middleware::RailsLogger if Rails.env.development?
|
131
|
-
c.use Faraday.default_adapter
|
132
|
-
end
|
131
|
+
Spyke (40.3ms) GET http://api.com/posts [200]
|
132
|
+
Completed 200 OK in 75ms (Views: 64.6ms | Spyke: 40.3ms | ActiveRecord: 0ms)
|
133
133
|
```
|
134
134
|
|
135
135
|
## Contributing
|
data/lib/spyke.rb
CHANGED
data/lib/spyke/http.rb
CHANGED
@@ -23,15 +23,18 @@ module Spyke
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def request(method, path, params = {})
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
ActiveSupport::Notifications.instrument('request.spyke', method: method) do |payload|
|
27
|
+
response = connection.send(method) do |request|
|
28
|
+
if method == :get
|
29
|
+
request.url path.to_s, params
|
30
|
+
else
|
31
|
+
request.url path.to_s
|
32
|
+
request.body = params
|
33
|
+
end
|
32
34
|
end
|
35
|
+
payload[:url], payload[:status] = response.env.url, response.status
|
36
|
+
Result.new_from_response(response)
|
33
37
|
end
|
34
|
-
Result.new_from_response(response)
|
35
38
|
end
|
36
39
|
|
37
40
|
def new_or_collection_from_result(result)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Spyke
|
2
|
+
module Instrumentation
|
3
|
+
module ControllerRuntime
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
attr_internal :spyke_runtime
|
9
|
+
|
10
|
+
def process_action(action, *args)
|
11
|
+
Spyke::Instrumentation::LogSubscriber.reset_runtime
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def cleanup_view_runtime
|
16
|
+
spyke_runtime_before_render = Spyke::Instrumentation::LogSubscriber.reset_runtime
|
17
|
+
self.spyke_runtime = (spyke_runtime || 0) + spyke_runtime_before_render
|
18
|
+
runtime = super
|
19
|
+
spyke_runtime_after_render = Spyke::Instrumentation::LogSubscriber.reset_runtime
|
20
|
+
self.spyke_runtime += spyke_runtime_after_render
|
21
|
+
runtime - spyke_runtime_after_render
|
22
|
+
end
|
23
|
+
|
24
|
+
def append_info_to_payload(payload)
|
25
|
+
super
|
26
|
+
payload[:spyke_runtime] = (spyke_runtime || 0) + Spyke::Instrumentation::LogSubscriber.reset_runtime
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def log_process_action(payload)
|
31
|
+
messages, spyke_runtime = super, payload[:spyke_runtime]
|
32
|
+
messages << ("Spyke: %.1fms" % spyke_runtime.to_f) if spyke_runtime
|
33
|
+
messages
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Spyke
|
2
|
+
module Instrumentation
|
3
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
4
|
+
def self.runtime=(value)
|
5
|
+
Thread.current['spyke_request_runtime'] = value
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.runtime
|
9
|
+
Thread.current['spyke_request_runtime'] ||= 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.reset_runtime
|
13
|
+
rt, self.runtime = runtime, 0
|
14
|
+
rt
|
15
|
+
end
|
16
|
+
|
17
|
+
def request(event)
|
18
|
+
return unless logger.debug?
|
19
|
+
self.class.runtime += event.duration
|
20
|
+
name = '%s (%.1fms)' % ["Spyke", event.duration]
|
21
|
+
details = "#{event.payload[:method].upcase} #{event.payload[:url]} [#{event.payload[:status]}]"
|
22
|
+
debug " #{color(name, GREEN, true)} #{color(details, BOLD, true)}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/spyke/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spyke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Balvig
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -276,7 +276,9 @@ files:
|
|
276
276
|
- lib/spyke/config.rb
|
277
277
|
- lib/spyke/exceptions.rb
|
278
278
|
- lib/spyke/http.rb
|
279
|
-
- lib/spyke/
|
279
|
+
- lib/spyke/instrumentation.rb
|
280
|
+
- lib/spyke/instrumentation/controller_runtime.rb
|
281
|
+
- lib/spyke/instrumentation/log_subscriber.rb
|
280
282
|
- lib/spyke/orm.rb
|
281
283
|
- lib/spyke/path.rb
|
282
284
|
- lib/spyke/relation.rb
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Spyke
|
2
|
-
module Middleware
|
3
|
-
class RailsLogger < Faraday::Middleware
|
4
|
-
CLEAR = "\e[0m"
|
5
|
-
BOLD = "\e[1m"
|
6
|
-
MAGENTA = "\e[35m"
|
7
|
-
|
8
|
-
def call(env)
|
9
|
-
logger.formatter = -> (severity, datetime, progname, msg) { msg }
|
10
|
-
|
11
|
-
logger.debug "\n\n\n\n#{env[:method].upcase} #{env[:url]}"
|
12
|
-
logger.debug "\n Headers: #{env[:request_headers]}"
|
13
|
-
logger.debug "\n Body: #{env[:body]}" if env[:body]
|
14
|
-
|
15
|
-
@app.call(env).on_complete do
|
16
|
-
Rails.logger.debug " #{BOLD}#{MAGENTA}#{env[:method].upcase} #{env[:url]} [#{env[:status]}]#{CLEAR}"
|
17
|
-
logger.debug "\n\nCompleted #{env[:status]}"
|
18
|
-
logger.debug "\n Headers: #{env[:response_headers]}"
|
19
|
-
logger.debug "\n Body: #{truncate_binary_values env[:body]}" if env[:body]
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def logger
|
26
|
-
@logger ||= Logger.new Rails.root.join('log', 'faraday.log')
|
27
|
-
end
|
28
|
-
|
29
|
-
def truncate_binary_values(body)
|
30
|
-
body.gsub(/(data:)([^"]+)/, 'data:...')
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|