Skip to content

Event Driven Support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ gem 'rspec'
gem 'vcr'
gem 'webmock'
gem 'timecop'
gem 'eventmachine'
gem 'em-http-request'
60 changes: 46 additions & 14 deletions lib/serp_metrics/command_sets/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'eventmachine'
require 'em-http'
require 'fiber'
require 'httpclient'

module SerpMetrics
Expand All @@ -9,6 +12,40 @@ def initialize(client)
self.client = client
end

private

def get path, query=nil
if EM.reactor_thread?
f = Fiber.current
http = EventMachine::HttpRequest.new(path).get(:query => query)
http.errback {f.resume(http.response)}
http.callback {f.resume(http.response)}
return Fiber.yield
else
return HTTPClient.get(path, {
:query => (to_query(query) unless query.empty?)
} ).body
end
end

def post path, query
if EM.reactor_thread?
f = Fiber.current
http = EventMachine::HttpRequest.new(path).post(:body => query)
http.errback {f.resume(http.response)}
http.callback {f.resume(http.response)}
return Fiber.yield
else
inner_res = HTTPClient.post(path, {
:body => (to_query(query) unless query.empty?)
})
if inner_res.redirect?
inner_res = HTTPClient.get(inner_res.http_header['Location'].first)
end
return inner_res.body
end
end

protected

def transact(method, path, options = {})
Expand All @@ -17,24 +54,19 @@ def transact(method, path, options = {})
query = signature_hash
query = query.merge(:params => options.to_json) unless options.empty?

res = case method
body = case method
when :get
HTTPClient.get(SerpMetrics::API_URI + path, {
:query => (to_query(query) unless query.empty?)
})
get(SerpMetrics::API_URI + path, query)
when :post
inner_res = HTTPClient.post(SerpMetrics::API_URI + path, {
:body => (to_query(query) unless query.empty?)
})

if inner_res.redirect?
inner_res = HTTPClient.get(inner_res.http_header['Location'].first)
end

inner_res
post(SerpMetrics::API_URI + path, query)
end

JSON.parse(res.body)
begin
result = JSON.parse(body)
rescue => e
result = {'status'=>"error: #{e.message}", 'raw'=>body}
end
result
end

private
Expand Down
8 changes: 6 additions & 2 deletions lib/serp_metrics/command_sets/keywords.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ def serp(check_id, domain = nil)
transact(:post, '/keywords/serp', transaction_options)
end

def all
transact(:post, '/keywords/all')
def all page=1, limit=100
transaction_options = {
:page => page,
:limit => limit
}
transact(:post, '/keywords/all', transaction_options)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/serp_metrics/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SerpMetrics
VERSION = '0.1.0'
VERSION = '0.1.3'
end
2 changes: 2 additions & 0 deletions serp_metrics.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ Gem::Specification.new do |gem|

gem.add_runtime_dependency 'multi_json', '~> 1.3'
gem.add_runtime_dependency 'httpclient', '~> 2.3'
gem.add_runtime_dependency 'eventmachine'
gem.add_runtime_dependency 'em-http-request'
end