Not logged in - Login
< back

Library: Ruby

We have made a library for PHPRuby to help you start using A Trigger in a minute. This PHPRuby library is made to avoid possible errors in your application. Even if ATrigger.com API server become unavailable, your application will work without any problem. On the other hand, all requests are Async by default and no slowness will happen.


Prerequisites

  1. First you need to follow Quick Start Guide.
  2. You have this personal details from your Account Setup page:
    1. API Key
    2. API Secret


Step 1: Initialize

Add ATrigger to your project's Gemfile:

gem 'ATrigger'
#gem install ATrigger

You can create separate ATrigger clients, but the easiest and recommended way is to just use the module. If you're using Rails, put this in config/initializers/ATrigger.rb.

Analytics.init({ key: "YOUR_API_KEY", secret: "YOUR_API_Secret" })
#Debug-friendly: ATrigger::init({key: "YOUR_API_KEY", secret: "YOUR_API_Secret", async: false, debug: true})

In development you might want to use development settings above to start debugging. Also you can require 'ATrigger' from your application code and then run the ATrigger::init method. You only need to initialize the module once. Whenever you require ATrigger from another file in your app you'll have access to the same Analytics instance.


Unicorn:

If you're using unicorn in production to run multiple ruby processes, you'll need to initialize the module in the post-fork section of your unicorn.rb config file.

# unicorn.rb

before_fork do |server, worker|
  [...]
end

after_fork do |server, worker|
  [...]
  defined?(Analytics) and Analytics.init({ key: "YOUR_API_KEY", secret: "YOUR_API_Secret" })
end

#Debug-friendly: ATrigger::init({key: "YOUR_API_KEY", secret: "YOUR_API_Secret", async: false, debug: true})


Passenger:

If you're using passenger in production to run multiple worker processes, you'll need to initialize the analytics module in your environment.rb when those workers are initiated.

# config/environment.rb

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|

    if forked # We're in smart spawning mode.
      Analytics.init({ key: "YOUR_API_KEY", secret: "YOUR_API_Secret" })
      #Debug-friendly: ATrigger::init({key: "YOUR_API_KEY", secret: "YOUR_API_Secret", async: false, debug: true})
    else
      # We're in direct spawning mode. We don't need to do anything.
    end
  end
end


Step 2: Start Using, Functions

Create

#Tags: Tags are required to identify tasks. 
#read more at: http://atrigger.com/docs/wiki/9/rest-api-v10-parameter-tag_
tags = Hash["type", "test", "key2", "value2"]

#Create
ATrigger.doCreate({timeSlice: "3month", url: "http://www.example.com/myRubyTask?abc", tags: tags })


  1. Required timeSlice: Xminute, Xhour, Xday, Xmonth, Xyear - more info
  2. Required url: The target url that A Trigger will call it at defined TimeSlice. The library will automatically encode this url. Don't use Server.URLEncode()
  3. Required tags: You need tag your tasks for future identification and to control them using API in the future. read more at tags_* parameters.
  4. [Optional] retries: How many times should try if your server failed(or it was down)? default value: 3
  5. [Optional] count: How many cycles should be repeated? read more at count parameter.
  6. [Optional] first: When should be the first call? You are not required to set time value by default. read more at first parameter.
  7. [Optional] postData: The hashtable for the content you want to HTTP POST to your URL when your task has been triggered.


More advanced example: This code will call your task URL at 1/1/2015. No repeats, no cycles. It will retries 5 times if your server be down.

tags = Hash["type", "test", "key2", "value2"]

postData = Hash["key1", "value1", "key2", "value2"]

firstDate = Date.new(2015,1,1);

ATrigger.doCreate({timeSlice: "0minute", count:0, retries: 5, url: "http://www.mousestats.com/myRubyTask?firstDate", first: firstDate, tags: tags, postData: postData }) 


Delete

#tags
tags = Hash["type", "test"]

#Delete
ATrigger.doDelete(tags)

Attention: This line will remove the task you've created in the last function(That is tagged "type=test").


  1. Required tags: All tasks with this tags will be removed. read more at tags_* parameters.


Pause

#tags
tags = Hash["type", "test"]

#Delete
ATrigger.doPause(tags)


  1. Required tags: All tasks with this tags will be paused. read more at tags_* parameters.


Resume

#tags
tags = Hash["type", "test"]

#Delete
ATrigger.doResume(tags)


  1. Required tags: All tasks with this tags will be resumed. read more at tags_* parameters.


Verify Request

All previous functions was for controlling tasks. but verifyRequest will be used to verify URL calls when you want to do a task to make sure the request is made from ATrigger servers, using the request IP and verifyRequest function:

if ATrigger.verifyRequest("127.0.0.1")
     #Invalid request: 
     #The request is not called from ATrigger servers.
else
     #Valid request:
     #Do the task here
end


  1. Required ip: The request IP that you want to check if it is an IP of ATrigger.


More

How Pause and Resume works?