Module: MediumApi::Utils

Defined in:
lib/medium_api/utils.rb

Class Method Summary collapse

Class Method Details

.camelcase(string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/medium_api/utils.rb', line 34

def camelcase(string)
    # alternative method
    # first_word, *words = string.split('_')
    
    # [first_word, *words.map(&:capitalize)].join('')

    words = string.split('_')
    capitalized_words = words[1..-1].map(&:capitalize)

    [words.first, capitalized_words].join('')
end

.camelcase_keys(hash) ⇒ Object

new_hash = value

end

end



27
28
29
30
31
32
# File 'lib/medium_api/utils.rb', line 27

def camelcase_keys(hash)
    hash.transform_keys do |key|
        new_key = camelcase(key.to_s)
        key.is_a?(Symbol) ? new_key.to_sym : new_key
    end
end

.underscore(string) ⇒ Object



14
15
16
# File 'lib/medium_api/utils.rb', line 14

def underscore(string)
    string.gsub(/([A-Z])/, '_\1').downcase
end

.underscore_keys(hash) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/medium_api/utils.rb', line 5

def underscore_keys(hash)
    hash.each_with_object({}) do |(key, value), new_hash|
        new_key = underscore(key.to_s)
        new_key = new_key.to_sym if key.is_a?(Symbol)

        new_hash[new_key] = value
    end            
end