Class: SecretService::Secret
- Inherits:
-
Object
- Object
- SecretService::Secret
- Defined in:
- lib/secret_service/secret.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#session_path ⇒ Object
Returns the value of attribute session_path.
Class Method Summary collapse
-
.content_type_to_encoding(content_type = nil) ⇒ Object
Derive the Ruby String encoding from a given Content-type.
-
.from_struct(s) ⇒ Object
Create a new SecretService::Secret from the DBus Struct format.
Instance Method Summary collapse
-
#initialize(attrs) ⇒ Secret
constructor
A new instance of Secret.
-
#sniff_content_type(str) ⇒ Object
Take a string, guess what the Content-type should be.
-
#to_struct ⇒ Object
Convert the human-readable version into the format expected by DBus.
- #value ⇒ Object
- #value=(val) ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Secret
Returns a new instance of Secret.
9 10 11 12 13 14 |
# File 'lib/secret_service/secret.rb', line 9 def initialize attrs @session_path = attrs[:session_path] @parameters = attrs[:parameters] || [] @value = attrs[:value] @content_type = attrs[:content_type] || sniff_content_type(@value) end |
Instance Attribute Details
#content_type ⇒ Object
Returns the value of attribute content_type.
7 8 9 |
# File 'lib/secret_service/secret.rb', line 7 def content_type @content_type end |
#parameters ⇒ Object
Returns the value of attribute parameters.
7 8 9 |
# File 'lib/secret_service/secret.rb', line 7 def parameters @parameters end |
#session_path ⇒ Object
Returns the value of attribute session_path.
7 8 9 |
# File 'lib/secret_service/secret.rb', line 7 def session_path @session_path end |
Class Method Details
.content_type_to_encoding(content_type = nil) ⇒ Object
Derive the Ruby String encoding from a given Content-type. When all else fails, assumes ASCII-8BIT (aka Binary)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/secret_service/secret.rb', line 60 def self.content_type_to_encoding content_type=nil binary = "ASCII-8BIT" begin case charset = content_type.match(/; charset=(.+)\z/)[1].upcase when /\AUTF[^-]/ # "UTF8" is a common misspelling of "UTF-8". charset.sub! /\AUTF/, "UTF-" when /./ # Accept charset.upcase at face value & hope for the best charset else # This is a technical violation of RFC 2616, sec. 3.7.1, which # states ISO-8859-1 is the default for "text/*" when no # charset is specified. However, I claim UTF-8 is a safer # assumption on the modern Internet. charset = content_type.match(/\Atext\//) ? "UTF-8" : binary end "".encode charset charset rescue binary end end |
.from_struct(s) ⇒ Object
Create a new SecretService::Secret from the DBus Struct format
26 27 28 29 30 31 32 |
# File 'lib/secret_service/secret.rb', line 26 def self.from_struct s initialize(:session_path => s[0][0], :parameters => s[0][1], :value => (s[0][2].map {|x| x.chr}).join, :content_type => (s[0][3] == "") ? nil : s[0][3] ) end |
Instance Method Details
#sniff_content_type(str) ⇒ Object
Take a string, guess what the Content-type should be. For Ruby < 1.9, assume binary encoding. You can avoid this by passing in an explicit content_type to the initializer.
Or, y’know, using a more recent version of the language.
48 49 50 51 52 53 54 55 56 |
# File 'lib/secret_service/secret.rb', line 48 def sniff_content_type str if (str.nil? or (not str.respond_to? :encoding ) or (str.encoding.to_s == "ASCII-8BIT")) "application/octet-stream" else "text/plain; charset=#{str.encoding}" end end |
#to_struct ⇒ Object
Convert the human-readable version into the format expected by DBus.
36 37 38 39 40 |
# File 'lib/secret_service/secret.rb', line 36 def to_struct [ "(oayays)", [@session_path, @parameters, @value.bytes.to_a, @content_type] ] end |
#value ⇒ Object
16 17 18 |
# File 'lib/secret_service/secret.rb', line 16 def value @value end |
#value=(val) ⇒ Object
20 21 22 23 |
# File 'lib/secret_service/secret.rb', line 20 def value= val @value = val @content_type = sniff_content_type attrs[:value] end |