Class: LWAC::DataPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/lwac/shared/data_types.rb

Overview


Holds a datapoint, which is the return value from querying a link Immutable.

Constant Summary collapse

INTERESTING_CURL_EASY_METHODS =

Methods to extract from Curl::Easy

%w{
    body_str
    cacert
    cert
    cert_key
    certtype
    connect_time
    connect_timeout
    content_type
    cookiefile
    cookiejar
    cookies
    dns_cache_timeout
    download_speed
    downloaded_bytes
    downloaded_content_length
    enable_cookies?
    encoding
    fetch_file_time?
    file_time
    follow_location?
    ftp_commands
    ftp_entry_path
    ftp_filemethod
    ftp_response_timeout
    header_in_body?
    header_size
    header_str
    headers
    http_auth_types
    http_connect_code
    ignore_content_length?
    interface
    last_effective_url
    local_port
    local_port_range
    low_speed_limit
    low_speed_time
    max_redirects
    multipart_form_post?
    name_lookup_time
    num_connects
    os_errno
    password
    post_body
    pre_transfer_time
    primary_ip
    proxy_auth_types
    proxy_port
    proxy_tunnel?
    proxy_type
    proxy_url
    proxypwd
    redirect_count
    redirect_time
    redirect_url
    request_size
    resolve_mode
    response_code
    ssl_verify_host
    ssl_verify_peer?
    ssl_version
    start_transfer_time
    status
    timeout
    total_time
    unrestricted_auth?
    upload_speed
    uploaded_bytes
    uploaded_content_length
    url
    use_netrc?
    use_ssl
    useragent
    username
    userpwd
    verbose?
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link, headers, head, body, response_properties, client_id, error = nil) ⇒ DataPoint

Returns a new instance of DataPoint.



95
96
97
98
99
100
101
102
103
104
# File 'lib/lwac/shared/data_types.rb', line 95

def initialize(link, headers, head, body, response_properties, client_id, error=nil)
  @link                 = link
  @headers              = headers
  @headers              = DataPoint.headers_to_hash(@headers) if not @headers.is_a?(Hash)
  @head                 = head
  @body                 = body
  @response_properties  = response_properties
  @error                = error
  @client_id            = client_id
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def body
  @body
end

#client_idObject (readonly)

Returns the value of attribute client_id.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def client_id
  @client_id
end

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def error
  @error
end

#headObject (readonly)

Returns the value of attribute head.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def head
  @head
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def headers
  @headers
end

Returns the value of attribute link.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def link
  @link
end

#response_propertiesObject (readonly)

Returns the value of attribute response_properties.



12
13
14
# File 'lib/lwac/shared/data_types.rb', line 12

def response_properties
  @response_properties
end

Class Method Details

.from_request(config, link, res, client_id, error) ⇒ Object

Converts a Curl result and an originating link into a datapoint with a standard character encoding, etc.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/lwac/shared/data_types.rb', line 126

def self.from_request(config, link, res, client_id, error)
  # DataPoint.new(link, headers, head, body, response_properties, @client_id, nil)
  require 'curl'  # 

  # Fix encoding of head if required
  $log.debug "Fixing header encoding..."
  body                = fix_encoding(res.body_str.to_s, config)

  # Fix encoding of head if required
  $log.debug "Fixing header encoding..."
  head                = fix_encoding(res.header_str.to_s, config)

  # Generate a hash of headers
  $log.debug "Parsing headers..."
  header_hash = DataPoint.headers_to_hash(head)


  # Per-regex MIME handling 
  $log.debug "Passing MIME filter in #{config[:mimes][:policy]} mode..."
  allow_mime = (config[:mimes][:policy] == :blacklist)
  encoding   = header_hash["Content-Type"].to_s
  config[:mimes][:list].each{|mime_rx|
    if encoding.to_s =~ Regexp.new(mime_rx, config[:mimes][:ignore_case]) then
      allow_mime = (config[:mimes][:policy] == :whitelist)
      $log.debug "Link #{link.id} matched MIME regex #{mime_rx}"
    end
  }
  body                  = '' unless allow_mime

  # Load stuff out of response object.
  $log.debug "Extracting #{INTERESTING_CURL_EASY_METHODS.length} details from result..."
  response_properties = {}
  INTERESTING_CURL_EASY_METHODS.map { |m| response_properties[m.to_sym] = res.send(m.to_sym) }
  response_properties[:mime_allowed] = allow_mime

  DataPoint.new(link,
                header_hash,
                head,
                body,
                response_properties,
                client_id,
                error
               )
end

.headers_to_hash(header_string) ⇒ Object

Turns HTTP headers into a ruby hash, by parsing them as a string



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lwac/shared/data_types.rb', line 112

def self.headers_to_hash(header_string)
  headers = {}
  header_string.each_line do |ln|
    if ln.index(':')
      key = ln[0..(ln.index(':') - 1)].strip
      val = ln[(ln.index(':') + 1)..-1].strip
      headers[key] = val
    end
  end
  return headers
end

Instance Method Details

#to_sObject



106
107
108
# File 'lib/lwac/shared/data_types.rb', line 106

def to_s
  "<DataPoint #{@link.to_s}>"
end