Skip to content

Commit

Permalink
Merge pull request #36 from malparty/feature/google-search-raw
Browse files Browse the repository at this point in the history
#6 As a user, I can query a single keyword and get its Google search raw response
  • Loading branch information
malparty committed Jun 24, 2021
2 parents afd4b1e + 8dbecbc commit cf89874
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -14,6 +14,7 @@ gem 'sidekiq' # background processing for Ruby
gem 'bootsnap', require: false # Reduces boot times through caching; required in config/boot.rb
gem 'i18n-js', '3.5.1' # A library to provide the I18n translations on the Javascript
gem 'jsonapi-serializer' # A fast JSON:API serializer for Ruby Objects.
gem 'httparty' # A HTTP client for Ruby.

# Authentications & Authorizations
gem 'devise' # Authentication solution for Rails with Warden
Expand All @@ -24,6 +25,9 @@ gem 'doorkeeper' # Awesome OAuth 2 provider for your Rails / Grape app
gem 'webpacker', '~>5.2.0' # Transpile app-like JavaScript
gem 'sass-rails' # SASS

# Logging tools
gem 'colorize' # Ruby gem for colorizing text using ANSI escape sequences

# Translations
# gem 'devise-i18n' # Translations for Devise
# gem 'rails-i18n', '~> 6.0.0' # Translations for Rails
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Expand Up @@ -97,6 +97,7 @@ GEM
sexp_processor
coderay (1.1.3)
colored2 (3.1.2)
colorize (0.8.1)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
cork (0.3.0)
Expand Down Expand Up @@ -188,6 +189,9 @@ GEM
globalid (0.4.2)
activesupport (>= 4.2.0)
hashdiff (1.0.1)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
i18n-js (3.5.1)
Expand Down Expand Up @@ -220,11 +224,15 @@ GEM
mini_mime (>= 0.1.1)
marcel (1.0.1)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0225)
mini_magick (4.11.0)
mini_mime (1.0.3)
mini_portile2 (2.5.3)
minitest (5.14.4)
msgpack (1.4.2)
multi_xml (0.6.0)
multipart-post (2.1.1)
nap (1.1.0)
nio4r (2.5.7)
Expand Down Expand Up @@ -473,6 +481,7 @@ DEPENDENCIES
brakeman
bullet
capybara (>= 2.15)
colorize
danger
danger-brakeman_scanner
danger-eslint
Expand All @@ -489,6 +498,7 @@ DEPENDENCIES
ffaker
figaro
foreman
httparty
i18n-js (= 3.5.1)
json_matchers
jsonapi-serializer
Expand Down
40 changes: 40 additions & 0 deletions app/services/google/client_service.rb
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Google
class ClientService
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '\
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'

BASE_SEARCH_URL = 'https://www.google.com/search'

def initialize(keyword:, lang: 'en')
@escaped_keyword = CGI.escape(keyword)
@uri = URI("#{BASE_SEARCH_URL}?q=#{@escaped_keyword}&hl=#{lang}&gl=#{lang}")
end

def call
result = HTTParty.get(@uri, { headers: { 'User-Agent' => USER_AGENT } })

return false unless valid_result? result

result
rescue HTTParty::Error, Timeout::Error, SocketError => e
Rails.logger.error "Error: Query Google with '#{@escaped_keyword}' thrown an error: #{e}".colorize(:red)

false
end

private

# Inspect Http response status code
# Any non 200 response code will be logged
def valid_result?(result)
return true if result&.response&.code == '200'

Rails.logger.warn "Warning: Query Google with '#{@escaped_keyword}' return status code #{result.response.code}"
.colorize(:yellow)

false
end
end
end
2 changes: 2 additions & 0 deletions config/locales/en.yml
Expand Up @@ -33,6 +33,8 @@ en:
app_name: 'Google Search Ruby'
doorkeeper:
token_revoked: 'If your token was valid, it has been revoked'
keywords:
could_not_query: 'An error occurs when performing the Google Search, please try again.'
auth:
logout: 'Sign out'
sign_up: 'Sign up'
Expand Down
85 changes: 43 additions & 42 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions spec/fixtures/vcr/google_search.yml

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions spec/fixtures/vcr/google_warn.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions spec/services/google/client_service_spec.rb
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Google::ClientService, type: :service do
context 'when querying a simple keyword' do
it 'returns an HTTParty Response', vcr: 'google_search' do
result = described_class.new(keyword: FFaker::Lorem.word).call

expect(result).to be_an_instance_of(HTTParty::Response)
end

it 'queries Google Search', vcr: 'google_search' do
path = described_class.new(keyword: FFaker::Lorem.word).call.request.path

expect(path.to_s).to start_with(described_class::BASE_SEARCH_URL)
end
end

context 'when google returns an HTTP error' do
it 'returns false', vcr: 'google_warn' do
result = described_class.new(keyword: FFaker::Lorem.word).call

expect(result).to eq(false)
end

it 'logs a warning with the escaped keyword', vcr: 'google_warn' do
allow(Rails.logger).to receive(:warn)

word = FFaker::Lorem.word
described_class.new(keyword: word).call

expect(Rails.logger).to have_received(:warn).with(/#{CGI.escape(word)}/)
end
end
end
1 change: 1 addition & 0 deletions spec/support/authentication_helper.rb
Expand Up @@ -16,6 +16,7 @@ def sign_in_ui(user = nil)

fill_in 'user_email', with: user.email
fill_in 'user_password', with: user.password

click_button 'Sign in'
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/vcr.rb
Expand Up @@ -14,7 +14,7 @@
end

RSpec.configure do |config|
# You can pass a hash to the `vcr` tag to specifcy additional options:
# You can pass a hash to the `vcr` tag to specify additional options:
# vcr: { group: 'places/google/details', cassettes: %w(kfc red_planet)}
# vcr: { group: 'places/google/details', cassette: 'kfc'}
# vcr: { cassette: 'places/google/details', options: { decode_compressed_response: true } }
Expand Down

0 comments on commit cf89874

Please sign in to comment.