Module: Koala::NetHTTPService

Defined in:
lib/http_services.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

this service uses Net::HTTP to send requests to the graph



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/http_services.rb', line 4

def self.included(base)
  base.class_eval do
    require 'net/http' unless defined?(Net::HTTP)
    require 'net/https'
  
    def self.make_request(path, args, verb, graph = true)
      # We translate args to a valid query string. If post is specified,
      # we send a POST request to the given path with the given arguments.

      # if the verb isn't get or post, send it as a post argument
      args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"

      http = Net::HTTP.new(graph ? Facebook::GRAPH_SERVER : Facebook::REST_SERVER, 443)
      http.use_ssl = true
      # we turn off certificate validation to avoid the 
      # "warning: peer certificate won't be verified in this SSL session" warning
      # not sure if this is the right way to handle it
      # see http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE

      result = http.start { |http|
        response, body = (verb == "post" ? http.post(path, encode_params(args)) : http.get("#{path}?#{encode_params(args)}")) 
        body
      }
    end

    protected
    def self.encode_params(param_hash)
      # TODO investigating whether a built-in method handles this
      # if no hash (e.g. no auth token) return empty string
      ((param_hash || {}).collect do |key_and_value| 
        key_and_value[1] = key_and_value[1].to_json if key_and_value[1].class != String
        "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
      end).join("&")
    end
  end
end