Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a custom subclass of ActiveResource::Connection if possible #287

Merged
merged 2 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions lib/active_resource/connection_ext.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
require 'shopify_api/connection'

module ActiveResource
class Connection
attr_reader :response

module ResponseCapture
def handle_response(response)
@response = super
end
end

module RequestNotification
def request(method, path, *arguments)
super.tap do |response|
notify_about_request(response, arguments)
end
rescue => e
notify_about_request(e.response, arguments) if e.respond_to?(:response)
raise
end

def notify_about_request(response, arguments)
ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
payload[:response] = response
payload[:data] = arguments
end
end
end

prepend ResponseCapture
prepend RequestNotification
prepend ShopifyAPI::Connection::ResponseCapture
prepend ShopifyAPI::Connection::RequestNotification
end
end
9 changes: 8 additions & 1 deletion lib/shopify_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'active_support/core_ext/class/attribute_accessors'
require 'digest/md5'
require 'base64'
require 'active_resource/connection_ext'
require 'active_resource/detailed_log_subscriber'
require 'shopify_api/limits'
require 'shopify_api/json_format'
Expand All @@ -22,3 +21,11 @@ module ShopifyAPI
require 'shopify_api/countable'
require 'shopify_api/resources'
require 'shopify_api/session'
require 'shopify_api/connection'

# ActiveResource::Base.connection_class was introduced in https://github.com/rails/activeresource/pull/222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeyhew can you remove this comment? It's useful information for the pull request, but doesn't seem necessary in the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterjm sure thing

if ShopifyAPI::Base.respond_to?(:connection_class)
ShopifyAPI::Base.connection_class = ShopifyAPI::Connection
else
require 'active_resource/connection_ext'
end
33 changes: 33 additions & 0 deletions lib/shopify_api/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ShopifyAPI
class Connection < ActiveResource::Connection
attr_reader :response

module ResponseCapture
def handle_response(response)
@response = super
end
end

include ResponseCapture

module RequestNotification
def request(method, path, *arguments)
super.tap do |response|
notify_about_request(response, arguments)
end
rescue => e
notify_about_request(e.response, arguments) if e.respond_to?(:response)
raise
end

def notify_about_request(response, arguments)
ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
payload[:response] = response
payload[:data] = arguments
end
end
end

include RequestNotification
end
end