class Srchio::Client

Attributes

api_domain[RW]
api_protocol[RW]
api_token[RW]
searcher_id[RW]

Public Class Methods

api_domain() click to toggle source
# File lib/srchio/client.rb, line 18
def self.api_domain
  @@api_domain ||= "srch.io"
end
api_domain=(domain) click to toggle source
# File lib/srchio/client.rb, line 22
def self.api_domain=(domain)
  @@api_domain = domain
  update_base_uri
end
api_protocol() click to toggle source
# File lib/srchio/client.rb, line 9
def self.api_protocol
  @@api_protocol ||= "https"
end
api_protocol=(p) click to toggle source
# File lib/srchio/client.rb, line 13
def self.api_protocol=(p)
  @@api_protocol = p
  update_base_uri
end
api_token() click to toggle source
# File lib/srchio/client.rb, line 27
def self.api_token
  @@api_token || nil
end
api_token=(token) click to toggle source
# File lib/srchio/client.rb, line 31
def self.api_token=(token)
  @@api_token = token
  update_default_headers
  true
end
new(opts={}) click to toggle source

Create a new Srchio::Client. options:

  • :searcher_id: The ID of the searcher to use for this client.

  • :api_token: This is required if you haven't set it using Srch::Client.api_token=

  • :api_protocol: optional - defaults to https

  • :api_domain: optional - defaults to srch.io

# File lib/srchio/client.rb, line 64
def initialize(opts={})
  raise ArgumentError, "opts must be a Hash" unless opts.is_a?(Hash)
  
  @searcher_id = opts[:searcher_id] || @@searcher_id
  
  raise ArgumentError, ":searcher_id must not be nil" if @searcher_id.nil?
end
searcher_id() click to toggle source
# File lib/srchio/client.rb, line 37
def self.searcher_id
  @@searcher_id || nil
end
searcher_id=(id) click to toggle source
# File lib/srchio/client.rb, line 41
def self.searcher_id=(id)
  @@searcher_id = id
  true
end
update_base_uri() click to toggle source
# File lib/srchio/client.rb, line 46
def self.update_base_uri
  base_uri "#{self.api_protocol}://#{self.api_domain}/api/v1/"
  true
end
update_default_headers() click to toggle source
# File lib/srchio/client.rb, line 51
def self.update_default_headers
  headers 'X_SRCH_API_TOKEN' => self.api_token
  true
end

Public Instance Methods

add_document(opts={}) click to toggle source

Add or update a document in the searcher. options:

  • :url: required, the URL for the document

*

  • :body: required, the body of the document

  • :tags: optional, the list of tags for the document.

  • :remote_id: optional, but recommended, the id of the document in your system.

  • :created: optional - the timestamp for the record.

# File lib/srchio/client.rb, line 89
def add_document(opts={})
  raise ArgumentError, ":title is required" if opts[:title].nil?
  raise ArgumentError, ":body is required" if opts[:body].nil?
  raise ArgumentError, ":url is required" if opts[:url].nil?
  
  if opts[:tags].is_a?(Array)
    opts[:tags] = opts[:tags].join(",")
  end
  
  doc = opts.to_json
  
  Srchio::Response.new(self.class.post("/searchers/#{searcher_id}/documents", :body => doc))
end
destroy_document(opts={}) click to toggle source

Delete a document from the searcher. options: One of the following is required.

  • :remote_id: The remote_id you set when adding the document.

  • :index_id: The id returned by the system when you added the document

# File lib/srchio/client.rb, line 109
def destroy_document(opts={})
  raise ArgumentError if opts[:remote_id].nil? && opts[:index_id].nil?
  
  Srchio::Response.new(self.class.delete("/searchers/#{searcher_id}/documents", :query => opts))
end
tag_cloud(opts={}) click to toggle source

Returns the tag cloud for your searcher. It will only return the number of unique tags you have that appear in at least one document. options:

  • :n: The number of tags to return. Defaults to 1,000.

end

# File lib/srchio/client.rb, line 144
def tag_cloud(opts={})
  Srchio::Response.new(self.class.get("/searchers/#{searcher_id}/tag_cloud", :query => opts))
end
test() click to toggle source

Sends a test request, making sure you're sending the api token correctly

# File lib/srchio/client.rb, line 75
def test
  Srchio::Response.new(self.class.get("/test"))
end