Class: Srchio::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/srchio/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

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

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
# 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

Instance Attribute Details

#api_domainObject

Returns the value of attribute api_domain.



5
6
7
# File 'lib/srchio/client.rb', line 5

def api_domain
  @api_domain
end

#api_protocolObject

Returns the value of attribute api_protocol.



5
6
7
# File 'lib/srchio/client.rb', line 5

def api_protocol
  @api_protocol
end

#api_tokenObject

Returns the value of attribute api_token.



5
6
7
# File 'lib/srchio/client.rb', line 5

def api_token
  @api_token
end

#searcher_idObject

Returns the value of attribute searcher_id.



5
6
7
# File 'lib/srchio/client.rb', line 5

def searcher_id
  @searcher_id
end

Class Method Details

.api_domainObject



18
19
20
# File 'lib/srchio/client.rb', line 18

def self.api_domain
  @@api_domain ||= "srch.io"
end

.api_domain=(domain) ⇒ Object



22
23
24
25
# File 'lib/srchio/client.rb', line 22

def self.api_domain=(domain)
  @@api_domain = domain
  update_base_uri
end

.api_protocolObject



9
10
11
# File 'lib/srchio/client.rb', line 9

def self.api_protocol
  @@api_protocol ||= "https"
end

.api_protocol=(p) ⇒ Object



13
14
15
16
# File 'lib/srchio/client.rb', line 13

def self.api_protocol=(p)
  @@api_protocol = p
  update_base_uri
end

.api_tokenObject



27
28
29
# File 'lib/srchio/client.rb', line 27

def self.api_token
  @@api_token || nil
end

.api_token=(token) ⇒ Object



31
32
33
34
35
# File 'lib/srchio/client.rb', line 31

def self.api_token=(token)
  @@api_token = token
  update_default_headers
  true
end

.searcher_idObject



37
38
39
# File 'lib/srchio/client.rb', line 37

def self.searcher_id
  @@searcher_id || nil
end

.searcher_id=(id) ⇒ Object



41
42
43
44
# File 'lib/srchio/client.rb', line 41

def self.searcher_id=(id)
  @@searcher_id = id
  true
end

.update_base_uriObject



46
47
48
49
# 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_headersObject



51
52
53
54
# File 'lib/srchio/client.rb', line 51

def self.update_default_headers
  headers 'X_SRCH_API_TOKEN' => self.api_token
  true
end

Instance Method Details

#add_document(opts = {}) ⇒ Object

Add or update a document in the searcher. options:

  • :url: required, the URL for the document

  • :title: required, the title of 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.

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
# 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 = {}) ⇒ Object

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

Raises:

  • (ArgumentError)


109
110
111
112
113
# 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

#search(opts = {}) ⇒ Object

Searches your searcher! options:

  • :query: A text string to search for. Searches title, body and tags for this string.

  • :body: A query, searches just the body.

  • :title: A query, searches just the title.

  • :tags: An array of tags to search for.

  • :remote_id: A remote_id to search for.

  • :id: The id of the document you wish to retrieve.

  • :page: The page of results to return. Defaults to 1.

  • :per_page: How many results to return per page. Defaults to 25.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/srchio/client.rb', line 127

def search(opts={})
  if opts[:query].nil? && opts[:body].nil? && opts[:title].nil? && opts[:tags].nil? && opts[:remote_id].nil? && opts[:id].nil?
    raise ArgumentError, "Opts must contain at least one of: :query, :body, :title, :tags, :remote_id, :id"
  end
  
  if opts[:tags].is_a?(Array)
    opts[:tags] = opts[:tags].join(",")
  end
  
  Srchio::Response.new(self.class.get("/searchers/#{searcher_id}/search", :query => opts))
end

#tag_cloud(opts = {}) ⇒ Object

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.



144
145
146
# 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

#testObject

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



75
76
77
# File 'lib/srchio/client.rb', line 75

def test
  Srchio::Response.new(self.class.get("/test"))
end