Class: Avm::Entries::UriBuilder
- Inherits:
-
Object
- Object
- Avm::Entries::UriBuilder
- Defined in:
- lib/avm/entries/uri_builder.rb
Constant Summary collapse
- FIELDS =
%w[scheme user password host port path query fragment].freeze
- FIELDS_TRANSLATIONS =
{ username: :user, hostname: :host }.freeze
- ENTRIES_FIELDS =
FIELDS.map do |field| FIELDS_TRANSLATIONS.key(field.to_sym).if_present(field).to_s end
Class Method Summary collapse
- .from_all_fields(&block) ⇒ Object
- .from_source(source) ⇒ Object
- .from_source_uri(source_uri) ⇒ Object
Instance Method Summary collapse
- #avm_field_get(avm_field) ⇒ Object
- #field_get(name) ⇒ String?
- #field_set(field, value) ⇒ Object
- #to_required_uri ⇒ Addressable::URI
- #to_uri ⇒ Addressable::URI?
- #translate_field(field) ⇒ Object
Class Method Details
.from_all_fields(&block) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/avm/entries/uri_builder.rb', line 19 def from_all_fields(&block) r = new ENTRIES_FIELDS.each do |field_name| field_value = block.call(field_name) r.field_set(field_name, field_value) if field_value.present? end r end |
.from_source(source) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/avm/entries/uri_builder.rb', line 28 def from_source(source) if source.is_a?(::Addressable::URI) from_source_uri(source) elsif source.blank? new else raise "Unexpected source: #{source}" end end |
.from_source_uri(source_uri) ⇒ Object
38 39 40 |
# File 'lib/avm/entries/uri_builder.rb', line 38 def from_source_uri(source_uri) new(source_uri.to_hash) end |
Instance Method Details
#avm_field_get(avm_field) ⇒ Object
45 46 47 |
# File 'lib/avm/entries/uri_builder.rb', line 45 def avm_field_get(avm_field) field_get(translate_field(avm_field)) end |
#field_get(name) ⇒ String?
50 51 52 53 |
# File 'lib/avm/entries/uri_builder.rb', line 50 def field_get(name) v = data[name.to_sym] v&.to_s end |
#field_set(field, value) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/avm/entries/uri_builder.rb', line 55 def field_set(field, value) tfield = translate_field(field) if FIELDS.include?(tfield) main_field_set(tfield, value) else sub_field_set(tfield, value) end end |
#to_required_uri ⇒ Addressable::URI
80 81 82 |
# File 'lib/avm/entries/uri_builder.rb', line 80 def to_required_uri ::Addressable::URI.new(FIELDS.to_h { |f| [f.to_sym, field_get(f)] }) end |
#to_uri ⇒ Addressable::URI?
73 74 75 76 77 |
# File 'lib/avm/entries/uri_builder.rb', line 73 def to_uri to_required_uri rescue ::Addressable::URI::InvalidURIError nil end |
#translate_field(field) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/avm/entries/uri_builder.rb', line 64 def translate_field(field) if FIELDS_TRANSLATIONS.key?(field.to_sym) FIELDS_TRANSLATIONS.fetch(field.to_sym).to_s else field.to_s end end |