Skip to content

Instantly share code, notes, and snippets.

@vanpelt
Created June 3, 2009 18:45
Show Gist options
  • Save vanpelt/123167 to your computer and use it in GitHub Desktop.
Save vanpelt/123167 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'httparty'
module HTTParty
class Request
def handle_response(response)
case response
when Net::HTTPRedirection
options[:limit] -= 1
self.path = response['location']
@redirect = true
@message = response.body.to_s
perform
else
parsed_response = parse_response(response.body.to_s) rescue puts(response.body.to_s)
Response.new(parsed_response, response.body.to_s, response.code, @message || response.message, response.to_hash)
end
end
end
end
class CrowdSifter
include HTTParty
format :json
def initialize(company, site_id, token)
@company = company
@id = site_id
@token = token
self.class.base_uri "https://#{company}.crowdsifter.com/api"
self.class.default_params({:token => token})
end
def moderate(urls, opts = {})
opts[:type] ||= :img
raise ArgumentError, "#{opts[:type].inspect} is a type not currently supported" unless [:img].include?(opts[:type])
self.class.post "/#{opts[:type]}s/#{opts[:site_id] || @id}.json", :body => urls.is_a?(Array) ? {:urls => urls.join(",")} : {:url => urls}
end
def img(what = {})
raise ArgumentError, "A hash with the key of :img_id or :url must be used to retrieve an individual image" if what && !what[:img_id] && !what[:url]
self.class.get "/imgs/#{what[:site_id] || @id}.json", :query => what
end
def imgs(format = :csv, site_id = nil)
raise ArgumentError, "Format must be one of :json, :xml, or :csv" unless [:json, :xml, :csv].include?(format)
self.class.get "/imgs/#{site_id || @id}.#{format}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment