spyke 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ccd9957900a277390812c84d78b30985292cb2a9
4
- data.tar.gz: c5cdb9be619f68febc8b20a51d339db19fcb7d4e
3
+ metadata.gz: 0d5dfe590d65f64f97fda8760b3c75ea3900d494
4
+ data.tar.gz: be9546662e362944f6ea66373c76821e23d1acb1
5
5
  SHA512:
6
- metadata.gz: af321cc8a304dc3e169ce7d31f113db4a7032de30f4b96e886d3ad8db6961a66b8f88ae932f732aa733b272a69a9840dc5226e9692cb40b3047bc4b44dadbe16
7
- data.tar.gz: cddd07072bde4a26fe0bfa5b497747f612f09327f7e7edb35f8f9e428959b32fdbaa90d1bd87e611e500e916e16b730fdfd05460a8e9b7d983ccd4c85ac460c5
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 showed significant performance problems and maintenance seemed to had gone stale.
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
- ### Logging/Debugging
122
+ ### Log output
112
123
 
113
- Spyke comes with Faraday middleware for Rails that will output helpful
114
- ActiveRecord-like output to the main log as well as keep a record of
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
@@ -1,7 +1,7 @@
1
1
  require 'active_support'
2
2
  require 'active_support/core_ext'
3
3
  require 'spyke/base'
4
- require 'spyke/middleware/rails_logger' if defined?(Rails)
4
+ require 'spyke/instrumentation' if defined?(Rails)
5
5
  require 'spyke/version'
6
6
 
7
7
  module Spyke
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
- response = connection.send(method) do |request|
27
- if method == :get
28
- request.url path.to_s, params
29
- else
30
- request.url path.to_s
31
- request.body = params
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,8 @@
1
+ require 'spyke/instrumentation/log_subscriber'
2
+ require 'spyke/instrumentation/controller_runtime'
3
+
4
+ Spyke::Instrumentation::LogSubscriber.attach_to :spyke
5
+
6
+ ActiveSupport.on_load(:action_controller) do
7
+ include Spyke::Instrumentation::ControllerRuntime
8
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.4.1'
2
+ VERSION = '1.5.0'
3
3
  end
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.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-06 00:00:00.000000000 Z
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/middleware/rails_logger.rb
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