iron_consumer 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f9af7eb804da99016154d9169c111b92829de2b
4
+ data.tar.gz: 1ec09872e86d8df74b1612c5e3cd78f121413def
5
+ SHA512:
6
+ metadata.gz: c615c8dffad3bd7716152de307d3d61ec3c6b5195e0e0717caa00c2776adadd323643604441c2414210289e4f7efef61cf08d56877bae3b3b97f636df14dba73
7
+ data.tar.gz: b367f3eeda5a1116de2fe208839bceb3fb18f59ef00018a9ddbf78e3bd94016d7fc97e3e68ba6531f4ec3b13488c86b12878057923a2030a9eb643d963afbf85
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.iml
2
+ .idea/
3
+ iron.json
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ This is a special way to make a worker that runs for a while and pulls off IronMQ.
2
+
3
+ Benefits:
4
+
5
+ - no load times so faster and cheaper
6
+ - can run locally and remote in the same way
7
+ - HUD could have easy editor (usersetup and usercode text boxes)
8
+
9
+ Can use with scheduler or alerts.
10
+
11
+ ## Usage
12
+
13
+ First thing is to get things running locally.
14
+
15
+ To do that, make three files, `iron.json`, `usersetup.rb` and
16
+ `usercode.rb`. OR to make this all faster, go git clone this repository and just modify the files:
17
+ https://github.com/iron-io/iron_consumer_starter
18
+
19
+ ### iron.json
20
+
21
+ This is a normal Iron.io config file and should contain your project id and token (you can download this from
22
+ HUD if you want).
23
+
24
+ ```json
25
+ {
26
+ "token": "X",
27
+ "project_id": "Y"
28
+ }
29
+ ```
30
+
31
+ ### usersetup.rb
32
+
33
+ Put all your stuff in here to setup the worker such as making database connections, api clients, etc. Put them
34
+ into the special hash called `connections` (todo: should that have a more generic name?). For example:
35
+
36
+ ```ruby
37
+ require 'mongo'
38
+ require 'twilio-ruby'
39
+
40
+ db = Mongo::MongoClient.from_uri("mongodb://username:pass@x.mongolab.com:12345/tester")
41
+ # Put this into the special connections map
42
+ IronConsumer.connections['mymongo'] = db.db()
43
+ ```
44
+
45
+ ### usercode.rb
46
+
47
+ This is where you'll write the code that actually does the work. The message will be available with
48
+ `IronConsumer.message` and this will be a normal IronMQ message from the iron_mq gem.
49
+
50
+ Typically you'll parse the message body and then do your thing. For example:
51
+
52
+ ```ruby
53
+ puts "Got message"
54
+ puts IronConsumer.message.body
55
+
56
+ # transform body or generate something or do whatever
57
+ username = "Travis" # Would normally get this from database for user
58
+ body = IronConsumer.message.body.gsub("#NAME#", username)
59
+
60
+ # Now post this new stuff into the database.
61
+ IronConsumer.connections["mongo1"].collection('msgs').insert({'body' => body})
62
+
63
+ # And/or send email, post to social media, send notifications to mobile phone, etc.
64
+ ```
65
+
66
+ ## Running locally
67
+
68
+ Just create a simple file called `local.rb` and put this in it:
69
+
70
+ ```ruby
71
+ require 'iron_consumer'
72
+ IronConsumer.run('my_queue')
73
+ ```
74
+
75
+ Once you've got it working, just upload it to IronWorker.
76
+
77
+ ## Upload to IronWorker
78
+
79
+ Run `iron_worker upload consumer` to get your code onto the IronWorker system.
80
+
81
+ ## Now queue up messages from wherever.
82
+
83
+ ```ruby
84
+ require 'iron_mq'
85
+
86
+ mq = IronMQ::Client.new()
87
+ # Same queue name you are reading from
88
+ qname = "testq"
89
+ queue = mq.queue(qname)
90
+ msg = {
91
+ "mystring" => "Hello #NAME#!"
92
+ }
93
+ queue.post(msg.to_json)
94
+ ```
95
+
96
+
97
+
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../lib/iron_consumer/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Travis Reeder"]
5
+ gem.email = ["travis@iron.io"]
6
+ gem.description = "Ruby client for IronWorker by www.iron.io"
7
+ gem.summary = "Ruby client for IronWorker by www.iron.io"
8
+ gem.homepage = "https://github.com/iron-io/iron_consumer"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "iron_consumer"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = IronConsumer::VERSION
16
+
17
+ gem.required_rubygems_version = ">= 1.3.6"
18
+ gem.required_ruby_version = Gem::Requirement.new(">= 1.9")
19
+ gem.add_runtime_dependency "iron_mq", ">= 4.0.0"
20
+
21
+ gem.add_development_dependency "test-unit"
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "rake"
24
+
25
+ end
26
+
data/lib/helpers.rb ADDED
@@ -0,0 +1,17 @@
1
+ # hash for user to stuff things to use later.
2
+ module IronConsumer
3
+ @@connections = {}
4
+
5
+ def self.connections
6
+ return @@connections
7
+ end
8
+
9
+ def self.set_message(m)
10
+ @@message = m
11
+ end
12
+
13
+ def self.message
14
+ @@message
15
+ end
16
+
17
+ end
@@ -0,0 +1,57 @@
1
+ # hard coding the stuff here that would normally be generate from initial dependency and connections setup.
2
+
3
+ require 'iron_mq'
4
+
5
+ require_relative 'helpers'
6
+
7
+ module IronConsumer
8
+
9
+ def self.run(queue_name)
10
+ qname = queue_name
11
+
12
+ # User can have whatever he wants in the usersetup part.
13
+ # We can put the helpers stuff in a gem that user can optionally use or something.
14
+
15
+ begin
16
+ load "usersetup.rb"
17
+ rescue LoadError => ex
18
+ p ex
19
+ puts ex.message
20
+ puts "You need a file called usersetup.rb."
21
+ exit false
22
+ end
23
+
24
+ # mongo1 name is defined by user
25
+ # Other connections would be made here
26
+
27
+ # Iron stuff is setup too:
28
+ mq = IronMQ::Client.new()
29
+
30
+ # qname defined by user too
31
+ queue = mq.queue(qname)
32
+ p mq.project_id
33
+
34
+ # This worker kicks off whenever a message gets on a queue (only once, needs alerts)
35
+ while msg = queue.get
36
+ IronConsumer.set_message(msg)
37
+ begin
38
+
39
+ # USER CODE STUFFED IN HERE
40
+ begin
41
+ load "usercode.rb"
42
+ rescue LoadError => ex
43
+ p ex
44
+ puts "You need a file called usercode.rb."
45
+ exit false
46
+ end
47
+ # USER CODE END
48
+
49
+ # If finished ok, delete message
50
+ msg.delete
51
+
52
+ rescue Exception => ex
53
+ puts "Error in user code #{ex} -- #{ex.message}"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ module IronConsumer
2
+ VERSION = "0.0.1"
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron_consumer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Travis Reeder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: iron_mq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit
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: minitest
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
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruby client for IronWorker by www.iron.io
70
+ email:
71
+ - travis@iron.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - README.md
78
+ - iron_consumer.gemspec
79
+ - lib/helpers.rb
80
+ - lib/iron_consumer.rb
81
+ - lib/iron_consumer/version.rb
82
+ homepage: https://github.com/iron-io/iron_consumer
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '1.9'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: 1.3.6
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Ruby client for IronWorker by www.iron.io
105
+ test_files: []