Class: W3cApi::Models::Translation

Inherits:
Base
  • Object
show all
Defined in:
lib/w3c_api/models/translation.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

transform_keys

Class Method Details

.from_response(response) ⇒ Object



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
# File 'lib/w3c_api/models/translation.rb', line 135

def self.from_response(response)
  transformed_response = transform_keys(response)

  translation = new
  transformed_response.each do |key, value|
    case key
    when :_links
      links = value.each_with_object({}) do |(link_name, link_data), acc|
        acc[link_name] = Link.new(href: link_data[:href], title: link_data[:title])
      end
      translation._links = TranslationLinks.new(links)
    when :call_for_translation
      translation.call_for_translation = CallForTranslationRef.new(value)
    when :translators
      # Handle translators as User objects if present
      if value.is_a?(Array)
        users = value.map { |user_data| User.from_response(user_data) }
        translation.translators = users
      end
    else
      translation.send("#{key}=", value) if translation.respond_to?("#{key}=")
    end
  end
  translation
end

Instance Method Details

#call_for_translation_objectObject

Get the call for translation as a CallForTranslation object



122
123
124
125
126
# File 'lib/w3c_api/models/translation.rb', line 122

def call_for_translation_object
  return nil unless call_for_translation

  Models::CallForTranslation.new(call_for_translation)
end

#published_dateObject

Parse date strings to Date objects



109
110
111
112
113
# File 'lib/w3c_api/models/translation.rb', line 109

def published_date
  Date.parse(published) if published
rescue Date::Error
  nil
end

#specification_version(client = nil) ⇒ Object

Get the specification version associated with this translation



129
130
131
132
133
# File 'lib/w3c_api/models/translation.rb', line 129

def specification_version(client = nil)
  return nil unless call_for_translation&.spec_version

  call_for_translation.spec_version
end

#updated_dateObject



115
116
117
118
119
# File 'lib/w3c_api/models/translation.rb', line 115

def updated_date
  Date.parse(updated) if updated
rescue Date::Error
  nil
end