Class: Github::ParamsHash

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

Overview

Class responsible for holding request parameters

Constant Summary collapse

REQUEST_PARAMS =
[:accept, :media, :data, :raw, :content_type, :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.


14
15
16
# File 'lib/github_api/params_hash.rb', line 14

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

Class Method Details

.emptyObject

Create empty hash


21
22
23
# File 'lib/github_api/params_hash.rb', line 21

def self.empty
  new({})
end

Instance Method Details

#acceptObject

Get accept header


37
38
39
40
41
42
# File 'lib/github_api/params_hash.rb', line 37

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

#dataObject

Extract request data from parameters


47
48
49
50
51
52
53
# File 'lib/github_api/params_hash.rb', line 47

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

#encoderObject


55
56
57
58
59
60
61
# File 'lib/github_api/params_hash.rb', line 55

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

#mediaObject

Extract and parse media type param

[.version].param[+json]

30
31
32
# File 'lib/github_api/params_hash.rb', line 30

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_api/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:


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

def options
  opts    = fetch('options', {})
  headers = fetch('headers', {})
  if value = accept
    headers[:accept] = value
  end
  if self['content_type']
    headers[:content_type] = self['content_type']
  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_api/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_api/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