Class: BridgeAPI::ApiArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bridge_api/api_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, api_client, result_mapping) ⇒ ApiArray

Returns a new instance of ApiArray.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bridge_api/api_array.rb', line 15

def initialize(response, api_client, result_mapping)
  @meta_fields = %w[meta linked]
  @api_client = api_client
  @linked = {}
  @meta = {}
  @extra_meta_fields = []
  pattern = /.*(\/api\/.*)/
  path = response.env.url
  matches = pattern.match(path.to_s)
  mapping = nil
  if matches
    mapping_exists = result_mapping.key?(matches[1])
    mapping = result_mapping[matches[1]] if mapping_exists
  end
  @extra_meta_fields.concat(mapping[:meta]) unless mapping.nil?
  case response.status
  when *((200..206).to_a + [302])
    (response)
    @members = get_response_content(response)
  else
    set_response_vars(response)
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def body
  @body
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def errors
  @errors
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def headers
  @headers
end

#linkedObject (readonly)

Returns the value of attribute linked.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def linked
  @linked
end

#membersObject (readonly)

Returns the value of attribute members.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def members
  @members
end

#metaObject (readonly)

Returns the value of attribute meta.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def meta
  @meta
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/bridge_api/api_array.rb', line 9

def status
  @status
end

Class Method Details

.process_response(response, api_client, result_mapping) ⇒ Object



11
12
13
# File 'lib/bridge_api/api_array.rb', line 11

def self.process_response(response, api_client, result_mapping)
  ApiArray.new(response, api_client, result_mapping)
end

Instance Method Details

#[](i) ⇒ Object



54
55
56
# File 'lib/bridge_api/api_array.rb', line 54

def [](i)
  @members[i]
end

#all_pages!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bridge_api/api_array.rb', line 86

def all_pages!
  if pages?
    response = get_page(@next_page)
    (response)
    @members.concat(get_response_content(response))
    while @next_page
      response = get_page(@next_page)
      (response)
      @members.concat(get_response_content(response))
    end
  end
  self
end

#eachObject



62
63
64
# File 'lib/bridge_api/api_array.rb', line 62

def each
  @members.each { |member| yield(member) }
end

#each_page {|@members, @linked, @meta| ... } ⇒ Object

Yields:



75
76
77
78
79
80
81
82
83
84
# File 'lib/bridge_api/api_array.rb', line 75

def each_page
  yield(@members, @linked, @meta)
  while @next_page
    response = get_page(@next_page)
    (response, false)
    @members = get_response_content(response)
    yield(@members, @linked, @meta)
  end
  @link_hash = {}
end

#lastObject



58
59
60
# File 'lib/bridge_api/api_array.rb', line 58

def last
  @members.last
end

#lengthObject



50
51
52
# File 'lib/bridge_api/api_array.rb', line 50

def length
  @members.length
end

#next_pageObject



71
72
73
# File 'lib/bridge_api/api_array.rb', line 71

def next_page
  load_page(@next_page)
end

#pages?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/bridge_api/api_array.rb', line 66

def pages?
  !@next_page.nil?
end

#set_response_vars(response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/bridge_api/api_array.rb', line 39

def set_response_vars(response)
  @headers = response.headers
  @status = response.status
  @body = response.body
  @errors = begin
              response.body['errors']
            rescue StandardError
              []
            end
end