module GMO::NetHTTPService

Public Class Methods

create_http(server, options) click to toggle source
   # File lib/gmo/http_services.rb
67 def self.create_http(server, options)
68   if options[:proxy]
69     proxy = URI.parse(options[:proxy])
70     http  = Net::HTTP.new \
71       server, 443,
72       proxy.host, proxy.port,
73       proxy.user, proxy.password
74   else
75     http = Net::HTTP.new server, 443
76   end
77   if options[:timeout]
78     http.open_timeout = options[:timeout]
79     http.read_timeout = options[:timeout]
80   end
81   http
82 end
encode_params(param_hash) click to toggle source
   # File lib/gmo/http_services.rb
58 def self.encode_params(param_hash)
59   ((param_hash || {}).collect do |key_and_value|
60     key_and_value[1] = GMO::JSON.dump(key_and_value[1]) if key_and_value[1].class != String
61     # converting to Shift-JIS
62     sjis_value = NKF.nkf('-s', key_and_value[1])
63     "#{key_and_value[0].to_s}=#{CGI.escape sjis_value}"
64   end).join("&")
65 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           if /\.json\Z/ =~ path
44             headers = { "Content-Type" => "application/json" }
45             h.post(path, args.to_json, headers)
46           else
47             h.post(path, encode_params(args))
48           end
49         else
50           h.get("#{path}?#{encode_params(args)}")
51         end
52         GMO::Response.new(response.code.to_i, response.body, response)
53       end
54     end
55 
56     protected
57 
58       def self.encode_params(param_hash)
59         ((param_hash || {}).collect do |key_and_value|
60           key_and_value[1] = GMO::JSON.dump(key_and_value[1]) if key_and_value[1].class != String
61           # converting to Shift-JIS
62           sjis_value = NKF.nkf('-s', key_and_value[1])
63           "#{key_and_value[0].to_s}=#{CGI.escape sjis_value}"
64         end).join("&")
65       end
66 
67       def self.create_http(server, options)
68         if options[:proxy]
69           proxy = URI.parse(options[:proxy])
70           http  = Net::HTTP.new \
71             server, 443,
72             proxy.host, proxy.port,
73             proxy.user, proxy.password
74         else
75           http = Net::HTTP.new server, 443
76         end
77         if options[:timeout]
78           http.open_timeout = options[:timeout]
79           http.read_timeout = options[:timeout]
80         end
81         http
82       end
83 
84   end
85 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       if /\.json\Z/ =~ path
44         headers = { "Content-Type" => "application/json" }
45         h.post(path, args.to_json, headers)
46       else
47         h.post(path, encode_params(args))
48       end
49     else
50       h.get("#{path}?#{encode_params(args)}")
51     end
52     GMO::Response.new(response.code.to_i, response.body, response)
53   end
54 end