Class: Gorgeous

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgeous.rb

Constant Summary collapse

PRETTY =
File.expand_path('../pretty.xslt', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Gorgeous

Returns a new instance of Gorgeous.



97
98
99
100
101
# File 'lib/gorgeous.rb', line 97

def initialize(input, options = {})
  @input = input
  @format = options[:format]
  @options = options
end

Class Method Details

.convert_utf8(string, from_charset) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/gorgeous.rb', line 19

def self.convert_utf8(string, from_charset)
  if from_charset.nil? or from_charset.downcase.tr('-', '') == 'utf8'
    string
  else
    require 'iconv'
    Iconv.conv 'utf-8', from_charset, string
  end
end

.filename_to_format(filename) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/gorgeous.rb', line 2

def self.filename_to_format(filename)
  case File.extname(filename)
  when '.json' then :json
  when '.xml', '.html' then :xml
  when '.rb' then :ruby
  when '.yml', '.yaml' then :yaml
  when '.mail', '.email' then :email
  end
end

.headers_from_mail(email) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gorgeous.rb', line 28

def self.headers_from_mail(email)
  require 'active_support/ordered_hash'
  require 'active_support/core_ext/object/blank'

  address_field = lambda { |name|
    if field = email.header[name]
      values = field.addrs.map { |a|
        Mail::Encodings.unquote_and_convert_to(a.format, 'utf-8')
      }
      values.size < 2 ? values.first : values
    end
  }
  header_value = lambda { |name|
    field = email.header[name]
    if Array === field
      field.map { |f| f.value.to_s }
    elsif field
      field.value.to_s
    end
  }
  decoded_value = lambda { |name|
    field = email.header[name]
    Mail::Encodings.unquote_and_convert_to(field.value, 'utf-8') if field
  }

  data = ActiveSupport::OrderedHash.new
  data[:subject] = decoded_value['subject']
  data[:from] = address_field['from']
  data[:to] = address_field['to']
  data[:cc] = address_field['cc']
  data[:bcc] = address_field['bcc']
  data[:reply_to] = address_field['reply-to']
  data[:return_path] = email.return_path

  data[:message_id] = email.message_id
  data[:in_reply_to] = email.in_reply_to
  data[:references] = email.references

  data[:date] = email.date
  data[:sender] = address_field['sender']
  data[:delivered_to] = header_value['delivered-to']
  data[:original_sender] = header_value['x-original-sender']
  data[:content_type] = email.content_type.to_s.split(';', 2).first.presence
  data[:precedence] = header_value['precedence']

  data.tap { |hash| hash.reject! { |k,v| v.nil? } }
end

.http_from_string(raw_response) ⇒ Object

adapted from webmock



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gorgeous.rb', line 77

def self.http_from_string(raw_response)
  if raw_response.is_a?(IO)
    string = raw_response.read
    raw_response.close
    raw_response = string
  end
  socket = ::Net::BufferedIO.new(raw_response)
  response = ::Net::HTTPResponse.read_new(socket)
  transfer_encoding = response.delete('transfer-encoding') # chunks were already read
  response.reading_body(socket, true) {}

  options = {}
  options[:headers] = {}
  response.each_header { |name, value| options[:headers][name] = value }
  options[:headers]['transfer-encoding'] = transfer_encoding if transfer_encoding
  options[:body] = response.read_body
  options[:status] = [response.code.to_i, response.message]
  options
end

.pretty_xml(ugly) ⇒ Object



14
15
16
17
# File 'lib/gorgeous.rb', line 14

def self.pretty_xml(ugly)
  tidy = Nokogiri::XSLT File.read(PRETTY)
  tidy.transform(ugly).to_s
end

Instance Method Details

#dataObject

typically hash or array



122
123
124
125
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
# File 'lib/gorgeous.rb', line 122

def data
  apply_query case format
  when :xml
    require 'active_support/core_ext/hash/conversions'
    Hash.from_xml(to_s)
  when :json
    begin
      require 'yajl/json_gem'
    rescue LoadError
      require 'json'
    end
    JSON.parse to_s
  when :yaml
    require 'yaml'
    YAML.load to_s
  when :email
    self.class.headers_from_mail to_mail
  when :ruby
    eval to_s # TODO: sandbox
  when :url
    require 'rack/utils'
    Rack::Utils.parse_nested_query(to_s.strip)
  when :http
    require 'net/http'
    self.class.http_from_string(to_s)[:headers]
  else
    raise ArgumentError, "don't know how to decode #{format}"
  end
end

#filtered?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/gorgeous.rb', line 103

def filtered?
  !!@options[:query]
end

#formatObject



111
112
113
114
115
116
117
118
119
# File 'lib/gorgeous.rb', line 111

def format
  @format ||= begin
    if @options[:filename]
      self.class.filename_to_format(@options[:filename])
    else
      guess_format
    end
  end
end

#to_mailObject



157
158
159
160
161
162
# File 'lib/gorgeous.rb', line 157

def to_mail
  require 'mail'
  raw = to_s.lstrip
  raw << "\n" unless raw[-1, 1] == "\n"
  Mail.new raw
end

#to_sObject



107
108
109
# File 'lib/gorgeous.rb', line 107

def to_s
  @str ||= @input.respond_to?(:read) ? @input.read : @input
end

#to_xmlObject



152
153
154
155
# File 'lib/gorgeous.rb', line 152

def to_xml
  require 'nokogiri'
  Nokogiri to_s
end