diff --git a/Gemfile.lock b/Gemfile.lock index d7bf5d1..c8db5ae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,40 +1,40 @@ GEM remote: http://rubygems.org/ specs: - faraday (0.8.1) - multipart-post (~> 1.1) - iron_core (0.3.3) - rest (>= 2.0.0) - iron_mq (2.1.0) - iron_core (>= 0.2.0) - iron_worker_ng (0.9.6) - bundler (> 1.0.0) - iron_core (>= 0.3.0) - zip - mime-types (1.19) - multi_json (1.3.6) - multipart-post (1.1.5) - net-http-persistent (2.7) - rack (1.4.1) - rack-protection (1.2.0) + faraday (0.8.8) + multipart-post (~> 1.2.0) + iron_core (1.0.1) + rest (>= 2.2.0) + iron_mq (4.0.3) + iron_core (>= 0.5.1) + iron_worker_ng (1.0.2) + bundler (>= 1.2.0) + iron_core (>= 1.0.0) + rubyzip (= 0.9.9) + mime-types (1.25) + multi_json (1.8.1) + multipart-post (1.2.0) + net-http-persistent (2.9) + rack (1.5.2) + rack-protection (1.5.0) rack - rest (2.0.2) + rest (2.6.3) net-http-persistent rest-client (>= 0.3.0) rest-client (1.6.7) mime-types (>= 1.16) - simple_oauth (0.1.9) - sinatra (1.3.2) - rack (~> 1.3, >= 1.3.6) - rack-protection (~> 1.2) - tilt (~> 1.3, >= 1.3.3) - tilt (1.3.3) - twitter (3.4.1) - faraday (~> 0.8) - multi_json (~> 1.3) - simple_oauth (~> 0.1.6) - uber_config (0.0.6) - zip (2.0.2) + rubyzip (0.9.9) + simple_oauth (0.2.0) + sinatra (1.4.3) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (~> 1.3, >= 1.3.4) + tilt (1.4.1) + twitter (4.8.1) + faraday (~> 0.8, < 0.10) + multi_json (~> 1.0) + simple_oauth (~> 0.2) + uber_config (1.1.0) PLATFORMS ruby diff --git a/README.md b/README.md index c0f3435..b13ba30 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ And put them in a file in this directory called `iron.json` in this format: ```javascript { - "project_id":"PROJECT_ID", - "token":"TOKEN" + "project_id": "PROJECT_ID", + "token": "TOKEN" } ``` @@ -39,6 +39,26 @@ Upload the worker: iron_worker upload workers/TweetWorker + +Now, open and fill configuration file named `config.json`: + +```javascript +{ + // This section is optional, environment variables have higher priority + "iron": { + "project_id": "PROJECT_ID", + "token": "TOKEN" + }, + // Twitter API requires authorization since v1.1 + "twitter": { + "consumer_key": "YOUR_CONSUMER_KEY", + "consumer_secret": "YOUR_CONSUMER_SECRET", + "oauth_token": "YOUR_OAUTH_TOKEN", + "oauth_token_secret": "YOUR_OAUTH_TOKEN_SECRET" + } +} +``` + Then just push to heroku! git push heroku master @@ -56,6 +76,6 @@ to get it. Then start this sinatra app using this command, replacing my_token and my_project_id with your credentials: - IRON_WORKER_TOKEN=my_token IRON_WORKER_PROJECT_ID=my_project_id rackup -p 3000 config.ru + IRON_IO_TOKEN=my_token IRON_IO_PROJECT_ID=my_project_id rackup -p 3000 config.ru diff --git a/config.json b/config.json new file mode 100644 index 0000000..cca5a95 --- /dev/null +++ b/config.json @@ -0,0 +1,12 @@ +{ + "iron": { + "token": null, + "project_id": null + }, + "twitter": { + "consumer_key": "YOUR_CONSUMER_KEY", + "consumer_secret": "YOUR_CONSUMER_SECRET", + "oauth_token": "YOUR_OAUTH_TOKEN", + "oauth_token_secret": "YOUR_OAUTH_TOKEN_SECRET" + } +} diff --git a/config.ru b/config.ru index 6013c3b..64ba042 100644 --- a/config.ru +++ b/config.ru @@ -12,20 +12,35 @@ rescue => ex puts "Swallowed error: #{ex.message}" end @config = {} unless @config -@config["iron_worker"] ||= {} -@config["iron_worker"]["token"] ||= ENV['IRON_WORKER_TOKEN'] -@config["iron_worker"]["project_id"] ||= ENV['IRON_WORKER_PROJECT_ID'] -@config["iron_mq"] ||= {} -@config["iron_mq"]["token"] ||= ENV['IRON_MQ_TOKEN'] -@config["iron_mq"]["project_id"] ||= ENV['IRON_MQ_PROJECT_ID'] + +@config["iron_worker"] ||= @config["iron"] || {} +iw_conf_heroku = { + 'token' => ENV['IRON_WORKER_TOKEN'] || ENV['IRON_IO_TOKEN'], + 'project_id' => ENV['IRON_WORKER_PROJECT_ID'] || ENV['IRON_IO_PROJECT_ID'] +} +@config["iron_worker"].merge!(iw_conf_heroku) do |k, v1, v2| + (v2.nil? || v2.empty?) ? v1 : v2 +end + +@config["iron_mq"] ||= @config["iron"] || {} +imq_conf_heroku = { + 'token' => ENV['IRON_MQ_TOKEN'] || ENV['IRON_IO_TOKEN'], + 'project_id' => ENV['IRON_MQ_PROJECT_ID'] || ENV['IRON_IO_PROJECT_ID'] +} +@config["iron_mq"].merge!(imq_conf_heroku) do |k, v1, v2| + (v2.nil? || v2.empty?) ? v1 : v2 +end + p @config -set :iron_worker, IronWorkerNG::Client.new(:token=>@config["iron_worker"]["token"], :project_id=>@config["iron_worker"]["project_id"]) +set :iron_worker, IronWorkerNG::Client.new(@config["iron_worker"]) set :queue_name, "tweets" -ironmq = IronMQ::Client.new(:token => @config["iron_mq"]["token"], :project_id => @config["iron_mq"]["project_id"]) +ironmq = IronMQ::Client.new(@config["iron_mq"]) #ironmq.logger.level = Logger::DEBUG set :ironmq, ironmq +set :twitter_conf, @config['twitter'] + require './hello' run Sinatra::Application diff --git a/hello.rb b/hello.rb index ec7ec73..1599e41 100644 --- a/hello.rb +++ b/hello.rb @@ -13,7 +13,10 @@ ## todo: store worker id in session then ajax show progress #worker.queue - task = settings.iron_worker.tasks.create("TweetWorker", {:mq=>settings.ironmq.options.merge({:queue_name =>settings.queue_name})}) + task = settings.iron_worker.tasks.create("TweetWorker", + { :mq => settings.ironmq.options, + :queue_name => settings.queue_name, + :twitter => settings.twitter_conf }) session[:worker_id] = task.id puts 'worker_id in session=' + task.id diff --git a/views/hello.erb b/views/hello.erb index 9ec352d..943b05a 100644 --- a/views/hello.erb +++ b/views/hello.erb @@ -135,38 +135,33 @@ -