Class: Github::ParamsHash

Inherits:
Hash
  • Object
show all
Includes:
MimeType, Normalizer
Defined in:
lib/github_api2/params_hash.rb

Overview

Class responsible for holding request parameters

Constant Summary collapse

REQUEST_PARAMS =
[:accept, :media, :data, :raw, :headers]

Constants included from MimeType

MimeType::MEDIA_LOOKUP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MimeType

#lookup_media, #parse

Methods included from Normalizer

#normalize!

Methods inherited from Hash

#deep_key?, #deep_keys, #deep_merge, #deep_merge!, #except, #except!, #serialize, #symbolize_keys, #symbolize_keys!

Constructor Details

#initialize(hash) ⇒ ParamsHash

Returns a new instance of ParamsHash.



17
18
19
# File 'lib/github_api2/params_hash.rb', line 17

def initialize(hash)
  super(normalize!(Hash[hash]))
end

Class Method Details

.emptyObject

Create empty hash



24
25
26
# File 'lib/github_api2/params_hash.rb', line 24

def self.empty
  new({})
end

Instance Method Details

#acceptObject

Get accept header



40
41
42
43
44
45
# File 'lib/github_api2/params_hash.rb', line 40

def accept
  if key?('accept') then self['accept']
  elsif key?('media') then media
  else nil
  end
end

#dataObject

Extract request data from parameters



50
51
52
53
54
55
56
# File 'lib/github_api2/params_hash.rb', line 50

def data
  if key?('data') && !self['data'].nil?
    self['data']
  else
    request_params
  end
end

#encoderObject



58
59
60
61
62
63
64
# File 'lib/github_api2/params_hash.rb', line 58

def encoder
  if key?('encoder') && self['encoder']
    self['encoder']
  else
    {}
  end
end

#mediaObject

Extract and parse media type param

[.version].param[+json]


33
34
35
# File 'lib/github_api2/params_hash.rb', line 33

def media
  parse(delete('media'))
end

#merge_default(defaults) ⇒ Object

Update hash with default parameters for non existing keys



84
85
86
87
88
89
90
91
# File 'lib/github_api2/params_hash.rb', line 84

def merge_default(defaults)
  if defaults && !defaults.empty?
    defaults.each do |key, value|
      self[key] = value unless self.key?(key)
    end
  end
  self
end

#optionsHash

Configuration options from request

Returns:



71
72
73
74
75
76
77
78
79
80
# File 'lib/github_api2/params_hash.rb', line 71

def options
  opts    = fetch('options', {})
  headers = fetch('headers', {})
  if value = accept
    headers[:accept] = value
  end
  opts[:raw]     = key?('raw') ? self['raw'] : false
  opts[:headers] = headers unless headers.empty?
  opts
end

#request_paramsObject

Filter out request params



109
110
111
112
113
# File 'lib/github_api2/params_hash.rb', line 109

def request_params
  to_hash.select do |key, value|
    !REQUEST_PARAMS.include?(key.to_sym)
  end
end

#strict_encode64(key) ⇒ Object

Base64 encode string removing newline characters



96
97
98
99
100
101
102
103
104
# File 'lib/github_api2/params_hash.rb', line 96

def strict_encode64(key)
  value = self[key]
  encoded = if Base64.respond_to?(:strict_encode64)
              Base64.strict_encode64(value)
            else
              [value].pack('m0')
            end
  self[key] = encoded.delete("\n\r")
end