Class: GMO::PG::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/gmo-pg/http_resource/payload.rb,
lib/gmo-pg/http_resource/payload/typecast.rb

Direct Known Subclasses

GenericRequest, GenericResponse

Defined Under Namespace

Modules: Typecast Classes: TypecastableEpochTime, TypecastableInteger, TypecastableValue

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Payload

Returns a new instance of Payload.



14
15
16
17
18
19
# File 'lib/gmo-pg/http_resource/payload.rb', line 14

def initialize(attributes = {})
  @attributes = {}
  attributes.each do |name, value|
    self[name] = value
  end
end

Class Method Details

.decode(body) ⇒ Object



10
11
12
# File 'lib/gmo-pg/http_resource/payload.rb', line 10

def self.decode(body)
  Hash[URI.decode_www_form(body)]
end

.encode(payload) ⇒ Object



6
7
8
# File 'lib/gmo-pg/http_resource/payload.rb', line 6

def self.encode(payload)
  URI.encode_www_form(payload)
end

Instance Method Details

#[](name) ⇒ Object



26
27
28
29
30
# File 'lib/gmo-pg/http_resource/payload.rb', line 26

def [](name)
  param_name = self.class.detect_param_name(name)
  return unless @attributes.key?(param_name)
  @attributes[param_name].to_attribute
end

#[]=(name, value) ⇒ Object



21
22
23
24
# File 'lib/gmo-pg/http_resource/payload.rb', line 21

def []=(name, value)
  param_name, _, options = self.class.detect_bind_attribute(name)
  @attributes[param_name] = Typecast.detect(options[:typecast]).new(value)
end

#inspectObject



44
45
46
47
48
49
50
# File 'lib/gmo-pg/http_resource/payload.rb', line 44

def inspect
  '#<%s:%014x "%s">' % [
    self.class.name,
    object_id << 1, # @see http://stackoverflow.com/questions/2818602/in-ruby-why-does-inspect-print-out-some-kind-of-object-id-which-is-different
    self.class.encode(payload),
  ]
end

#payloadObject



32
33
34
35
36
# File 'lib/gmo-pg/http_resource/payload.rb', line 32

def payload
  @attributes.each_with_object({}) do |(param_name, value), payload|
    payload[param_name] = value.to_payload
  end
end

#to_hashObject



38
39
40
41
42
# File 'lib/gmo-pg/http_resource/payload.rb', line 38

def to_hash
  @attributes.each_with_object({}) do |(param_name, value), hash|
    hash[self.class.detect_attribute_name(param_name)] = value.to_attribute
  end
end