api_notify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +21 -0
- data/lib/api_notify.rb +11 -0
- data/lib/api_notify/active_record/logger.rb +11 -0
- data/lib/api_notify/active_record/main.rb +120 -0
- data/lib/api_notify/active_record/synchronizer.rb +87 -0
- data/lib/api_notify/engine.rb +4 -0
- data/lib/api_notify/hooks.rb +10 -0
- data/lib/api_notify/railtie.rb +12 -0
- data/lib/api_notify/version.rb +3 -0
- data/lib/tasks/api_notify_tasks.rake +4 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e25935a7b0f1fd786759eff72cdd365534379960
|
4
|
+
data.tar.gz: 527091dfa0cf26a1a13c416d8f1730238feb24cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3affb4ecb095ad2c46eb54240dfd3507aa51c1062b9499c0daea3e5046d90cde81c9f58db8acbb1401eef549cdfa7c605a17ba421f9c8f48bf80f27718fe79b
|
7
|
+
data.tar.gz: 96c7bdd3d9e525d83597f129692423f19121b1714a3a739153cd32de03f64d06b3b5ada7203f9b0f7f3892b61632b2ffa8e581ea0b9458f83a6c7935f3636a65
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 EDGARS KAUKIS (MOGO.LV)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ApiNotify'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
data/lib/api_notify.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/array/extract_options'
|
3
|
+
require 'active_support/deprecation/reporting'
|
4
|
+
require "api_notify/active_record/synchronizer"
|
5
|
+
|
6
|
+
module ApiNotify
|
7
|
+
module ActiveRecord
|
8
|
+
module Main
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
METHODS = %w[post get delete put]
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def api_notify(fields, identificators, *args)
|
15
|
+
options = args.extract_options!
|
16
|
+
|
17
|
+
after_update :post_via_api
|
18
|
+
after_create :post_via_api
|
19
|
+
after_destroy :delete_via_api
|
20
|
+
|
21
|
+
attr_accessor :skip_api_notify
|
22
|
+
|
23
|
+
define_method :notify_attributes do
|
24
|
+
fields
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method :identificators do
|
28
|
+
identificators
|
29
|
+
end
|
30
|
+
|
31
|
+
METHODS.each do |method|
|
32
|
+
define_method "api_notify_#{method}_success" do |response|
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "api_notify_#{method}_failed" do |response|
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
options.each_pair do |key, value|
|
40
|
+
define_singleton_method key do
|
41
|
+
value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
define_singleton_method :synchronizer do
|
46
|
+
begin
|
47
|
+
_api_route_name = api_route_name
|
48
|
+
ApiNotify::LOGGER.info "api_route_name2: #{api_route_name}"
|
49
|
+
rescue Exception => e
|
50
|
+
_api_route_name = class_name.pluralize
|
51
|
+
end
|
52
|
+
|
53
|
+
ApiNotify::ActiveRecord::Synchronizer.new _api_route_name.downcase, identificators.keys.first
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def disable_api_notify
|
60
|
+
self.skip_api_notify = true
|
61
|
+
end
|
62
|
+
|
63
|
+
def enable_api_notify
|
64
|
+
self.skip_api_notify = false
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Check if any watched attribute changed
|
69
|
+
# Unless any attribute changed than dont send any request
|
70
|
+
# if is_synchronized defined than check if its true, unless its true synchronize it
|
71
|
+
|
72
|
+
def attributes_as_params(method)
|
73
|
+
_fields = {}
|
74
|
+
must_sync = false
|
75
|
+
if defined? self.class.is_synchronized
|
76
|
+
ApiNotify::LOGGER.info "Is synchronized: #{self.class.is_synchronized}"
|
77
|
+
must_sync = !send(self.class.is_synchronized)
|
78
|
+
end
|
79
|
+
|
80
|
+
notify_attributes.each do |field|
|
81
|
+
if send("#{field.to_s}_changed?") || must_sync
|
82
|
+
_fields[field] = self.send(field)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
return _fields if _fields.empty? && method != "delete"
|
87
|
+
|
88
|
+
identificators.each_pair do |key, value|
|
89
|
+
_fields[key] = self.send(value)
|
90
|
+
end
|
91
|
+
|
92
|
+
ApiNotify::LOGGER.info "fields: #{_fields}"
|
93
|
+
_fields
|
94
|
+
end
|
95
|
+
|
96
|
+
def method_missing(m, *args)
|
97
|
+
vars = m.to_s.split(/_/, 2)
|
98
|
+
if METHODS.include?(vars.first) && vars.last == "via_api"
|
99
|
+
ApiNotify::LOGGER.info "called method: #{m}"
|
100
|
+
return if skip_api_notify || attributes_as_params(vars.first).empty?
|
101
|
+
synchronizer = self.class.synchronizer
|
102
|
+
synchronizer.set_params(attributes_as_params(vars.first))
|
103
|
+
synchronizer.send_request(vars.first.upcase)
|
104
|
+
|
105
|
+
disable_api_notify
|
106
|
+
|
107
|
+
if synchronizer.success?
|
108
|
+
send("api_notify_#{vars.first}_success", synchronizer.response)
|
109
|
+
else
|
110
|
+
send("api_notify_#{vars.first}_failed", synchronizer.response)
|
111
|
+
end
|
112
|
+
|
113
|
+
enable_api_notify
|
114
|
+
else
|
115
|
+
super
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ApiNotify
|
2
|
+
module ActiveRecord
|
3
|
+
class Synchronizer
|
4
|
+
require "net/http"
|
5
|
+
require 'net/https'
|
6
|
+
|
7
|
+
def initialize route_name, id_param
|
8
|
+
@config = load_config_yaml
|
9
|
+
@_params = {}
|
10
|
+
@route_name = route_name
|
11
|
+
@_success = false
|
12
|
+
@id_param = id_param
|
13
|
+
end
|
14
|
+
|
15
|
+
def response
|
16
|
+
if @_response.body
|
17
|
+
_response = { status: @_response.code }
|
18
|
+
begin
|
19
|
+
_response[:body] = JSON.parse(@_response.body)
|
20
|
+
rescue
|
21
|
+
_response[:body] = @_response.body
|
22
|
+
end
|
23
|
+
else
|
24
|
+
_response = { status: "error" }
|
25
|
+
_response[:body] = @_response
|
26
|
+
end
|
27
|
+
_response
|
28
|
+
end
|
29
|
+
|
30
|
+
def success?
|
31
|
+
@_success
|
32
|
+
end
|
33
|
+
|
34
|
+
def send_request(type = 'GET', url_param = false)
|
35
|
+
begin
|
36
|
+
ApiNotify::LOGGER.info "Request Started"
|
37
|
+
http = Net::HTTP.new(@config["domain"], @config["port"])
|
38
|
+
if @config["port"].to_i == 443
|
39
|
+
http.use_ssl = true
|
40
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
41
|
+
end
|
42
|
+
_url = url_param ? build_url(url_param) : url(type)
|
43
|
+
ApiNotify::LOGGER.info "Request url: #{_url}"
|
44
|
+
@_response = http.send_request(type, _url, params_query, headers)
|
45
|
+
@_success = true
|
46
|
+
ApiNotify::LOGGER.info "#{@_response.code}: #{ @_response.body.truncate(200, separator: "\n")}"
|
47
|
+
rescue Exception => e
|
48
|
+
@_response = {error: e}
|
49
|
+
ApiNotify::LOGGER.error @_response[:error]
|
50
|
+
end
|
51
|
+
@_response
|
52
|
+
end
|
53
|
+
|
54
|
+
def params_query
|
55
|
+
@_params.empty? ? "" : "#{@_params.to_query}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def headers
|
59
|
+
headers = {
|
60
|
+
"Content-type" => "application/x-www-form-urlencoded",
|
61
|
+
"Content-Length" => params_query.length.to_s,
|
62
|
+
"Api-Key" => @config["api_key"].to_s
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def url type
|
67
|
+
id = @_params[@id_param] && type!= "POST" ? "#{@_params[@id_param]}" : nil
|
68
|
+
build_url id
|
69
|
+
end
|
70
|
+
|
71
|
+
def build_url param
|
72
|
+
_url = param ? "/#{param}" : ""
|
73
|
+
"#{@config["base_path"]}/#{@route_name}#{_url}"
|
74
|
+
end
|
75
|
+
|
76
|
+
def set_params params
|
77
|
+
@_params = params
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
def load_config_yaml
|
82
|
+
config_yaml = "#{Rails.root.to_s}/config/api_notify.yml"
|
83
|
+
YAML.load_file(config_yaml)[Rails.env] if File.exists?(config_yaml)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ApiNotify
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer 'apinotify.initialize' do
|
4
|
+
require "api_notify/active_record/logger"
|
5
|
+
logfile = File.open("#{Rails.root}/log/api_notify.log", 'a')
|
6
|
+
logfile.sync = true
|
7
|
+
ApiNotify::LOGGER = ApiNotify::ActiveRecord::Logger.new(logfile)
|
8
|
+
|
9
|
+
ApiNotify::Hooks.init
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_notify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Edgars Kaukis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-20 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ApiNotify is an ActiveRecord extender. Based on model callbacks, api_notify
|
112
|
+
knows if it needs to send request.
|
113
|
+
email:
|
114
|
+
- e.kaukis@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- MIT-LICENSE
|
120
|
+
- README.rdoc
|
121
|
+
- Rakefile
|
122
|
+
- lib/api_notify.rb
|
123
|
+
- lib/api_notify/active_record/logger.rb
|
124
|
+
- lib/api_notify/active_record/main.rb
|
125
|
+
- lib/api_notify/active_record/synchronizer.rb
|
126
|
+
- lib/api_notify/engine.rb
|
127
|
+
- lib/api_notify/hooks.rb
|
128
|
+
- lib/api_notify/railtie.rb
|
129
|
+
- lib/api_notify/version.rb
|
130
|
+
- lib/tasks/api_notify_tasks.rake
|
131
|
+
homepage: https://github.com/ekaukis/api_notify
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.2.2
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: ApiNotify allows to comunicate between two systems via API
|
155
|
+
test_files: []
|