locomotive_google_search 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/locomotive_google_search.rb +6 -0
- data/lib/locomotive_google_search/google_search/query.rb +64 -0
- data/lib/locomotive_google_search/google_search/result.rb +15 -0
- data/lib/locomotive_google_search/google_search/search.rb +43 -0
- data/lib/locomotive_google_search/liquid/drops/result.rb +11 -0
- data/lib/locomotive_google_search/liquid/drops/search.rb +64 -0
- data/lib/locomotive_google_search/middlewares/custom_drops.rb +22 -0
- data/lib/locomotive_google_search/version.rb +3 -0
- data/locomotive_google_search-0.1.0.gem +0 -0
- data/locomotive_google_search.gemspec +23 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7dfb734c0125e4af3747ce178eb49e2e2e3797b1
|
4
|
+
data.tar.gz: 5adabe83589a8a465cd3df1a35b9e8c5874ddfae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a40734cd899868e36a1bc95d9e2ae2770d4a7420d6c093cb413aba1574018d7b0412fd4db728c773c16a51679647c4a343dff1cb305f4ef26bf7024adaaeb133
|
7
|
+
data.tar.gz: 880b5f4aaab35747e91fa652d21f8634883be5313efc88602ab5c2009518c0271f59c5b573b01e35169cb94f78f8a73fac7407f5c1e0df45db99cdd06fecf381
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# LocomotiveGoogleSearch
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/locomotive_google_search`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'locomotive_google_search'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install locomotive_google_search
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/locomotive_google_search.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "locomotive_google_search"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
class GoogleSearch::Query
|
4
|
+
|
5
|
+
attr_reader :term, :start, :data, :results
|
6
|
+
|
7
|
+
def initialize(args = {})
|
8
|
+
@term = args[:term]
|
9
|
+
@start = args[:start].try(:to_s) || "1"
|
10
|
+
pull_data
|
11
|
+
build_results
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def build_results
|
17
|
+
@results = data["items"].map do |item_hash|
|
18
|
+
GoogleSearch::Result.new(item_hash)
|
19
|
+
end
|
20
|
+
return @results
|
21
|
+
end
|
22
|
+
|
23
|
+
def pull_data
|
24
|
+
@data = Rails.env.development? ? stubbed_json : JSON.parse(open(generate_url).read)
|
25
|
+
end
|
26
|
+
|
27
|
+
def start_hash
|
28
|
+
return {} unless start.present?
|
29
|
+
return { start: start }
|
30
|
+
end
|
31
|
+
|
32
|
+
def options
|
33
|
+
@options ||= { key: ENV["GOOGLE_API_KEY"], cx: ENV["GOOGLE_SEARCH_ENGINE_ID"], q: term }.merge(start_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
def base_url
|
37
|
+
"https://www.googleapis.com/customsearch/v1?"
|
38
|
+
end
|
39
|
+
|
40
|
+
def generate_url(args = {})
|
41
|
+
path = args[:path]
|
42
|
+
url = base_url
|
43
|
+
return url unless options.present?
|
44
|
+
options_string = ""
|
45
|
+
option_to_string = Proc.new {|k, v, index| "#{'&' unless index == 0}#{k.to_s}=#{v.to_s}"}
|
46
|
+
options.each_with_index do |(key, value), index|
|
47
|
+
if value.is_a?(String)
|
48
|
+
options_string << option_to_string.call(key, value, index)
|
49
|
+
elsif value.is_a?(Array)
|
50
|
+
value.each do |val|
|
51
|
+
options_string << option_to_string.call(key, val, index)
|
52
|
+
end
|
53
|
+
else
|
54
|
+
raise "Currently only string and array values are supported. You tried a #{value.class} value."
|
55
|
+
end
|
56
|
+
end
|
57
|
+
URI.encode("#{url}#{options_string}")
|
58
|
+
end
|
59
|
+
|
60
|
+
def stubbed_json
|
61
|
+
JsonData.new(file_name: "google_search").gather
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class GoogleSearch::Result
|
2
|
+
|
3
|
+
attr_reader :title, :link, :snippet
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
@title = args["title"]
|
7
|
+
@link = args["link"]
|
8
|
+
@snippet = args["snippet"]
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_liquid
|
12
|
+
Shop::Liquid::Drops::Result.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class GoogleSearch::Search
|
2
|
+
|
3
|
+
attr_reader :term, :page, :results, :options, :query
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
@term = args[:term]
|
7
|
+
@page = args[:page].try(:to_i) || 1
|
8
|
+
start = ((@page - 1) * 10) + 1
|
9
|
+
@options = { term: term, start: start }
|
10
|
+
search_and_return_one_page
|
11
|
+
end
|
12
|
+
|
13
|
+
def search_and_return_all_results
|
14
|
+
@results = []
|
15
|
+
|
16
|
+
more_pages = true
|
17
|
+
while more_pages
|
18
|
+
search = GoogleSearch::Query.new(options)
|
19
|
+
@results << search.results
|
20
|
+
more_pages = search.data["queries"]["nextPage"].present? ? true : false
|
21
|
+
options[:start] += 10
|
22
|
+
end
|
23
|
+
@results.flatten!
|
24
|
+
end
|
25
|
+
|
26
|
+
def search_and_return_one_page
|
27
|
+
@query = GoogleSearch::Query.new(options)
|
28
|
+
@results = @query.results
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_next_page?
|
32
|
+
query.data["queries"]["nextPage"].present?
|
33
|
+
end
|
34
|
+
|
35
|
+
def next_page
|
36
|
+
((query.data["queries"]["nextPage"].first["startIndex"].to_i - 1) / 10) + 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def previous_page
|
40
|
+
page - 1
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module LocomotiveGoogleSearch
|
2
|
+
module Liquid
|
3
|
+
module Drops
|
4
|
+
class Search < ::Liquid::Drop
|
5
|
+
|
6
|
+
def first
|
7
|
+
self.collection.first
|
8
|
+
end
|
9
|
+
|
10
|
+
def refresh
|
11
|
+
@search_object = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def last
|
15
|
+
self.collection.last
|
16
|
+
end
|
17
|
+
|
18
|
+
def each(&block)
|
19
|
+
self.collection.each(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def each_with_index(&block)
|
23
|
+
self.collection.each_with_index(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def page
|
27
|
+
@context.registers[:controller].params[:page] || 1
|
28
|
+
end
|
29
|
+
|
30
|
+
def options
|
31
|
+
text = @context.registers[:controller].params[:query]
|
32
|
+
{ term: text, page: page }
|
33
|
+
end
|
34
|
+
|
35
|
+
def search_object
|
36
|
+
return @search_object if @search_object.present?
|
37
|
+
@search_object = ::GoogleSearch::Search.new(options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def collection
|
41
|
+
@search_object = nil # @collection is cached across requests
|
42
|
+
@collection = search_object.results
|
43
|
+
end
|
44
|
+
|
45
|
+
def next_page
|
46
|
+
search_object.next_page
|
47
|
+
end
|
48
|
+
|
49
|
+
def next_page?
|
50
|
+
search_object.has_next_page?
|
51
|
+
end
|
52
|
+
|
53
|
+
def previous_page?
|
54
|
+
page != 1
|
55
|
+
end
|
56
|
+
|
57
|
+
def previous_page
|
58
|
+
search_object.previous_page
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'locomotive_google_search/google_search/query'
|
2
|
+
require 'locomotive_google_search/google_search/result'
|
3
|
+
require 'locomotive_google_search/google_search/search'
|
4
|
+
require 'locomotive_google_search/liquid/drops/results'
|
5
|
+
require 'locomotive_google_search/liquid/drops/search'
|
6
|
+
|
7
|
+
module LocomotiveGoogleSearch
|
8
|
+
module Middlewares
|
9
|
+
class CustomDrops
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
(env['steam.liquid_assigns'] ||= {}).tap do |assigns|
|
16
|
+
assigns['google_search'] = LocomotiveGoogleSearch::Liquid::Drops::Search.new
|
17
|
+
end
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
Binary file
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'locomotive_google_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "locomotive_google_search"
|
8
|
+
spec.version = LocomotiveGoogleSearch::VERSION
|
9
|
+
spec.authors = ["Ryan Francis"]
|
10
|
+
spec.email = ["ryan.p.francis@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem for implementing Google Search in LocomotiveCMS}
|
13
|
+
spec.homepage = "https://github.com/LaunchPadLab/locomotive_google_search"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: locomotive_google_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Francis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- ryan.p.francis@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/locomotive_google_search.rb
|
71
|
+
- lib/locomotive_google_search/google_search/query.rb
|
72
|
+
- lib/locomotive_google_search/google_search/result.rb
|
73
|
+
- lib/locomotive_google_search/google_search/search.rb
|
74
|
+
- lib/locomotive_google_search/liquid/drops/result.rb
|
75
|
+
- lib/locomotive_google_search/liquid/drops/search.rb
|
76
|
+
- lib/locomotive_google_search/middlewares/custom_drops.rb
|
77
|
+
- lib/locomotive_google_search/version.rb
|
78
|
+
- locomotive_google_search-0.1.0.gem
|
79
|
+
- locomotive_google_search.gemspec
|
80
|
+
homepage: https://github.com/LaunchPadLab/locomotive_google_search
|
81
|
+
licenses: []
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.5.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A gem for implementing Google Search in LocomotiveCMS
|
103
|
+
test_files: []
|