srchio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NThhN2Y1YTU2ODUwODdlNGQ0YzY3ZDcyYzU2OWQzOGIzNjM4OTQxNw==
5
+ data.tar.gz: !binary |-
6
+ ZGY1N2M4NWI3MWZmMGFlNDk1ZGMxYzIxNTljNmUxMWRkYjg0OWRjNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ M2VkZDM5OWQ1NzRiYzMwN2RmM2FkMzhkYTU1NmY5Yzk1MjJkZjk3NmJlZWUy
10
+ ZjY2NmY3ZGJmOTY3ZGU4ZTgzZGIxYTFkOTBkYWE4Njk2ZTIzZjg0NTkwMmJk
11
+ NjRmYzdhNGVmNmQ1ODQ5ZjYwZDZlNGQ1ZGY4MTViZGIxMDA3YTg=
12
+ data.tar.gz: !binary |-
13
+ YmQ4Mjg3MWZmNzUyODM4Yzk5YWEyYTQxODhiZGE3NDg3NjQyZGQyYTJlODhj
14
+ YWY4ODQ0MGE2YTcxYjVjNjNjNDc1Y2YzZGNkNWMxMWJmYjMxYjdmODU1NDZi
15
+ ODFhMWZiMGUxMTMzZDZjMzIzYzQ3MGQ3YTdhOGJkZmIwZDllNWU=
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'httparty'
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ ## Introduction
2
+
3
+ This is the ruby wrapper for [srch.io](http://srch.io). We're a platform as a service providing an easy developer-friendly way to add search to your application or site!
4
+
5
+ ## Setup
6
+
7
+ Add the gem to your Gemfile:
8
+
9
+ <pre><code>gem 'srchio'</code></pre>
10
+
11
+ Run <code>bundle install</code>.
12
+
13
+ Log in to your account on [srch.io](http://srch.io) and create a new Searcher. You'll need two pieces of information to use the gem: your api token, and the id of your searcher. You can get your API Token on your [Account page](https://srch.io/account), and the ID of your searcher from that searcher's page, linked from [Your Searchers](https://srch.io/searchers).
14
+
15
+ If you're using it in Rails, you'll probably want to create an initializer that looks something like this:
16
+
17
+ <pre><code>require 'srchio'
18
+ Srchio::Client.api_token = ENV['SRCH_API_TOKEN']
19
+ Srchio::Client.searcher_id = ENV['SRCH_ID']</code></pre>
20
+
21
+ ## Usage
22
+
23
+ Once you've configured things, you'll want to use it!
24
+
25
+ <pre><code>client = Srchio::Client.new
26
+
27
+ client.add_document(
28
+ url: "http://srch.io",
29
+ title: "srch.io - home",
30
+ body: "srch.io is a fun way to add search to your app.",
31
+ remote_id: 1,
32
+ tags: ['fun', 'api', 'awesome']
33
+ )
34
+
35
+ client.search(query: "fun")</code></pre>
36
+
37
+ If you need to delete a document, you can do it either with your remote_id or the index_id returned in the add_document call:
38
+
39
+ <pre><code>client.destroy_document(remote_id: 1)</code></pre>
40
+
41
+ If you have any issues, please create a [Github Issue](https://github.com/railsmachine/srchio/issues). Thanks for using srch.io!
data/lib/srchio.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'httparty'
2
+ require 'srchio/client'
3
+ require 'srchio/response'
4
+ require 'srchio/result'
5
+ require 'srchio/version'
6
+
7
+ module Srchio
8
+
9
+ end
@@ -0,0 +1,132 @@
1
+ module Srchio
2
+ class Client
3
+ include HTTParty
4
+
5
+ attr_accessor :searcher_id, :api_token, :api_domain, :api_protocol
6
+
7
+ base_uri "https://srch.io/"
8
+
9
+ def self.api_protocol
10
+ @@api_protocol ||= "https"
11
+ end
12
+
13
+ def self.api_protocol=(p)
14
+ @@api_protocol = p
15
+ update_base_uri
16
+ end
17
+
18
+ def self.api_domain
19
+ @@api_domain ||= "srch.io"
20
+ end
21
+
22
+ def self.api_domain=(domain)
23
+ @@api_domain = domain
24
+ update_base_uri
25
+ end
26
+
27
+ def self.api_token
28
+ @@api_token || nil
29
+ end
30
+
31
+ def self.api_token=(token)
32
+ @@api_token = token
33
+ update_default_headers
34
+ true
35
+ end
36
+
37
+ def self.searcher_id
38
+ @@searcher_id || nil
39
+ end
40
+
41
+ def self.searcher_id=(id)
42
+ @@searcher_id = id
43
+ true
44
+ end
45
+
46
+ def self.update_base_uri
47
+ base_uri = "#{self.api_protocol}://#{self.api_domain}/"
48
+ true
49
+ end
50
+
51
+ def self.update_default_headers
52
+ headers 'X_SRCH_API_TOKEN' => self.api_token
53
+ true
54
+ end
55
+
56
+ =begin rdoc
57
+ Create a new Srchio::Client.
58
+ options:
59
+ * :searcher_id: The ID of the searcher to use for this client.
60
+ * :api_token: This is required if you haven't set it using Srch::Client.api_token=
61
+ * :api_protocol: optional - defaults to https
62
+ * :api_domain: optional - defaults to srch.io
63
+ =end
64
+ def initialize(opts={})
65
+ raise ArgumentError, "opts must be a Hash" unless opts.is_a?(Hash)
66
+
67
+ @searcher_id = opts[:searcher_id] || @@searcher_id
68
+
69
+ raise ArgumentError, ":searcher_id must not be nil" if @searcher_id.nil?
70
+ end
71
+
72
+ =begin rdoc
73
+ Sends a test request, making sure you're sending the api token correctly
74
+ =end
75
+ def test
76
+ Srchio::Response.new(self.class.get("/api/test"))
77
+ end
78
+
79
+ =begin rdoc
80
+ Add or update a document in the searcher.
81
+ options:
82
+ * :url: required, the URL for the document
83
+ * :title: required, the title of the document
84
+ * :body: required, the body of the document
85
+ * :tags: optional, the list of tags for the document.
86
+ * :remote_id: optional, but recommended, the id of the document in your system.
87
+ =end
88
+ def add_document(opts={})
89
+ raise ArgumentError, ":title is required" if opts[:title].nil?
90
+ raise ArgumentError, ":body is required" if opts[:body].nil?
91
+ raise ArgumentError, ":url is required" if opts[:url].nil?
92
+
93
+ if opts[:tags].is_a?(Array)
94
+ opts[:tags] = opts[:tags].join(",")
95
+ end
96
+
97
+ Srchio::Response.new(self.class.post("/api/searchers/#{searcher_id}/documents", :query => opts))
98
+ end
99
+
100
+ =begin rdoc
101
+ Delete a document from the searcher.
102
+ options: One of the following is required.
103
+ * :remote_id: The remote_id you set when adding the document.
104
+ * :index_id: The id returned by the system when you added the document
105
+ =end
106
+ def destroy_document(opts={})
107
+ raise ArgumentError if opts[:remote_id].nil? && opts[:index_id].nil?
108
+
109
+ Srchio::Response.new(self.class.delete("/api/searchers/#{searcher_id}/documents", :query => opts))
110
+ end
111
+
112
+ =begin rdoc
113
+ Searches your searcher!
114
+ options:
115
+ * :query: A text string to search for. Searches title, body and tags for this string.
116
+ * :body: A query, searches just the body.
117
+ * :title: A query, searches just the title.
118
+ * :tags: An array of tags to search for.
119
+ * :remote_id: A remote_id to search for.
120
+ * :id: The id of the document you wish to retrieve.
121
+ * :page: The page of results to return. Defaults to 1.
122
+ * :per_page: How many results to return per page. Defaults to 25.
123
+ =end
124
+ def search(opts={})
125
+ if opts[:query].nil? && opts[:body].nil? && opts[:title].nil? && opts[:tags].nil? && opts[:remote_id].nil? && opts[:id].nil?
126
+ raise ArgumentError, "Opts must contain at least one of: :query, :body, :title, :tags, :remote_id, :id"
127
+ end
128
+
129
+ Srchio::Response.new(self.class.get("/api/searchers/#{searcher_id}/search", :query => opts))
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,26 @@
1
+ module Srchio
2
+ class Response
3
+ attr_accessor :success, :error, :results, :pages, :current_page, :previous_page
4
+
5
+ def initialize(response)
6
+ puts response.body
7
+ r = response.parsed_response
8
+ @success = r['success']
9
+ if r['success'].nil?
10
+ @success = false
11
+ end
12
+ @error = r['error']
13
+ @pages = r['pages']
14
+ @current_page = r['current_page']
15
+ @next_page = r['next_page']
16
+ @previous_page = r['previous_page']
17
+
18
+ if r['results'].is_a?(Array)
19
+ @results = []
20
+ r['results'].each do |result|
21
+ @results << Srchio::Result.new(result)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Srchio
2
+ class Result
3
+ attr_accessor :title, :highlight, :url, :tags, :remote_id, :index_id
4
+
5
+ def initialize(result={})
6
+ @title = result['title']
7
+ @highlight = result['highlight']
8
+ @url = result['url']
9
+ @tags = result['tags']
10
+ @remote_id = result['remote_id']
11
+ @index_id = result['index_id']
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Srchio
2
+ VERSION = "0.0.1"
3
+ end
data/srchio.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "srchio/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "srchio"
6
+ s.version = Srchio::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Kevin Lawver"]
9
+ s.email = ["support@srch.io"]
10
+ s.licenses = ["MIT"]
11
+ s.homepage = "http://github.com/railsmachine/srchio"
12
+ s.summary = %q{The official gem for srch.io! Makes searching fun and easy.}
13
+ s.description = %q{The official gem and wrapper for the srch.io API. More info @ http://srch.io}
14
+
15
+ s.required_ruby_version = '>= 1.9.3'
16
+
17
+ s.add_dependency 'httparty', ">= 0.12.0"
18
+
19
+ s.post_install_message = "Hooray, you've installed srchio! Thanks!"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: srchio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Lawver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.12.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.12.0
27
+ description: The official gem and wrapper for the srch.io API. More info @ http://srch.io
28
+ email:
29
+ - support@srch.io
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - README.md
36
+ - lib/srchio.rb
37
+ - lib/srchio/client.rb
38
+ - lib/srchio/response.rb
39
+ - lib/srchio/result.rb
40
+ - lib/srchio/version.rb
41
+ - srchio.gemspec
42
+ homepage: http://github.com/railsmachine/srchio
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message: Hooray, you've installed srchio! Thanks!
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.9.3
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.1.11
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: The official gem for srch.io! Makes searching fun and easy.
66
+ test_files: []