Sha256: a235492d9ea55705130ca5f87ebc383184d98b91fddb5f28689fad0a463191c3

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'http'

module Cellect
  module Client
    class Connection
      include Celluloid
      include Celluloid::IO
      
      def reload_project(id)
        broadcast :post, "/projects/#{ id }/reload"
      end
      
      def delete_project(id)
        broadcast :delete, "/projects/#{ id }"
      end
      
      def add_subject(id, project_id: project_id, group_id: nil, priority: nil)
        broadcast :put, "/projects/#{ project_id }/add", querystring(subject_id: id, group_id: group_id, priority: priority)
      end
      
      def remove_subject(id, project_id: project_id, group_id: nil)
        broadcast :put, "/projects/#{ project_id }/remove", querystring(subject_id: id, group_id: group_id)
      end
      
      def load_user(id, host: host, project_id: project_id)
        send_http host, :post, "/projects/#{ project_id }/users/#{ id }/load"
      end
      
      def add_seen(id, user_id: user_id, host: host, project_id: project_id)
        send_http host, :put, "/projects/#{ project_id }/users/#{ user_id }/add_seen", querystring(subject_id: id)
      end
      
      protected
      
      def broadcast(action, path, query = '')
        Cellect::Client.node_set.nodes.each_pair do |node, host|
          send_http host, action, path, query
        end
      end
      
      def send_http(host, action, path, query = '')
        params = { host: host, path: path }
        params[:query] = query if query && !query.empty?
        uri = URI::HTTP.build params
        HTTP.send action, uri.to_s, socket_class: Celluloid::IO::TCPSocket
      end
      
      def querystring(hash = { })
        [].tap do |list|
          hash.each_pair do |key, value|
            next unless value
            list << "#{ key }=#{ value }"
          end
        end.join('&')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cellect-client-0.0.1 lib/cellect/client/connection.rb