zapier_ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8141f429752055c586e7caa70087653279a331a3
4
+ data.tar.gz: 89a0de3442e99a0626e630cb79f2ef57798d5465
5
+ SHA512:
6
+ metadata.gz: 1d29c01ee77188a0814b4b69384974d070d346b4b4f87c672ea0fafd37d0a68179aa3abab6cfaa6beb7ca1faecb80a43f267872e6bee82ef9935d76409a89efe
7
+ data.tar.gz: ae6669128464030bd5f7b0201f2abaf0ed549d67354fab91d92d64db12b99bbfa695a5af19f2c8d30e7542d0670e51e1fbf36a2e805d2010bafc51a75ee51d1f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zapier_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 David Peterson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # ZapierRuby
2
+
3
+ Zapier Ruby provides a simple wrapper to post a 'zap' to a Zapier (https://zapier.com) webhook from any Ruby application. You must first have a Zapier account and have created a webhook configured to 'catch hook'. This gem is useful for simple integrations, such as posting to slack when an event happens in your Rails app, or sending an email when your chef deploy has completed.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zapier_ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install zapier_ruby
20
+
21
+ ## Usage
22
+
23
+ ### General Usage
24
+ First, configure ZapierRuby. Pass a hash of each of your zap webhooks you would like to integrate, you can also change the uri we post to or disable logging.
25
+
26
+ ```
27
+ ZapierRuby.configure.do |c|
28
+ c.web_hooks = {example_zap: "webhook_id"}
29
+ c.enable_logging = false
30
+ end
31
+ ```
32
+
33
+ You can find the value to fill in for webhook id in the location highlighted below ('xxxxxx' in the green box) when configuring your Zap:
34
+
35
+ ![](https://github.com/pete2786/pete2786.github.io/blob/master/images/finding_webhook.png)
36
+
37
+ Next, instantiate a Zapper for the webhook to hit. Then, use the `zap` method with hash of params and send it to the Zapier web hook.
38
+
39
+ ```
40
+ zapper=ZapierRuby::Zapper.new(:example_zap)
41
+ zapper.zap({hello: "world"})
42
+ ```
43
+
44
+ Each param you send can be used by Zapier, so include all of the information required to complete the task.
45
+
46
+ ### Rails Usage
47
+ If you are using ZapierRuby with Rails, I'd recommend using creating an initializer (ex. config/intializers/zapier_ruby.rb) and with the following:
48
+
49
+ ```
50
+ ZapierRuby.configure.do |c|
51
+ c.web_hooks = {example_zap: "zap_webhook_id"}
52
+ end
53
+ ```
54
+
55
+ #### Example Usage
56
+
57
+ If you do not have email configured for you application, you could send an email via a Zap to notify a new user that their account has been created.
58
+ ```
59
+ class User < ActiveRecord::Base
60
+ after_create :welcome_new_user
61
+
62
+ def welcome_new_user
63
+ ZapierRuby::Zapper.new(:welcome_new_user).zap(user.attributes)
64
+ end
65
+ end
66
+ ```
67
+
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( https://github.com/[my-github-username]/zapier_ruby/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ module ZapierRuby
2
+ class Config
3
+ attr_accessor :base_uri, :web_hooks, :enable_logging, :logger
4
+
5
+ def initialize
6
+ self.base_uri = "https://zapier.com/hooks/catch/"
7
+ self.web_hooks = { example_webhook: "webhook_id" }
8
+ self.enable_logging = true
9
+ self.logger = Logger.new(STDOUT)
10
+ end
11
+
12
+ def configure_with(path_to_yaml_file)
13
+ begin
14
+ config_yaml = YAML::load(IO.read(path_to_yaml_file))
15
+ reconfigure(config_yaml)
16
+ rescue Exception => e
17
+ logger.error "YAML configuration file cannot be loaded. Using defaults. Error :#{e.message}"
18
+ return
19
+ end
20
+ end
21
+
22
+ def reconfigure(config = {})
23
+ config.keys.select{|k| respond_to?("#{k}=")}.each{|k| self.send("#{k}=", config[k])}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module ZapierRuby
2
+ class LoggerDecorator < SimpleDelegator
3
+ attr_accessor :enable_logging
4
+
5
+ def initialize(enable_logging)
6
+ self.enable_logging = enable_logging
7
+ super(ZapierRuby.config.logger)
8
+ end
9
+
10
+ def error(message)
11
+ super if enable_logging
12
+ end
13
+
14
+ def info(message)
15
+ super if enable_logging
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module ZapierRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ module ZapierRuby
2
+ class Zapper
3
+ attr_accessor :zap_name, :logger
4
+
5
+ def initialize(zap_name)
6
+ self.zap_name = zap_name
7
+ self.logger = LoggerDecorator.new(config.enable_logging)
8
+ end
9
+
10
+ def zap(params={})
11
+ unless zap_web_hook_id
12
+ logger.error "No zap configured for #{zap_name}. Configured webhooks: #{config.web_hooks.to_s}"
13
+ return false
14
+ end
15
+
16
+ logger.info "Zapping #{zap_name} with params: #{params.to_json}"
17
+ post_zap(params)
18
+ end
19
+
20
+ def post_zap(params)
21
+ begin
22
+ rest_client.post(params, zap_headers)
23
+ true
24
+ rescue Exception => e
25
+ logger.error "Unable to post to Zapier url: #{zap_url} with params: #{params.to_json}. Error: #{e.message}"
26
+ false
27
+ end
28
+ end
29
+ private :post_zap
30
+
31
+ def rest_client
32
+ @rest_client ||= RestClient::Resource.new(zap_url, ssl_version: 'TLSv1')
33
+ end
34
+ private :rest_client
35
+
36
+ def zap_web_hook_id
37
+ return @zap_web_hook if defined?(@zap_web_hook)
38
+
39
+ @zap_web_hook = config.web_hooks[zap_name]
40
+ end
41
+ private :zap_web_hook_id
42
+
43
+ def zap_headers
44
+ {
45
+ "Accept" => " application/json",
46
+ "Content-Type" => "application/json"
47
+ }
48
+ end
49
+ private :zap_headers
50
+
51
+ def zap_url
52
+ "#{config.base_uri}#{zap_web_hook_id}/"
53
+ end
54
+ private :zap_url
55
+
56
+ def config
57
+ ZapierRuby.config
58
+ end
59
+ private :config
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ require 'rest_client'
2
+ require 'yaml'
3
+ require 'delegate'
4
+ require 'logger'
5
+
6
+ require 'zapier_ruby/version'
7
+ require 'zapier_ruby/logger_decorator'
8
+ require 'zapier_ruby/config'
9
+ require 'zapier_ruby/zapper'
10
+
11
+ module ZapierRuby
12
+ class << self
13
+ attr_accessor :config
14
+ end
15
+
16
+ def self.configure
17
+ self.config ||= ZapierRuby::Config.new
18
+ yield(config) if block_given?
19
+ end
20
+
21
+ def self.configure_with(path_to_yaml_file)
22
+ self.config ||= ZapierRuby::Config.new
23
+ config.configure_with(path_to_yaml_file)
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zapier_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zapier_ruby"
8
+ spec.version = ZapierRuby::VERSION
9
+ spec.authors = ["David Peterson"]
10
+ spec.email = ["pete2786@umn.edu"]
11
+ spec.summary = %q{Simple gem to integrate Zapier webhooks with a Ruby project.}
12
+ spec.description = %q{Simple gem to integrate Zapier webhooks with a Ruby project.}
13
+ spec.homepage = "http://pete2786.github.io"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zapier_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Peterson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple gem to integrate Zapier webhooks with a Ruby project.
56
+ email:
57
+ - pete2786@umn.edu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/zapier_ruby.rb
68
+ - lib/zapier_ruby/config.rb
69
+ - lib/zapier_ruby/logger_decorator.rb
70
+ - lib/zapier_ruby/version.rb
71
+ - lib/zapier_ruby/zapper.rb
72
+ - zapier_ruby.gemspec
73
+ homepage: http://pete2786.github.io
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Simple gem to integrate Zapier webhooks with a Ruby project.
97
+ test_files: []