Class: AdaptivePay::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/adaptive_pay/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Request

Returns a new instance of Request.

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
# File 'lib/adaptive_pay/request.rb', line 33

def initialize(&block)
  @attributes = {}
  self.class.attributes.each do |k|
    write_attribute k[:name], k[:default] if k[:default]
  end
  yield self if block_given?
end

Class Method Details

.attribute(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/adaptive_pay/request.rb', line 15

def self.attribute(name, options={})
  top_level_name = name.to_s.split(".").last.underscore
  define_method top_level_name do
    read_attribute name
  end

  define_method "#{top_level_name}=" do |value|
    write_attribute name, value
  end

  self.attributes << options.merge(:name => name)
end

.attribute_namesObject



28
29
30
# File 'lib/adaptive_pay/request.rb', line 28

def self.attribute_names
  self.attributes.map { |a| a[:name] }
end

.response_typeObject



8
9
10
# File 'lib/adaptive_pay/request.rb', line 8

def self.response_type
  :other
end

Instance Method Details

#format_attribute(name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adaptive_pay/request.rb', line 45

def format_attribute(name)
  attrib = self.class.attributes.find { |a| a[:name] == name }
  case
  when attrib.nil?
    nil
  when attrib[:format] == :date
    @attributes[name].strftime("%Y-%m-%d")
  else
    @attributes[name]
  end
end

#perform(interface) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/adaptive_pay/request.rb', line 62

def perform(interface)
  uri = construct_uri(interface)
  request = Net::HTTP::Post.new uri.request_uri
  request.body = serialize
  request.initialize_http_header(headers(interface))
  http_response = build_http(uri).request request
  Response.new interface, self.class.response_type, http_response
end

#read_attribute(name) ⇒ Object



41
42
43
# File 'lib/adaptive_pay/request.rb', line 41

def read_attribute(name)
  @attributes[name]
end

#serializeObject



71
72
73
74
75
76
77
# File 'lib/adaptive_pay/request.rb', line 71

def serialize
  result = []
  all_attributes.each do |k, v|
    result << "#{k}=#{URI.escape(v.to_s)}"
  end
  result.join("&")
end

#write_attribute(name, value) ⇒ Object



57
58
59
# File 'lib/adaptive_pay/request.rb', line 57

def write_attribute(name, value)
  @attributes[name] = value
end