Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ updates:
open-pull-requests-limit: 10
cooldown:
default-days: 7
- package-ecosystem: bundler
directory: /components/serialization/text
Comment thread
Copilot marked this conversation as resolved.
schedule:
interval: daily
open-pull-requests-limit: 10
cooldown:
default-days: 7
- package-ecosystem: bundler
directory: /components/http
schedule:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
component:
- { name: serialization-json, dir: components/serialization/json }
- { name: serialization-form, dir: components/serialization/form }
- { name: serialization-text, dir: components/serialization/text }
- { name: http, dir: components/http }
- { name: authentication-oauth, dir: components/authentication/oauth }
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- { name: abstractions, dir: components/abstractions }
- { name: serialization-json, dir: components/serialization/json }
- { name: serialization-form, dir: components/serialization/form }
- { name: serialization-text, dir: components/serialization/text }
- { name: http, dir: components/http }
- { name: authentication-oauth, dir: components/authentication/oauth }
runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'microsoft_kiota_authentication_oauth', path: 'components/authentication/oau
gem 'microsoft_kiota_faraday', path: 'components/http'
gem 'microsoft_kiota_serialization_form', path: 'components/serialization/form'
gem 'microsoft_kiota_serialization_json', path: 'components/serialization/json'
gem 'microsoft_kiota_serialization_text', path: 'components/serialization/text'

gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Ruby 3.3 or newer is required.
| [microsoft_kiota_abstractions](components/abstractions/) | Core abstractions for Kiota-generated clients | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_abstractions.svg)](https://rubygems.org/gems/microsoft_kiota_abstractions) |
| [microsoft_kiota_serialization_json](components/serialization/json/) | JSON serialization implementation | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_serialization_json.svg)](https://rubygems.org/gems/microsoft_kiota_serialization_json) |
| [microsoft_kiota_serialization_form](components/serialization/form/) | Form serialization implementation | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_serialization_form.svg)](https://rubygems.org/gems/microsoft_kiota_serialization_form) |
| [microsoft_kiota_serialization_text](components/serialization/text/) | Text serialization implementation | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_serialization_text.svg)](https://rubygems.org/gems/microsoft_kiota_serialization_text) |
| [microsoft_kiota_faraday](components/http/) | HTTP client implementation with Faraday | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_faraday.svg)](https://rubygems.org/gems/microsoft_kiota_faraday) |
| [microsoft_kiota_authentication_oauth](components/authentication/oauth/) | OAuth authentication provider | [![Gem Version](https://badge.fury.io/rb/microsoft_kiota_authentication_oauth.svg)](https://rubygems.org/gems/microsoft_kiota_authentication_oauth) |

Expand All @@ -26,6 +27,7 @@ Add the gems you need to your application's Gemfile:
gem 'microsoft_kiota_abstractions'
gem 'microsoft_kiota_serialization_json'
gem 'microsoft_kiota_serialization_form'
gem 'microsoft_kiota_serialization_text'
gem 'microsoft_kiota_faraday'
gem 'microsoft_kiota_authentication_oauth'
```
Expand All @@ -48,6 +50,7 @@ To run tests for a specific component:
rake abstractions:spec
rake serialization-json:spec
rake serialization-form:spec
rake serialization-text:spec
rake http:spec
rake authentication-oauth:spec
```
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ COMPONENTS = {
'abstractions' => 'components/abstractions',
'serialization-json' => 'components/serialization/json',
'serialization-form' => 'components/serialization/form',
'serialization-text' => 'components/serialization/text',
'http' => 'components/http',
'authentication-oauth' => 'components/authentication/oauth'
}.freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def string
end
iso_str = iso_str.strip
iso_str = iso_str.chomp('T') if iso_str[-1].eql? 'T'
iso_str
iso_str == 'P' ? 'PT0S' : iso_str
end

def normalize
Expand Down Expand Up @@ -182,7 +182,7 @@ def parse_hash(input)
end
iso_str = iso_str.strip
iso_str = iso_str.chomp('T') if iso_str[-1].eql? 'T'
ISO8601::Duration.new(iso_str)
ISO8601::Duration.new(iso_str == 'P' ? 'PT0S' : iso_str)
end

def update_member_variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
expect(time3.string).to eq(MicrosoftKiotaAbstractions::ISODuration.new('P4Y2MT4H').string)
end

it 'writes and builds a zero duration as PT0S' do
expect(MicrosoftKiotaAbstractions::ISODuration.new('PT0S').string).to eq('PT0S')
expect(MicrosoftKiotaAbstractions::ISODuration.new({ seconds: 0 }).string).to eq('PT0S')
expect((MicrosoftKiotaAbstractions::ISODuration.new('PT2H') - MicrosoftKiotaAbstractions::ISODuration.new('PT2H')).string).to eq('PT0S')
end

it 'handles subtraction' do
time1 = MicrosoftKiotaAbstractions::ISODuration.new('P4Y2MT2H')
time2 = MicrosoftKiotaAbstractions::ISODuration.new({ years: 1, months: 1, hours: 2 })
Expand Down
11 changes: 11 additions & 0 deletions components/serialization/text/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

source 'https://rubygems.org'

ruby '>= 3.3.0'

gem 'microsoft_kiota_abstractions', path: '../../abstractions'
gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'

gemspec
8 changes: 8 additions & 0 deletions components/serialization/text/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require_relative 'microsoft_kiota_serialization_text/text_parse_node'
require_relative 'microsoft_kiota_serialization_text/text_parse_node_factory'
require_relative 'microsoft_kiota_serialization_text/text_serialization_writer'
require_relative 'microsoft_kiota_serialization_text/text_serialization_writer_factory'
require_relative 'microsoft_kiota_serialization_text/version'

module MicrosoftKiotaSerializationText
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module MicrosoftKiotaSerializationText
# bytes are read as UTF-8; a string Faraday tagged with its charset is converted
def self.to_utf8(text)
text.encoding == Encoding::BINARY ? text.dup.force_encoding(Encoding::UTF_8) : text.encode(Encoding::UTF_8)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# frozen_string_literal: true

require 'time'
require 'date'
require 'uuidtools'
require 'microsoft_kiota_abstractions'

module MicrosoftKiotaSerializationText
class TextParseNode
include MicrosoftKiotaAbstractions::ParseNode

NO_STRUCTURED_DATA_MESSAGE = 'Text does not support structured data'

# temporal readers take only their own shape, as in the form gem, so a union picks the right member
DATE = /\A\d{4}-\d{2}-\d{2}\z/
TIME = /\A\d{2}:\d{2}(:\d{2}(\.\d+)?)?\z/
DATE_TIME = /\A\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?([Zz]|[+-]\d{2}(:?\d{2})?)?\z/

def initialize(text)
@text = text
end

def get_string_value
@text unless @text.nil? || @text.empty?
end

def get_boolean_value
case scalar_text&.downcase
when 'true' then true
when 'false' then false
end
end

def get_number_value
text = scalar_text
Integer(text, 10, exception: false) if text
end

def get_float_value
text = scalar_text
Float(text, exception: false) if text
end

def get_guid_value
parsed { |text| UUIDTools::UUID.parse(text) }
end

def get_date_value
parsed { |text| Date.parse(text) if DATE.match?(text) }
end

def get_time_value
parsed { |text| Time.parse(text) if TIME.match?(text) }
end

def get_date_time_value
parsed { |text| DateTime.parse(text) if DATE_TIME.match?(text) }
end

def get_duration_value
parsed { |text| MicrosoftKiotaAbstractions::ISODuration.new(text) }
end

def get_byte_array_value
scalar_text&.unpack1('m')
end

def get_enum_value(type)
text = scalar_text
resolve_enum_member(type, text) if text
end

def get_child_node(_name)
raise StandardError, NO_STRUCTURED_DATA_MESSAGE
end

def get_collection_of_primitive_values(_type)
raise StandardError, NO_STRUCTURED_DATA_MESSAGE
end

def get_collection_of_object_values(_factory)
raise StandardError, NO_STRUCTURED_DATA_MESSAGE
end

def get_collection_of_enum_values(_type)
raise StandardError, NO_STRUCTURED_DATA_MESSAGE
end

def get_object_value(_factory)
raise StandardError, NO_STRUCTURED_DATA_MESSAGE
end

private

# text that does not parse reads as nil, as .NET's TryParse and the form gem do
def parsed
text = scalar_text
yield text if text
rescue StandardError
nil
end

# typed values ignore surrounding whitespace, as .NET's TryParse does
def scalar_text
text = get_string_value&.strip
text unless text.nil? || text.empty?
end

# same resolution as JsonParseNode: the wire value first, then the member name
def resolve_enum_member(type, raw)
return raw.to_sym unless type.is_a?(Hash)

type.each_value { |value| return value if value.to_s == raw }
type[raw.to_sym] || type[(raw[0].to_s.upcase + raw[1..].to_s).to_sym]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'microsoft_kiota_abstractions'
require_relative 'encoding'

module MicrosoftKiotaSerializationText
class TextParseNodeFactory
include MicrosoftKiotaAbstractions::ParseNodeFactory

def get_valid_content_type
'text/plain'
end

def get_parse_node(content_type, content)
raise ArgumentError, 'content type cannot be undefined or empty' if content_type.nil? || content_type.empty?
raise ArgumentError, "expected a #{get_valid_content_type} content type" unless get_valid_content_type.casecmp?(content_type)
raise ArgumentError, 'content cannot be undefined or empty' if content.nil? || content.empty?

TextParseNode.new(MicrosoftKiotaSerializationText.to_utf8(content).delete_prefix("\uFEFF"))
end
end
end
Loading
Loading