Skip to content

Commit

Permalink
move http params to http_request helper, dry
Browse files Browse the repository at this point in the history
  • Loading branch information
portertech committed Oct 9, 2011
1 parent fdb2c3a commit 49b7392
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/redphone/helpers.rb
Expand Up @@ -11,21 +11,27 @@ def http_request(options={})
user = options[:user]
password = options[:password]
headers = options[:headers] || Hash.new
parameters = options[:parameters] || Hash.new
body = options[:body]
http = Net::HTTP.new(uri.host, uri.port)
if options[:ssl] == true
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request_uri = uri.request_uri
unless parameters.empty?
request_uri += "?"
request_uri += parameters.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
end
request = case method
when "get"
Net::HTTP::Get.new(uri.request_uri)
Net::HTTP::Get.new(request_uri)
when "post"
Net::HTTP::Post.new(uri.request_uri)
Net::HTTP::Post.new(request_uri)
when "put"
Net::HTTP::Put.new(uri.request_uri)
Net::HTTP::Put.new(request_uri)
when "delete"
Net::HTTP::Delete.new(uri.request_uri)
Net::HTTP::Delete.new(request_uri)
else
raise "Unknown HTTP method: #{method}"
end
Expand Down
9 changes: 4 additions & 5 deletions lib/redphone/loggly.rb
Expand Up @@ -15,12 +15,12 @@ def initialize(options={})

def search(options={})
raise "You must supply a query string" if options[:q].nil?
params = options.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
response = http_request(
:user => @user,
:password => @password,
:ssl => true,
:uri => "https://#{@subdomain}.loggly.com/api/search?#{params}"
:uri => "https://#{@subdomain}.loggly.com/api/search",
:parameters => options
)
JSON.parse(response.body)
end
Expand All @@ -29,13 +29,12 @@ def facets(options={})
raise "You must supply a query string" if options[:q].nil?
facet_type = options[:facet_type] || "date"
raise "Facet type must be date, ip, or input" if !%w[date ip input].include?(facet_type)
params_hash = options.reject { |key, value| key == :facet_type }
params = params_hash.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
response = http_request(
:user => @user,
:password => @password,
:ssl => true,
:uri => "https://#{@subdomain}.loggly.com/api/facets/#{facet_type}/?#{params}"
:uri => "https://#{@subdomain}.loggly.com/api/facets/#{facet_type}/",
:parameters => options.reject { |key, value| key == :facet_type }
)
JSON.parse(response.body)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/redphone/pagerduty.rb
Expand Up @@ -47,12 +47,12 @@ def resolve_incident(options={})
end

def incidents(options={})
params = options.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
response = http_request(
:user => @user,
:password => @password,
:ssl => true,
:uri => "https://#{@subdomain}.pagerduty.com/api/v1/incidents?#{params}"
:uri => "https://#{@subdomain}.pagerduty.com/api/v1/incidents",
:parameters => options
)
JSON.parse(response.body)
end
Expand Down
File renamed without changes.

0 comments on commit 49b7392

Please sign in to comment.