Skip to content

Instantly share code, notes, and snippets.

@webcracy
Created November 25, 2011 14:43
Show Gist options
  • Save webcracy/1393676 to your computer and use it in GitHub Desktop.
Save webcracy/1393676 to your computer and use it in GitHub Desktop.
Manybots API - Auth Token example
# encoding: UTF-8
require 'rubygems'
require 'json/add/core'
require 'httparty'
TOKEN = 'YOUR API TOKEN' #find it in your /account page
module Manybots
class Client
include HTTParty
base_uri 'https://www.manybots.com'
def initialize(t)
@auth_token = t
end
def list_activities(filters = {}, page = 1, per_page = 10)
options = {
:query => {
:auth_token => @auth_token,
:filter => filters,
:page => page,
:per_page => per_page
}
}
self.class.get('/activities.json', options)
end
def filter_activities(filters = {}, page = 1, per_page = 10)
options = {
:body => {
:auth_token => @auth_token,
:filter => filters,
:page => page,
:per_page => per_page
}
}
self.class.post('/activities/filter.json', options)
end
def bundle_activities(filters = [], page = 1, per_page = 10)
options = {
:body => {
:auth_token => @auth_token,
:filters => filters.to_a,
:page => page,
:per_page => per_page
}
}
self.class.post('/activities/bundle.json', options)
end
def test_array(filters = [])
options = {
:body => {
:auth_token => @auth_token,
:filters => [{:oi => 'ola'}, {:tudo_bem =>'tudo bom'}]
}
}
self.class.post('/activities/test_array.json', options)
end
def create_activity(activity = {})
options = {
:body => {
:auth_token => @auth_token,
:version => '1.0',
:activity => activity
}
}
self.class.post('/activities.json', options)
end
end
end
#
#
# A simple activity filter - a list of 50 most recent commits tagged with work
#
#
activities = Manybots::Client.new(TOKEN).filter_activities({
:verbs => '',
:objects => 'commit',
:targets => '',
:target_values => '',
:tags => ['work']
}, 1, 50)
puts activities['data']['items'].map{|activity| activity['target']['displayName'].inspect }
#
#
# A bundle of filters - the 200 most recent phone calls with Bill Gates, Steve Jobs and Larry Elisson
# It's very important that all hashes contain the same elements
# set the value to nil or '' if necessary
#
#
activities = Manybots::Client.new(TOKEN).bundle_activities([
{
:verbs => '',
:objects => 'phone-call',
:targets => '',
:target_values => 'Bill Gates',
:tags => []
},
{
:verbs => '',
:objects => 'phone-call',
:targets => '',
:target_values => 'Steve Jobs',
:tags => []
},
{
:verbs => '',
:objects => 'phone-call',
:targets => 'person',
:target_values => 'Larry Ellison',
:tags => []
},
], 1, 200)
puts activities['data']['items'].map{|activity|
Time.at(activity['published_epoch']).strftime('%d %b %Y') + ' ' +
activity['object']['displayName'] + ' ' +
activity['target']['displayName']
}
#
#
# Creating a new activity
# The commented portions are optional.
# :published will default to Manybots server's Time.now
#
#
new_activity = Manybots::Client.new(TOKEN).create_activity({
# :id => 'http://somesite.com/activities/1',
# :url => 'http://somesite.com/1',
# :published => Time.now,
# :actor => {
# :id => 'http://webcracy.org',
# :url => 'http://webcracy.org',
# :displayName => 'Alex Solleiro',
# },
:auto_title => true,
:title => 'ACTOR demoed OBJECT to TARGET',
:summary => '',
:content => '',
:verb => 'demo',
:object => {
:objectType => 'script',
:id => 'http://ruby-lang.org',
:url => 'http://ruby-lang.org',
:displayName => 'Manybots API Demo Script'
},
:target => {
:objectType => 'person',
:id => 'http://nikoroberts.com',
:url => 'http://nikoroberts.com',
:displayName => 'Nikó Roberts'
},
:tags => ['test', 'code', 'ruby', 'api'],
:icon => '',
:generator => {
:url => 'http://myscript.com',
:id => 'http://myscript.com',
:displayName => 'My Script',
:image => {
:url => ''
}
},
:provider => {
:url => 'http://myscript.com',
:id => 'http://myscript.com',
:displayName => 'My Script',
:image => {
:url => ''
}
}
})
puts new_activity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment