Module: Polyseerio::Resource::Helper

Defined in:
lib/resource/helper.rb

Overview

Helper methods for building and working with resources

Constant Summary collapse

EID_REGEX =
Regexp.new('/environments/([\da-z\.-]+)/')

Class Method Summary collapse

Class Method Details

.forward_self(func) ⇒ Object

Forward self as first argument. Used for method procs.



8
9
10
11
12
# File 'lib/resource/helper.rb', line 8

def self.forward_self(func)
  proc do |*args|
    func.call(self, *args)
  end
end

.get_eid_from_resource_path(path) ⇒ Object

Given a request path an eid is returned or nil



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

def self.get_eid_from_resource_path(path)
  # TODO: consolidate with regex?
  if path.include? '/environments/name/'
    return path.split('/environments/name/').last
  end

  result = EID_REGEX.match path

  return result if result.nil?

  result[1]
end

.parse_resource_response(response, cid) ⇒ Object

Parse a resource response TODO: hard to unit-test (due to Factory.make)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/resource/helper.rb', line 63

def self.parse_resource_response(response, cid)
  body = response.body
  meta = body[:meta] || {}
  included = body[:included] || {}
  data = body[:data]

  eid = get_eid_from_resource_path response.request.uri.path

  # attach eid if we have attributes and an id
  # would need to occur for each?????
  data[:attributes][:eid] = eid if !eid.nil? && data.key?(:attributes)

  # if resource collection if so map return
  if resource_collection? data
    return data.map do |item|
      to_instance(item, meta, included, cid)
    end
  end

  # convert to instance
  to_instance(data, meta, included, cid)
end

.parse_response(response, cid) ⇒ Object

Parse a response



87
88
89
90
91
92
93
# File 'lib/resource/helper.rb', line 87

def self.parse_response(response, cid)
  if resource_response? response.body
    return parse_resource_response(response, cid)
  end

  response
end

.resource?(data) ⇒ Boolean

True if response data is a resource collection (alias)

Returns:

  • (Boolean)


40
41
42
# File 'lib/resource/helper.rb', line 40

def self.resource?(data)
  data.is_a? Hash
end

.resource_collection?(data) ⇒ Boolean

True if response data is a resource collection (alias)

Returns:

  • (Boolean)


35
36
37
# File 'lib/resource/helper.rb', line 35

def self.resource_collection?(data)
  data.is_a? Array
end

.resource_response?(body) ⇒ Boolean

True if the response is a resource based response

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/resource/helper.rb', line 29

def self.resource_response?(body)
  body.key?(:data) &&
    (resource?(body[:data]) || resource_collection?(body[:data]))
end

.to_instance(data, _meta, _included, cid) ⇒ Object

Convert parsed response data into an instance TODO: hard to unit-test (due to Factory.make)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/resource/helper.rb', line 46

def self.to_instance(data, _meta, _included, cid)
  type = data.fetch(:type, nil)
  id = data.fetch(:id, nil)
  attributes = data.fetch(:attributes, {})

  attributes[:id] = id

  # parse_response would only eve be called once the client has been
  # constructed meaning in order to get the class we simply need to
  # pass the type and cid. make is memoized on the type and cid args.
  resource = Factory.make(type, nil, cid)

  resource.new(attributes) # TODO: need to include meta
end