Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Created April 8, 2013 12:30
Show Gist options
  • Select an option

  • Save stefanoverna/5336438 to your computer and use it in GitHub Desktop.

Select an option

Save stefanoverna/5336438 to your computer and use it in GitHub Desktop.
require 'rest-client'
require 'logger'
require 'json'
require 'map'
class CrimsonMonitor
include ValueObject
attribute :monitor_id
attribute :email
attribute :password
attribute :rate_limit_per_hour
module Type
BLOG = 'Blogs'
FORUM = 'Forums'
NEWS = 'News'
FACEBOOK = 'Facebook'
TWITTER = 'Twitter'
COMMENT = 'Comments'
FLICKR = 'Flickr'
YOUTUBE = 'YouTube'
CUSTOM = 'Custom'
def self.values
[ BLOG, FORUM, NEWS, FACEBOOK, TWITTER, COMMENT, FLICKR, YOUTUBE, CUSTOM ]
end
end
class Post
include ValueObject
attribute :posted_at
attribute :type
attribute :url
attribute :author
attribute :title
attribute :content
attribute :language
attribute :category_id
attribute :category_name
attribute :score
end
def start_date
Date.parse get("/monitor/detail")[:resultsStart]
end
def posts_count(date)
monitor_get(
"/monitor/results",
start: date.strftime("%Y-%m-%d"),
end: (date + 1).strftime("%Y-%m-%d")
)[:results].first[:numberOfRelevantDocuments].to_i
end
def sources(date)
monitor_get(
"/monitor/sources",
start: date.strftime("%Y-%m-%d"),
end: (date + 1).strftime("%Y-%m-%d")
)
end
def posts(date, category_acceptance_threshold = 0)
monitor_get(
"/monitor/posts",
start: date.strftime("%Y-%m-%d"),
end: (date + 1).strftime("%Y-%m-%d")
)[:posts].map do |p|
max_cat = p[:categoryScores].select do |cat|
cat[:score].to_f >= category_acceptance_threshold
end.max_by do |cat|
cat[:score].to_f
end
Post.new(
posted_at: Date.parse(p[:date]),
type: p[:type],
url: p[:url],
author: p[:author],
title: p[:title],
content: p[:contents],
language: p[:language],
category_id: (max_cat ? max_cat[:categoryId].to_s : nil),
category_name: (max_cat ? max_cat[:categoryName] : nil),
score: (max_cat ? max_cat[:score].to_f : nil)
)
end
end
def get(url, params = {})
if rate_limit_per_hour
sleep 3600 / rate_limit_per_hour
end
params.merge!(version: 3)
query = "?" + params.inject([]) { |s, (k, v)| s << "#{k}=#{v}" }.join("&")
Map.new JSON.parse client[url+query].get(accept: :json)
end
def monitor_get(url, params = {})
get(url, params.merge!(id: monitor_id, auth: auth_key))
end
def auth_key
get('/authenticate', username: email, password: password)[:auth]
end
def client
@client ||= RestClient::Resource.new('https://forsight.crimsonhexagon.com/api')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment