Class: Atig::Http

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/atig/http.rb

Constant Summary collapse

@@proxy =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionUtil

daemon, safe

Constructor Details

#initialize(logger = nil) ⇒ Http

Returns a new instance of Http.



20
21
22
23
24
# File 'lib/atig/http.rb', line 20

def initialize(logger=nil)
  @log = logger
  @cert_store = OpenSSL::X509::Store.new
  @cert_store.set_default_paths
end

Class Method Details

.proxy=(proxy) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/atig/http.rb', line 9

def self.proxy=(proxy)
  if proxy =~ /\A(?:([^:@]+)(?::([^@]+))?@)?([^:]+)(?::(\d+))?\z/ then
    @@proxy = OpenStruct.new({
                               :user => $1,
                               :password => $2,
                               :address => $3,
                               :port => $4.to_i,
                             })
  end
end

Instance Method Details

#http(uri, open_timeout = nil, read_timeout = 60) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/atig/http.rb', line 37

def http(uri, open_timeout = nil, read_timeout = 60)
  http = case
         when @@proxy
           Net::HTTP.new(uri.host, uri.port, @@proxy.address, @@proxy.port,
                         @@proxy.user, @@proxy.password)
         when ENV["HTTP_PROXY"], ENV["http_proxy"]
           proxy = URI(ENV["HTTP_PROXY"] || ENV["http_proxy"])
           Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port,
                         proxy.user, proxy.password)
         else
           Net::HTTP.new(uri.host, uri.port)
         end
  http.open_timeout = open_timeout if open_timeout # nil by default
  http.read_timeout = read_timeout if read_timeout # 60 by default
  if uri.is_a? URI::HTTPS
    http.use_ssl    = true
    http.cert_store = @cert_store
  end
  http
rescue => e
  log(:error, e) if @log
end

#req(method, uri, header = {}, credentials = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/atig/http.rb', line 60

def req(method, uri, header = {}, credentials = nil)
  accepts = ["*/*"]
  types   = { "json" => "application/json", "txt" => "text/plain" }
  ext     = uri.path[/[^.]+\z/]
  accepts.unshift types[ext] if types.key?(ext)
  user_agent = "#{self.class}/#{server_version} (#{File.basename(__FILE__)}; net-irc) Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM})"

  header["User-Agent"]      ||= user_agent
  header["Accept"]          ||= accepts.join(",")
  header["Accept-Charset"]  ||= "UTF-8,*;q=0.0" if ext != "json"

  req = case method.to_s.downcase.to_sym
        when :get
          Net::HTTP::Get.new    uri.request_uri, header
        when :head
          Net::HTTP::Head.new   uri.request_uri, header
        when :post
          Net::HTTP::Post.new   uri.path,        header
        when :put
          Net::HTTP::Put.new    uri.path,        header
        when :delete
          Net::HTTP::Delete.new uri.request_uri, header
        else # raise ""
        end
  if req.request_body_permitted?
    req["Content-Type"] ||= "application/x-www-form-urlencoded"
    req.body = uri.query
  end
  req.basic_auth(*credentials) if credentials
  req
rescue => e
  log(:error, e) if @log
end

#server_nameObject



26
27
28
# File 'lib/atig/http.rb', line 26

def server_name
  "twittergw"
end

#server_versionObject



30
31
32
33
34
35
# File 'lib/atig/http.rb', line 30

def server_version
  @server_version ||= instance_eval {
    head = `git rev-parse HEAD 2>/dev/null`.chomp
    head.empty?? "unknown" : head
  }
end