Class: Github::ParamsHash
- 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
Class Method Summary collapse
-
.empty ⇒ Object
Create empty hash.
Instance Method Summary collapse
-
#accept ⇒ Object
Get accept header.
-
#data ⇒ Object
Extract request data from parameters.
- #encoder ⇒ Object
-
#initialize(hash) ⇒ ParamsHash
constructor
A new instance of ParamsHash.
-
#media ⇒ Object
Extract and parse media type param.
-
#merge_default(defaults) ⇒ Object
Update hash with default parameters for non existing keys.
-
#options ⇒ Hash
Configuration options from request.
-
#request_params ⇒ Object
Filter out request params.
-
#strict_encode64(key) ⇒ Object
Base64 encode string removing newline characters.
Methods included from MimeType
Methods included from Normalizer
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
.empty ⇒ Object
Create empty hash
24 25 26 |
# File 'lib/github_api2/params_hash.rb', line 24 def self.empty new({}) end |
Instance Method Details
#accept ⇒ Object
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 |
#data ⇒ Object
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 |
#encoder ⇒ Object
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 |
#media ⇒ Object
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 |
#options ⇒ Hash
Configuration options from request
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/github_api2/params_hash.rb', line 71 def 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_params ⇒ Object
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 |