Sha256: bcd3de1877c1065b259bf202fe120738f305c388a88596a0787d9cb26faa4bcb
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
require 'curb' require 'json' require 'gnip-rule/rule' module GnipRule class Client attr_reader :url, :username, :password def initialize(url, username, password) @url = url.gsub(/\.xml$/, '.json') @username = username @password = password end def add(value, tag=nil) post(@url, jsonify_rules(value, tag)) end def delete(value, tag=nil) post("#{@url}?_method=delete", jsonify_rules(value, tag)) end def list() rules = nil Curl::Easy.http_get(@url) do |curl| curl.http_auth_types = :basic curl.username = @username curl.password = @password curl.on_body do |obj| rules = JSON.parse(obj)['rules'].collect { |o| Rule.new(o['value'], o['tag']) } obj.size end end rules end def jsonify_rules(values, tag=nil) rules = nil if values.instance_of?(Array) rules = values.collect { |o| o.instance_of?(Rule) ? o : Rule.new(o, tag) } elsif values.instance_of?(Rule) rules = [values] else rules = [Rule.new(values, tag)] end {:rules => rules.collect(&:as_hash)}.to_json end protected def post(url, data) Curl::Easy.http_post(url, data) do |curl| curl.http_auth_types = :basic curl.username = @username curl.password = @password curl.on_complete do |res| if res.response_code >= 400 raise "Got #{res.response_code}; body: #{res.body_str}" end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gnip-rule-0.1.1 | lib/gnip-rule/client.rb |