Class: Bluepay::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/bluepay/response.rb

Overview

bpdailyreport2 returns csv in body bp10emu returns 302, html body, details are in Location header stq returns query string in body

#data returns a unified merge of all the data sources, with tabular data under the key :rows as an array of OpenStruct instances

Constant Summary collapse

LOCATION =
'Location'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Response

Returns a new instance of Response.



14
15
16
# File 'lib/bluepay/response.rb', line 14

def initialize(http)
  @response = http
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



12
13
14
# File 'lib/bluepay/response.rb', line 12

def action
  @action
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/bluepay/response.rb', line 12

def response
  @response
end

Instance Method Details

#bodyObject



57
58
59
# File 'lib/bluepay/response.rb', line 57

def body
  response.body
end

#body_csvObject



34
35
36
37
# File 'lib/bluepay/response.rb', line 34

def body_csv
  require 'csv'
  CSV.parse(response.body, headers: true) rescue Array.new
end

#body_paramsObject



22
23
24
# File 'lib/bluepay/response.rb', line 22

def body_params
  URI.decode_www_form(response.body).sort.to_h rescue {}
end

#body_rowsObject



39
40
41
# File 'lib/bluepay/response.rb', line 39

def body_rows
  body_csv.each.map {|r| OpenStruct.new(r.to_h)}
end

#codeObject



53
54
55
# File 'lib/bluepay/response.rb', line 53

def code
  response.code.to_i
end

#dataObject



43
44
45
46
47
# File 'lib/bluepay/response.rb', line 43

def data
  params.to_h.merge(
    rows: body_rows
  )
end

#headersObject



61
62
63
# File 'lib/bluepay/response.rb', line 61

def headers
  response.to_hash
end

#location_query_paramsObject



18
19
20
# File 'lib/bluepay/response.rb', line 18

def location_query_params
  URI.decode_www_form(URI(response['Location']).query).sort.to_h rescue {}
end

#paramsObject



26
27
28
29
30
31
32
# File 'lib/bluepay/response.rb', line 26

def params
  location_query_params.merge(body_params).inject({}) { |memo, kv|
    k, v = kv
    memo[k.to_s.downcase.to_sym] = v
    memo
  }
end

#to_hObject



49
50
51
# File 'lib/bluepay/response.rb', line 49

def to_h
  data
end