eriwen/gnip-rule

View on GitHub
lib/gnip-rule/client.rb

Summary

Maintainability
A
25 mins
Test Coverage
require 'rest-client'
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(input, tag=nil)
      post(@url, gen_json_payload(input, tag))
    end

    def delete(input, tag=nil)
      post("#@url?_method=delete", gen_json_payload(input, tag))
    end

    def list()
      response = RestClient::Request.new(:method => :get, :url => @url,
                                         :user => @username, :password => @password,
                                         :headers => { :accept => :json }).execute
      JSON.parse(response.to_s)['rules'].collect { |o| Rule.new(o['value'], o['tag']) }
    end

    def gen_json_payload(input, tag=nil)
      input = [input] unless input.respond_to? :collect
      rules = input.map { |i|
        if i.respond_to?(:to_rule)
          i.to_rule(tag)
        elsif i.is_a?(String)
          GnipRule::Rule.new(i, tag)
        else
          raise 'Input must be convertable to GnipRule::Rule'
        end
      }
      { rules: rules.map(&:to_hash) }.to_json
    end

    protected
    def post(url, data)
      RestClient::Request.new(:method => :post, :url => url, :payload => data,
          :user => @username, :password => @password, :headers => { :content_type => :json, :accept => :json }).execute
    end
  end
end