closeio-rails 0.1.0 → 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
- SHA1:
3
- metadata.gz: e7203477a04ff5ef6a351a70598b7106857e205c
4
- data.tar.gz: 076170b93c9f9a57b2a4fbf1064af82b036bf37e
2
+ SHA256:
3
+ metadata.gz: 0b47e709d54fee2cc7a82b087ec14ac40d5c8365fa8f11d23cc6ec8cb3b553ce
4
+ data.tar.gz: 05e6e984037ae84ce6e406a1aebf518372c1dd03a17c1255b23783a1c6638e50
5
5
  SHA512:
6
- metadata.gz: c19c337c975d042c456ee0bdf177ed051d27dc12053d801d745eb8935951fceadcc87934af3f9dc0cefd9c9ac67b879394cc0d1bea30a473ff73a3f36ff3ba7f
7
- data.tar.gz: 461ea3a9bbb20485c6294a6cdfc0fbd45e8de8fc79ebab5247beadda0523df9f8e40e52a1ead202b5a473f643183242a3d7d85cf9a50414fabe9d9e1774a1543
6
+ metadata.gz: 2c42f9f9a5d2274a561c8279d3c40cb58f6ac729669fe6b48f8e07d53680aa7008a53e517d136a01c1df4a543c95a6292ff5ab59c95142283aaab9f811638ba8
7
+ data.tar.gz: 905d7004844dc3ee11b8e3c938f9d17de26337f646d2813560f5c033a0f11c6baf1dc9e69895db334681aeac2be55885c6c108e0c64fa389972fd70f85683176
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  A handy wrapper around the `closeio` gem to make Rails integration a little easier.
3
3
 
4
4
  ## Models
5
- There are 4 models defined in this gem. The aim is to have a DSL a little more like the familiar ActiveRecord one.
5
+ There are 5 models defined in this gem. The aim is to have a DSL a little more like the familiar ActiveRecord one.
6
6
 
7
7
  ### `Closeio::Rails::Base`
8
8
  All the models inherit from `Closeio::Rails::Base`, which has a couple of mixins:
@@ -25,6 +25,35 @@ It's often useful to create and update custom fields in Close.io programatically
25
25
  ### `Closeio::Rails::Note`
26
26
  This is a very lightweight wrapper around Close::Client#create_note.
27
27
 
28
+ ### `Closeio::Rails::Webhook`
29
+ A similarly lightweight wrapper around Closeio::Client#create_webhook
30
+
31
+ ## Configuration
32
+ Configure the gem in an initializer like this:
33
+
34
+ ```
35
+ Rails.application.config.to_prepare do
36
+ Closeio::Rails.configure do |config|
37
+ config.api_key = 'your key' #or put in Rails secrets, or somewhere else
38
+ config.verbose = false
39
+ end
40
+ end
41
+ ```
42
+
43
+ You can add your own methods into the Closeio::Rails models by including them in your initializer like this:
44
+
45
+ ```
46
+ # Leadmethods and ContactMethods are modules in your Rails project. Obviously they could be namespaced, etc.
47
+ Closeio::Rails::Lead.send(:include, LeadMethods)
48
+ Closeio::Rails::Contact.send(:prepend, ContactMethods)
49
+ ```
50
+
51
+ You might want to add actions to the webhooks controller, too. Sometimes we use this on applications with some sort of authentication defined in `ApplicationController`, which needs to be ignored on the webhooks controller:
52
+
53
+ ```
54
+ Closeio::Rails::WebhooksController.send(:skip_before_action, :authenticate_user!)
55
+ Closeio::Rails::WebhooksController.send(:skip_after_action, :verify_authorized)
56
+ ```
28
57
 
29
58
  ## Mountable webhooks controller
30
59
  It's useful to receive webhooks from Close.io. You have to drop a note to their support team to set it up but it's very quick.
@@ -44,6 +73,69 @@ webhooks POST /webhooks(.:format) closeio/rails/webhooks#create {:format=>
44
73
 
45
74
  The `debug_webhooks_path` only works in development. It just sanity-checks that you've got ot mounted properly :)
46
75
 
76
+ ## Adding webhooks - rake tasks
77
+ There are 3 rake tasks which are available to use in your application:
78
+
79
+ ### `rake closeio:rails:list_webhooks`
80
+ Give you a list of webhooks with their ID and endpoint
81
+
82
+ ### `rake closeio:rails:create_lead_webhook[endpoint_url]`
83
+ Sets up a webhook to track leads. Pass in an endpoint URL.
84
+
85
+ ### `rake closeio:rails:destroy_webhook[id]`
86
+ Pass in the ID you got from `rake closeio:rails:list_webhooks` to destroy a webhook and no longer receive messages to that endpoint.
87
+
88
+ ## Adding other webhooks
89
+ You can add other webhooks with different subscriptions by calling `Close::Rails::Webhook.create(params)` where params is a hash which looks like this:
90
+
91
+ ```
92
+ Closeio::Rails::Webhook.create({
93
+ url: url,
94
+ events: [
95
+ {
96
+ object_type: 'lead',
97
+ action: 'created'
98
+ },
99
+ {
100
+ object_type: 'lead',
101
+ action: 'updated'
102
+ },
103
+ {
104
+ object_type: 'lead',
105
+ action: 'deleted'
106
+ },
107
+ {
108
+ object_type: 'lead',
109
+ action: 'merged'
110
+ }
111
+ ]
112
+ })
113
+ end
114
+ ```
115
+
116
+ See [the docs](https://developer.close.com/#webhooks) for more details.
117
+
118
+ ### Consuming webhooks in your application
119
+ The controller doesn't do anything when it receives a webhook - instead it uses ActiveSupport::Notifications to allow you to subscribe to the raised events to do what you need to. Here's an example:
120
+
121
+ ```
122
+ ActiveSupport::Notifications.subscribe(/closeio\.(create|update|delete)/) do |name, start, finish, id, payload|
123
+ if payload[:model] == 'lead'
124
+ SomeLeadRelatedJob.perform_later payload[:data][:id]
125
+ end
126
+ end
127
+ ```
128
+
129
+ Check the Close.io docs to get a sense of what you get in the payload - one important thing to nose is that when you get a 'closeio.merge' notification, you don't get an ID, you'll get a `:source_id` and a `:destination_id`.
130
+
131
+ Importantly, if you're using ActiveJob with the `inline` strategy you'll need to make sure you rescue from any failures, or the controller won't return a 200 OK and Close.io will keep retrying.
132
+
133
+ # Contributing
134
+ Pull requests welcome. The usual drill: fork, commit changes, PR.
135
+
136
+ # Licence
137
+ MIT - see LICENCE.txt.
138
+
47
139
 
48
140
 
49
141
 
@@ -12,8 +12,7 @@ module Closeio
12
12
 
13
13
  def create
14
14
  request.format = :json
15
- event = params[:event]
16
- ActiveSupport::Notifications.instrument("closeio.#{event}", params)
15
+ ActiveSupport::Notifications.instrument("closeio.#{params[:action]}", params)
17
16
 
18
17
  #must return an ok - check Rails version to determine whether to return nothing or head response.
19
18
  if ::Rails.version =~ /^4/
@@ -4,13 +4,13 @@ module Closeio
4
4
  attr_reader :contacts
5
5
 
6
6
  def self.all
7
- Closeio::Rails.configuration.client.list_leads("*", per_page: 99999).data.collect do |lead|
7
+ Closeio::Rails.configuration.client.list_leads("*", paginate: true)[:data].collect do |lead|
8
8
  self.new(lead.to_hash)
9
9
  end
10
10
  end
11
11
 
12
12
  def self.with_status(status)
13
- Closeio::Rails.configuration.client.list_leads("lead_status: #{status}", per_page: 99999).data.collect do |lead|
13
+ Closeio::Rails.configuration.client.list_leads("lead_status: #{status}", paginate: true)[:data].collect do |lead|
14
14
  self.new(lead.to_hash)
15
15
  end
16
16
  end
@@ -0,0 +1,34 @@
1
+ module Closeio
2
+ module Rails
3
+ class Webhook < Base
4
+ def self.all
5
+ Closeio::Rails.configuration.client.list_webhooks['data'].collect do |webhook|
6
+ self.new(webhook)
7
+ end
8
+ end
9
+
10
+ def self.create(params)
11
+ response = Closeio::Rails.configuration.client.create_webhook(params)
12
+ if response.has_key?('field-errors')
13
+ raise Closeio::Error, "#{response['field-errors']['name']}"
14
+ elsif response.has_key?('error')
15
+ raise Closeio::Error, "#{response['error']}"
16
+ else
17
+ response
18
+ end
19
+ end
20
+
21
+ def self.destroy!(id)
22
+ response = Closeio::Rails.configuration.client.delete_webhook(id)
23
+ if response.has_key?('field-errors')
24
+ raise Closeio::Error, "#{response['field-errors']['name']}"
25
+ elsif response.has_key?('error')
26
+ raise Closeio::Error, "#{response['error']}"
27
+ else
28
+ response
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+ end
data/close-rails.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
 
26
26
  spec.add_dependency "require_all", "~> 1.3"
27
- spec.add_dependency "closeio", "~> 2.6"
27
+ spec.add_dependency "closeio", "~>3.5"
28
28
  spec.add_dependency "rails", ">= 4.2", "< 6.0.0"
29
29
 
30
30
  end
data/lib/closeio/rails.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'closeio'
1
2
  require 'require_all'
2
3
  require_rel '.'
3
4
 
@@ -1,5 +1,5 @@
1
1
  module Closeio
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,43 @@
1
+ namespace :closeio do
2
+ namespace :rails do
3
+ desc "List webhooks"
4
+ task list_webhooks: :environment do
5
+ Closeio::Rails::Webhook.all.each do |webhook|
6
+ puts "id:#{webhook.id} endpoint:#{webhook.url}"
7
+ end
8
+ end
9
+
10
+ desc "Create webhook with defined endpoints"
11
+ task :create_leads_webhook, [:endpoint] => :environment do |task, args|
12
+ url = args[:endpoint]
13
+ raise Closeio::Rails::Error, "You need to include an endpoint URL or have configured the default URL for your environment" unless url.present?
14
+ Closeio::Rails::Webhook.create({
15
+ url: url,
16
+ events: [
17
+ {
18
+ object_type: 'lead',
19
+ action: 'created'
20
+ },
21
+ {
22
+ object_type: 'lead',
23
+ action: 'updated'
24
+ },
25
+ {
26
+ object_type: 'lead',
27
+ action: 'deleted'
28
+ },
29
+ {
30
+ object_type: 'lead',
31
+ action: 'merged'
32
+ }
33
+ ]
34
+ })
35
+ end
36
+
37
+ desc "Remove a webhook by ID"
38
+ task :destroy_webhook, [:id] => :environment do |task, args|
39
+ raise Closeio::Rails::Error, "You need to include a webhook subscription ID to destroy it" unless args[:id].present?
40
+ Closeio::Rails::Webhook.destroy!(args[:id])
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closeio-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed Jones
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-10-08 00:00:00.000000000 Z
12
+ date: 2019-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -59,14 +59,14 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '2.6'
62
+ version: '3.5'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '2.6'
69
+ version: '3.5'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rails
72
72
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +109,7 @@ files:
109
109
  - app/models/closeio/rails/custom_field.rb
110
110
  - app/models/closeio/rails/lead.rb
111
111
  - app/models/closeio/rails/note.rb
112
+ - app/models/closeio/rails/webhook.rb
112
113
  - app/models/concerns/closeio/rails/attributes.rb
113
114
  - app/models/concerns/closeio/rails/date_coercion.rb
114
115
  - bin/console
@@ -118,6 +119,7 @@ files:
118
119
  - lib/closeio/rails.rb
119
120
  - lib/closeio/rails/engine.rb
120
121
  - lib/closeio/rails/version.rb
122
+ - lib/tasks/webhooks.rake
121
123
  homepage: https://github.com/errorstudio/close-rails
122
124
  licenses:
123
125
  - MIT
@@ -138,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
140
  version: '0'
139
141
  requirements: []
140
142
  rubyforge_project:
141
- rubygems_version: 2.6.12
143
+ rubygems_version: 2.7.7
142
144
  signing_key:
143
145
  specification_version: 4
144
146
  summary: A wrapper around the closeio gem to provide some useful functionality in