Module: Dickless
- Defined in:
- lib/dickless.rb,
lib/dickless/types.rb,
lib/dickless/client.rb,
lib/dickless/errors.rb
Defined Under Namespace
Classes: ApiError, ChatChoice, ChatMessage, ChatResponse, ChatUsage, Client, CreditBalance, CreditTransaction, DetectedThreat, Error, ModerateResponse, ModerationCategory, OcrResponse, RedactResponse, RedactedEntity, RoastResponse, SanitizeResponse, ScreenshotResponse, SentimentResponse, ShortUrlStats, ShortenResponse, SummarizeResponse, TranslateResponse, ValidateResponse
Class Method Summary collapse
-
.camelize_to_snake(str) ⇒ Object
Convert a camelCase or snake_case string key to a Ruby-style snake_case symbol.
-
.define_from_hash(klass, &nested_block) ⇒ Object
Build a .from_hash class method on a Struct that maps camelCase keys to the struct’s snake_case members.
-
.new(**kwargs) ⇒ Object
Returns a new Client instance.
Class Method Details
.camelize_to_snake(str) ⇒ Object
Convert a camelCase or snake_case string key to a Ruby-style snake_case symbol. Examples: “overallScore” => :overall_score, “id” => :id
8 9 10 11 12 13 14 |
# File 'lib/dickless/types.rb', line 8 def self.camelize_to_snake(str) str.to_s .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase .to_sym end |
.define_from_hash(klass, &nested_block) ⇒ Object
Build a .from_hash class method on a Struct that maps camelCase keys to the struct’s snake_case members. Accepts an optional block for custom nested-object hydration.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dickless/types.rb', line 19 def self.define_from_hash(klass, &nested_block) klass.define_singleton_method(:from_hash) do |hash| return nil if hash.nil? mapped = {} hash.each do |key, value| snake = Dickless.camelize_to_snake(key) # Rename :end to :end_ since `end` is a reserved word in Ruby. snake = :end_ if snake == :end mapped[snake] = value if klass.members.include?(snake) end # Let the caller transform nested objects. nested_block&.call(mapped) klass.new(**mapped) end end |
.new(**kwargs) ⇒ Object
Returns a new Client instance. Convenience wrapper around Client.new.
client = Dickless.new(api_key: "dk_live_...")
12 13 14 |
# File 'lib/dickless.rb', line 12 def self.new(**kwargs) Client.new(**kwargs) end |