module GMO::NetHTTPService

Public Class Methods

create_http(server, options) click to toggle source
   # File lib/gmo/http_services.rb
62 def self.create_http(server, options)
63   if options[:proxy]
64     proxy = URI.parse(options[:proxy])
65     http  = Net::HTTP.new \
66       server, 443,
67       proxy.host, proxy.port,
68       proxy.user, proxy.password
69   else
70     http = Net::HTTP.new server, 443
71   end
72   if options[:timeout]
73     http.open_timeout = options[:timeout]
74     http.read_timeout = options[:timeout]
75   end
76   http
77 end
encode_params(param_hash) click to toggle source
   # File lib/gmo/http_services.rb
53 def self.encode_params(param_hash)
54   ((param_hash || {}).collect do |key_and_value|
55     key_and_value[1] = GMO::JSON.dump(key_and_value[1]) if key_and_value[1].class != String
56     # converting to Shift-JIS
57     sjis_value = NKF.nkf('-s', key_and_value[1])
58     "#{key_and_value[0].to_s}=#{CGI.escape sjis_value}"
59   end).join("&")
60 end
included(base) click to toggle source
   # File lib/gmo/http_services.rb
28 def self.included(base)
29   base.class_eval do
30     require "net/http" unless defined?(Net::HTTP)
31     require "net/https"
32 
33     include GMO::HTTPService
34 
35     def self.make_request(path, args, verb, options = {})
36       args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
37 
38       http = create_http(server(options), options)
39       http.use_ssl = true
40 
41       http.start do |h|
42         response = if verb == "post"
43           h.post(path, encode_params(args))
44         else
45           h.get("#{path}?#{encode_params(args)}")
46         end
47         GMO::Response.new(response.code.to_i, response.body, response)
48       end
49     end
50 
51     protected
52 
53       def self.encode_params(param_hash)
54         ((param_hash || {}).collect do |key_and_value|
55           key_and_value[1] = GMO::JSON.dump(key_and_value[1]) if key_and_value[1].class != String
56           # converting to Shift-JIS
57           sjis_value = NKF.nkf('-s', key_and_value[1])
58           "#{key_and_value[0].to_s}=#{CGI.escape sjis_value}"
59         end).join("&")
60       end
61 
62       def self.create_http(server, options)
63         if options[:proxy]
64           proxy = URI.parse(options[:proxy])
65           http  = Net::HTTP.new \
66             server, 443,
67             proxy.host, proxy.port,
68             proxy.user, proxy.password
69         else
70           http = Net::HTTP.new server, 443
71         end
72         if options[:timeout]
73           http.open_timeout = options[:timeout]
74           http.read_timeout = options[:timeout]
75         end
76         http
77       end
78 
79   end
80 end
make_request(path, args, verb, options = {}) click to toggle source
   # File lib/gmo/http_services.rb
35 def self.make_request(path, args, verb, options = {})
36   args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
37 
38   http = create_http(server(options), options)
39   http.use_ssl = true
40 
41   http.start do |h|
42     response = if verb == "post"
43       h.post(path, encode_params(args))
44     else
45       h.get("#{path}?#{encode_params(args)}")
46     end
47     GMO::Response.new(response.code.to_i, response.body, response)
48   end
49 end