webhook-payload 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p125@webhook-payload --create
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in webhook-payload.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Allen Madsen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # webhook-payload
2
+
3
+ [![Build Status](http://travis-ci.org/blatyo/webhook-payload.png)](http://travis-ci.org/blatyo/webhook-payload)
4
+
5
+ This gem is a convenience wrapper for [Github's webhook payload](https://help.github.com/articles/post-receive-hooks) that is triggered from a post receive hook. Feed it a hash of data and it will parse it into an object that you can use.
6
+
7
+ ## Setup
8
+
9
+ **Gemfile**
10
+ ``` ruby
11
+ gem 'webhook-payload'
12
+ ```
13
+
14
+ **Manual**
15
+ ``` ruby
16
+ require 'webhook/payload'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Say you have a hash from a post receive hook like so:
22
+
23
+ ``` ruby
24
+ payload_data = {
25
+ "before" => "5aef35982fb2d34e9d9d4502f6ede1072793222d",
26
+ "repository" => {
27
+ "url" => "http://github.com/defunkt/github",
28
+ "name" => "github",
29
+ "description" => "You're lookin' at it.",
30
+ "watchers" => 5,
31
+ "forks" => 2,
32
+ "private" => 1,
33
+ "owner" => {
34
+ "email" => "chris@ozmm.org",
35
+ "name" => "defunkt"
36
+ }
37
+ },
38
+ "commits" => [
39
+ {
40
+ "id" => "41a212ee83ca127e3c8cf465891ab7216a705f59",
41
+ "url" => "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
42
+ "author" => {
43
+ "email" => "chris@ozmm.org",
44
+ "name" => "Chris Wanstrath"
45
+ },
46
+ "message" => "okay i give in",
47
+ "timestamp" => "2008-02-15T14:57:17-08:00",
48
+ "added" => ["filepath.rb"]
49
+ },
50
+ {
51
+ "id" => "de8251ff97ee194a289832576287d6f8ad74e3d0",
52
+ "url" => "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
53
+ "author" => {
54
+ "email" => "chris@ozmm.org",
55
+ "name" => "Chris Wanstrath"
56
+ },
57
+ "message" => "update pricing a tad",
58
+ "timestamp" => "2008-02-15T14:36:34-08:00"
59
+ }
60
+ ],
61
+ "after" => "de8251ff97ee194a289832576287d6f8ad74e3d0",
62
+ "ref" => "refs/heads/master"
63
+ }
64
+ ```
65
+
66
+ You can load that into an object with:
67
+
68
+ ``` ruby
69
+ payload = Webhook::Payload.new(payload_data)
70
+ ```
71
+
72
+ Then you can access the data via methods rather than through hash keys. It also does conversion of timestamps to `Time`, integer booleans to `true` or `false`, urls to `URI`, and file to `Pathname`.
73
+
74
+ ``` ruby
75
+ payload.after #=> "de8251ff97ee194a289832576287d6f8ad74e3d0"
76
+ payload.repository.url #=> #<URI::HTTP:0x007ff14b0ace60 URL:http://github.com/defunkt/github>
77
+ payload.commits.first.author.name #=> "Chris Wanstrath"
78
+ payload.commits.first.timestamp #=> 2008-02-15 17:57:17 -0500
79
+ payload.commits.first.timestamp.class #=> Time
80
+ payload.commits.first.added.first #=> #<Pathname:filepath.rb>
81
+ payload.repository.private? #=> true
82
+ ```
83
+
84
+ Hopefully, everything else you can do is pretty obvious. Method names and hash key names are all the same.
85
+
86
+ ## Note on Reporting Issues
87
+
88
+ * Try to make a failing test case
89
+ * Tell me which version of ruby you're using
90
+ * Tell me which OS you are using
91
+ * Provide me with any extra files if necessary
92
+
93
+ ## Note on Patches/Pull Requests
94
+ * Fork the project.
95
+ * Make your feature addition or bug fix.
96
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
97
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
98
+ * Send me a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
6
+ task :test => :spec
@@ -0,0 +1,2 @@
1
+ require "webhook/payload"
2
+ require "webhook/payload/version"
@@ -0,0 +1,81 @@
1
+ require 'virtus'
2
+ require 'pathname'
3
+ require 'uri'
4
+
5
+ module Virtus
6
+ class Coercion
7
+ class String < Virtus::Coercion::Object
8
+ def self.to_pathname(value)
9
+ Pathname.new(value)
10
+ end
11
+
12
+ def self.to_uri(value)
13
+ URI.parse(value)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module Webhook
20
+ class Payload
21
+ include Virtus
22
+
23
+ module User
24
+ include Virtus
25
+
26
+ attribute :email, String
27
+ attribute :name, String
28
+ end
29
+
30
+ class URI < Virtus::Attribute::Object
31
+ primitive String
32
+ coercion_method :to_uri
33
+ end
34
+
35
+ class Commit
36
+ include Virtus
37
+
38
+ class Author
39
+ include User
40
+ end
41
+
42
+ class Pathname < Virtus::Attribute::Object
43
+ primitive String
44
+ coercion_method :to_pathname
45
+ end
46
+
47
+ attribute :id, String
48
+ attribute :url, URI
49
+ attribute :message, String
50
+ attribute :timestamp, Time
51
+ attribute :added, Array[Pathname], :default => []
52
+ attribute :modified, Array[Pathname], :default => []
53
+ attribute :removed, Array[Pathname], :default => []
54
+ attribute :author, Author
55
+ end
56
+
57
+ class Repository
58
+ include Virtus
59
+
60
+ class Owner
61
+ include User
62
+ end
63
+
64
+ attribute :url, URI
65
+ attribute :name, String
66
+ attribute :homepage, String
67
+ attribute :pledgie, String
68
+ attribute :description, String
69
+ attribute :watchers, Integer
70
+ attribute :forks, Integer
71
+ attribute :private, Boolean
72
+ attribute :owner, Owner
73
+ end
74
+
75
+ attribute :before, String
76
+ attribute :after, String
77
+ attribute :ref, String
78
+ attribute :repository, Repository
79
+ attribute :commits, Array[Commit]
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ module Webhook
2
+ class Payload
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path("../../lib/webhook-payload", __FILE__)
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe Webhook::Payload do
4
+ let(:payload){Webhook::Payload.new(payload_data)}
5
+ subject{payload}
6
+
7
+ its(:before){should == "5aef35982fb2d34e9d9d4502f6ede1072793222d"}
8
+ its(:after){should == "de8251ff97ee194a289832576287d6f8ad74e3d0"}
9
+ its(:ref){should == "refs/heads/master"}
10
+
11
+ it "should have two commits" do
12
+ subject.commits.size.should == 2
13
+ end
14
+
15
+ describe "#respository" do
16
+ let(:repository){payload.repository}
17
+ subject{repository}
18
+
19
+ its(:url){should == URI.parse("http://github.com/defunkt/github")}
20
+ its(:name){should == "github"}
21
+ its(:pledgie){should be_nil}
22
+ its(:homepage){should be_nil}
23
+ its(:description){should == "You're lookin' at it."}
24
+ its(:watchers){should == 5}
25
+ its(:forks){should == 2}
26
+ its(:private){should == true}
27
+
28
+ describe "#owner" do
29
+ subject{repository.owner}
30
+
31
+ its(:email){should == "chris@ozmm.org"}
32
+ its(:name){should == "defunkt"}
33
+ end
34
+ end
35
+
36
+ describe "a commit" do
37
+ let(:commit){payload.commits.first}
38
+ subject{commit}
39
+
40
+ its(:id){should == "41a212ee83ca127e3c8cf465891ab7216a705f59"}
41
+ its(:url){should == URI.parse("http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59")}
42
+ its(:message){should == "okay i give in"}
43
+ its(:timestamp){should == Time.parse("2008-02-15T14:57:17-08:00")}
44
+ its(:added){should == [Pathname.new("filepath.rb")]}
45
+ its(:removed){should == []}
46
+ its(:modified){should == []}
47
+
48
+ describe "#author" do
49
+ subject{commit.author}
50
+
51
+ its(:email){should == "chris@ozmm.org"}
52
+ its(:name){should == "Chris Wanstrath"}
53
+ end
54
+ end
55
+
56
+ let(:payload_data) do
57
+ {
58
+ "before" => "5aef35982fb2d34e9d9d4502f6ede1072793222d",
59
+ "repository" => {
60
+ "url" => "http://github.com/defunkt/github",
61
+ "name" => "github",
62
+ "description" => "You're lookin' at it.",
63
+ "watchers" => 5,
64
+ "forks" => 2,
65
+ "private" => 1,
66
+ "owner" => {
67
+ "email" => "chris@ozmm.org",
68
+ "name" => "defunkt"
69
+ }
70
+ },
71
+ "commits" => [
72
+ {
73
+ "id" => "41a212ee83ca127e3c8cf465891ab7216a705f59",
74
+ "url" => "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
75
+ "author" => {
76
+ "email" => "chris@ozmm.org",
77
+ "name" => "Chris Wanstrath"
78
+ },
79
+ "message" => "okay i give in",
80
+ "timestamp" => "2008-02-15T14:57:17-08:00",
81
+ "added" => ["filepath.rb"]
82
+ },
83
+ {
84
+ "id" => "de8251ff97ee194a289832576287d6f8ad74e3d0",
85
+ "url" => "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
86
+ "author" => {
87
+ "email" => "chris@ozmm.org",
88
+ "name" => "Chris Wanstrath"
89
+ },
90
+ "message" => "update pricing a tad",
91
+ "timestamp" => "2008-02-15T14:36:34-08:00"
92
+ }
93
+ ],
94
+ "after" => "de8251ff97ee194a289832576287d6f8ad74e3d0",
95
+ "ref" => "refs/heads/master"
96
+ }
97
+ end
98
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "webhook/payload/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "webhook-payload"
7
+ s.version = Webhook::Payload::VERSION
8
+ s.authors = ["Allen Madsen"]
9
+ s.email = ["allen.c.madsen@gmail.com"]
10
+ s.homepage = "https://github.com/blatyo/webhook-payload"
11
+ s.summary = "This gem is a convenience wrapper for Github's webhook payload (https://help.github.com/articles/post-receive-hooks) that is triggered from a post receive hook. Feed it a hash of data and it will parse it into an object that you can use."
12
+ s.description = "This gem is a convenience wrapper for Github's webhook payload (https://help.github.com/articles/post-receive-hooks) that is triggered from a post receive hook. Feed it a hash of data and it will parse it into an object that you can use. It also provides conversions from basic data types into more useful types like Time, URI, Pathname, and Boolean."
13
+
14
+ s.rubyforge_project = "webhook-payload"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rake"
24
+ s.add_runtime_dependency "virtus"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webhook-payload
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Allen Madsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70133146630780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70133146630780
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70133146630320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70133146630320
36
+ - !ruby/object:Gem::Dependency
37
+ name: virtus
38
+ requirement: &70133146629860 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70133146629860
47
+ description: This gem is a convenience wrapper for Github's webhook payload (https://help.github.com/articles/post-receive-hooks)
48
+ that is triggered from a post receive hook. Feed it a hash of data and it will parse
49
+ it into an object that you can use. It also provides conversions from basic data
50
+ types into more useful types like Time, URI, Pathname, and Boolean.
51
+ email:
52
+ - allen.c.madsen@gmail.com
53
+ executables: []
54
+ extensions: []
55
+ extra_rdoc_files: []
56
+ files:
57
+ - .gitignore
58
+ - .rspec
59
+ - .rvmrc
60
+ - .travis.yml
61
+ - Gemfile
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/webhook-payload.rb
66
+ - lib/webhook/payload.rb
67
+ - lib/webhook/payload/version.rb
68
+ - spec/spec_helper.rb
69
+ - spec/webhook/payload_spec.rb
70
+ - webhook-payload.gemspec
71
+ homepage: https://github.com/blatyo/webhook-payload
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project: webhook-payload
91
+ rubygems_version: 1.8.17
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: This gem is a convenience wrapper for Github's webhook payload (https://help.github.com/articles/post-receive-hooks)
95
+ that is triggered from a post receive hook. Feed it a hash of data and it will parse
96
+ it into an object that you can use.
97
+ test_files:
98
+ - spec/spec_helper.rb
99
+ - spec/webhook/payload_spec.rb