class AssUpdater::HTTP

HTTP access to 1C services. @note 1C servise often unavaileble and {#get} fail on timeout

Constants

USER_AGENT_DEFAULT

Value of useragent controled 1C web server and server may return 403 error

Attributes

open_timeout[RW]

see Net::HTTP#open_timeout @note (see AssUpdater::HTTP)

proxy_options[RW]

see {#initialize} param @return [Hash]

read_timeout[RW]

see Net::HTTP#read_timeout @note (see AssUpdater::HTTP)

user_agent[RW]

(see USER_AGENT_DEFAULT) @return [String]

Public Class Methods

new(proxy_options = {}) click to toggle source

@param proxy_options [Hash] options for proxy server

# File lib/ass_updater/http.rb, line 23
def initialize(proxy_options = {})
  self.open_timeout = 30 # fucking 1C
  self.user_agent = USER_AGENT_DEFAULT
  self.proxy_options = { addr: nil,
                         port: nil,
                         user: nil,
                         pass: nil }.merge(proxy_options)
  yeld self if block_given?
end

Public Instance Methods

get(uri_str, user_name = nil, password = nil) click to toggle source

@note (see AssUpdater::HTTP) @param uri_str [String] @param user_name [String] user 1C sevice @param password [String]

# File lib/ass_updater/http.rb, line 37
def get(uri_str, user_name = nil, password = nil)
  response = _http(URI(uri_str)).request(_get(user_name,
                                              password,
                                              URI(uri_str)
                                             )
                                        )
  _body(response, uri_str)
end

Private Instance Methods

_body(response, uri_str) click to toggle source
# File lib/ass_updater/http.rb, line 48
def _body(response, uri_str)
  if response.code != '200'
    fail AssUpdater::Error,
         "#{response.code} #{response.message} for `#{uri_str}'"
  end
  response.body
end
_get(user, pass, uri) click to toggle source
# File lib/ass_updater/http.rb, line 73
def _get(user, pass, uri)
  g = Net::HTTP::Get.new(uri.path, 'User-Agent' => user_agent)
  g.basic_auth user, pass if user
  g
end
_http(uri) click to toggle source
# File lib/ass_updater/http.rb, line 56
def _http(uri)
  h = Net::HTTP.new(uri.host,
                    uri.port,
                    proxy_options[:addr],
                    proxy_options[:port],
                    proxy_options[:user],
                    proxy_options[:pass]
                   )
  timeouts h
end
timeouts(h) click to toggle source
# File lib/ass_updater/http.rb, line 67
def timeouts(h)
  h.open_timeout = open_timeout
  h.read_timeout = read_timeout
  h
end