ATrigger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2d620fd1766af5497ac80939067544c76d49223
4
+ data.tar.gz: 82ded13c7a085771c710d64affbf12a76b73cdcf
5
+ SHA512:
6
+ metadata.gz: 6f929ac4c7ead4ae60391da7f37bb7418dc36c4e9956988cff1c1644e24a8f2910d023e53c1d72704be46de96ea227622270ce397fec46badcf9da78f948c729
7
+ data.tar.gz: 83854ebeb5c910261a2dba0566adf284927e291cab8cd17ca1b6a64963e554392a6f633d628252db21cd0f2e72190ec9b22647e21896242d806f305ba2b0caf7
@@ -0,0 +1,17 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'ATrigger'
7
+ s.version = ATrigger::VERSION
8
+ s.files = Dir.glob('**/*')
9
+ s.require_paths = ['lib']
10
+ s.date = '2013-08-16'
11
+ s.summary = "ATrigger library for Ruby"
12
+ s.description = "See http://atrigger.com for more info"
13
+ s.authors = ["ATrigger Team"]
14
+ s.email = 'hi@atrigger.com'
15
+ s.homepage = 'http://atrigger.com'
16
+ s.license = 'MIT'
17
+ end
@@ -0,0 +1,44 @@
1
+
2
+ require_relative "ClientKernel"
3
+
4
+ module ATrigger
5
+ module ClassMethods
6
+ # By default use a single client for the module
7
+ def init(options = {})
8
+ @client = ATrigger::Client.new(options)
9
+ end
10
+
11
+ def doCreate(options)
12
+ return false unless @client
13
+ @client.doCreate(options)
14
+ end
15
+
16
+ def doDelete(_tags)
17
+ return false unless @client
18
+ @client.doDelete(_tags)
19
+ end
20
+
21
+ def doPause(_tags)
22
+ return false unless @client
23
+ @client.doPause(_tags)
24
+ end
25
+
26
+ def doResume(_tags)
27
+ return false unless @client
28
+ @client.doResume(_tags)
29
+ end
30
+ def doGet(_tags)
31
+ return false unless @client
32
+ @client.doGet(_tags)
33
+ end
34
+ def errorsCount
35
+ return false unless @client
36
+ @client.errorsCount
37
+ end
38
+ def verifyRequest(_requestIP)
39
+ return false unless @client
40
+ @client.verifyRequest(_requestIP)
41
+ end
42
+ end
43
+ extend ClassMethods
44
+ end
@@ -0,0 +1,217 @@
1
+
2
+ require 'time'
3
+ require 'thread'
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'erb'
7
+
8
+ module ATrigger
9
+
10
+ class Client
11
+ @@_isInited = false;
12
+ @@_key;
13
+ @@_secret;
14
+ @@_apiServer = "api.atrigger.com"
15
+ @@_apiTimeout = 5
16
+ @@_debug = false
17
+ @@_async = true
18
+ @@_errors = 0
19
+ @@_ssl = true #You are allowed to change this value if your host doesn't support php_openssl
20
+
21
+
22
+ def initialize(options = {})
23
+ symbolize_keys!(options)
24
+
25
+ @@_key = options[:key]
26
+ @@_secret = options[:secret]
27
+ @@_apiServer = options[:apiServer] || @@_apiServer
28
+ @@_apiTimeout = options[:apiTimeout] || @@_apiTimeout
29
+ @@_debug = options[:debug] if options[:debug].is_a?(TrueClass) or options[:debug].is_a?(FalseClass)
30
+ @@_async = options[:async] if options[:async].is_a?(TrueClass) or options[:async].is_a?(FalseClass)
31
+
32
+ check_secret
33
+ end
34
+
35
+
36
+ def doCreate(options)
37
+
38
+ check_secret
39
+
40
+ symbolize_keys!(options)
41
+
42
+ _timeSlice = CGI::escape(options[:timeSlice])
43
+ _url = CGI::escape(options[:url])
44
+ _tags = options[:tags] #http://www.ruby-doc.org/core-1.9.3/Hash.html
45
+ _first = toISO8601(options[:first]) || ""
46
+ _count = options[:count] || 1
47
+ _retries = options[:retries] || -1
48
+ _postData = options[:postData] || Hash.new() #http://www.ruby-doc.org/core-1.9.3/Hash.html
49
+
50
+ _first = CGI::escape(_first.to_s);
51
+
52
+ _tagsRaw = ""
53
+ if not _tags.nil?
54
+ _tagsRaw = dictionary2string(_tags, "tag_")
55
+ end
56
+
57
+
58
+ if _timeSlice.nil? || _timeSlice.empty?
59
+ @@_errors += 1
60
+ fail ArgumentError, 'Must supply timeSlice as a non-empty string'
61
+ end
62
+ if _url.nil? || _url.empty?
63
+ @@_errors += 1
64
+ fail ArgumentError, 'Must supply url as a non-empty string'
65
+ end
66
+
67
+
68
+ _urlQueries = "timeSlice=#{_timeSlice}&url=#{_url}&first=#{_first}&count=#{_count}&retries=#{_retries}&#{_tagsRaw}"
69
+
70
+ callATrigger('tasks/create', _urlQueries , _postData)
71
+ end
72
+
73
+
74
+ def doDelete(_tags)
75
+ _out = actionUsingTags("tasks/delete", _tags)
76
+ _out
77
+ end
78
+
79
+
80
+ def doPause(_tags)
81
+ _out = actionUsingTags("tasks/pause", _tags)
82
+ _out
83
+ end
84
+
85
+
86
+ def doResume(_tags)
87
+ _out = actionUsingTags("tasks/resume", _tags)
88
+ _out
89
+ end
90
+
91
+ def doGet(_tags)
92
+ _out = actionUsingTags("tasks/get", _tags)
93
+ _out
94
+ end
95
+
96
+ def errorsCount
97
+ @@_errors
98
+ end
99
+
100
+ def verifyRequest(_requestIP)
101
+ _out = ""
102
+ begin
103
+ _out = httpRequest("/v1/ipverify?ip=#{ERB::Util.url_encode(_requestIP)}", "")
104
+ return true if _out.to_s.include?("""OK""")
105
+ rescue Exception => err
106
+ if err.to_s.include?("""ERROR""") and err.to_s.include?("""INVALID IP ADDRESS""")
107
+ return false
108
+ else
109
+ raise err if @@_debug
110
+ end
111
+ end
112
+ false
113
+ end
114
+
115
+ private
116
+
117
+ def actionUsingTags(_urlType, _tags)
118
+ check_secret
119
+ begin
120
+ #Creating tags query array
121
+ _urlQueries = dictionary2string(_tags, 'tag_')
122
+ _out = callATrigger(_urlType, _urlQueries , nil)
123
+ _out
124
+ rescue Exception => err
125
+ @@_errors += 1
126
+ fail ArgumentError, "ATrigger: actionUsingTags, #{err}" if @@_debug
127
+ end
128
+ end
129
+
130
+
131
+ def callATrigger(_urlType, _urlQueries, _postData)
132
+ _queries = "?key=#{ERB::Util.url_encode(@@_key)}&secret=#{ERB::Util.url_encode(@@_secret)}&" + _urlQueries
133
+ _payload = dictionary2string(_postData)
134
+ _apiURL = "/v1/" + _urlType + _queries
135
+
136
+
137
+ _out = ""
138
+ if @@_async == true
139
+ Thread.new httpRequest(_apiURL, _payload)
140
+ _out = 'Disable async requests to get success result /or/ error details.'
141
+ else
142
+ _out = httpRequest(_apiURL, _payload)
143
+ end
144
+ _out
145
+ end
146
+
147
+
148
+ def httpRequest(_apiURL, _payload)
149
+ _out = ""
150
+ request = Net::HTTP::Post.new(_apiURL, initheader = {'Content-Type' =>'application/x-www-form-urlencoded'})
151
+ request.body = _payload
152
+
153
+ response = Net::HTTP.new(@@_apiServer, 443)
154
+ response.use_ssl = true
155
+ response.verify_mode = OpenSSL::SSL::VERIFY_NONE
156
+ response.open_timeout = @@_apiTimeout
157
+ response.read_timeout = @@_apiTimeout
158
+ response = response.start { |http| http.request(request) }
159
+
160
+ #puts _apiURL
161
+
162
+ if response.code == '200'
163
+ _out = response.body
164
+ raise "A Trigger error responsed: #{response.body}" if _out.to_s.include?("""ERROR""") and @@_debug
165
+ else
166
+ @@_errors += 1
167
+ raise "A Trigger HTTP result error: #{response.code} #{response.message}: #{response.body}" if @@_debug
168
+ end
169
+
170
+ _out
171
+ end
172
+
173
+ def toISO8601(inDate)
174
+ _out = inDate
175
+ if inDate.to_s.include?(" ") or inDate.to_s.include?("/")
176
+ #Make ISO format
177
+ _out = DateTime.parse(inDate).iso8601
178
+ end
179
+ _out
180
+ end
181
+
182
+
183
+ def dictionary2string(inHash, inPreKey = "")
184
+ _out = ""
185
+ if not inHash.nil?
186
+ inHash.each do |key, value|
187
+ key = ERB::Util.url_encode(key)
188
+ value = ERB::Util.url_encode(value)
189
+ _out = _out + "#{inPreKey}#{key}=#{value}&"
190
+ end
191
+ _out = _out.chomp('&')
192
+ end
193
+ _out
194
+ end
195
+
196
+
197
+ # private: Checks that the secret is properly initialized
198
+ def check_secret
199
+ fail 'Key/Secret must be initialized' if @@_secret.nil?
200
+ end
201
+
202
+
203
+
204
+ def symbolize_keys(hash)
205
+ hash.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
206
+ end
207
+
208
+ def symbolize_keys!(hash)
209
+ hash.replace symbolize_keys(hash)
210
+ end
211
+
212
+ def stringify_keys(hash)
213
+ hash.inject({}) { |memo, (k,v)| memo[k.to_s] = v; memo }
214
+ end
215
+
216
+ end
217
+ end
@@ -0,0 +1,3 @@
1
+ module ATrigger
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ATrigger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ATrigger Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: See http://atrigger.com for more info
14
+ email: hi@atrigger.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ATrigger.gemspac
20
+ - lib/ATrigger.rb
21
+ - lib/ClientKernel.rb
22
+ - lib/version.rb
23
+ homepage: http://atrigger.com
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: ATrigger library for Ruby
47
+ test_files: []