Class: BridgeAPI::ApiArray
- Inherits:
-
Object
- Object
- BridgeAPI::ApiArray
- Includes:
- Enumerable
- Defined in:
- lib/bridge_api/api_array.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#linked ⇒ Object
readonly
Returns the value of attribute linked.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #[](i) ⇒ Object
- #all_pages! ⇒ Object
- #each ⇒ Object
- #each_page {|@members, @linked, @meta| ... } ⇒ Object
-
#initialize(response, api_client, result_mapping) ⇒ ApiArray
constructor
A new instance of ApiArray.
- #last ⇒ Object
- #length ⇒ Object
- #next_page ⇒ Object
- #pages? ⇒ Boolean
- #set_response_vars(response) ⇒ Object
Constructor Details
#initialize(response, api_client, result_mapping) ⇒ ApiArray
Returns a new instance of ApiArray.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bridge_api/api_array.rb', line 16 def initialize(response, api_client, result_mapping) = %w[meta linked] @api_client = api_client @linked = {} = {} = [] pattern = %r{.*(/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 .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
#body ⇒ Object (readonly)
Returns the value of attribute body.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def body @body end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def errors @errors end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def headers @headers end |
#linked ⇒ Object (readonly)
Returns the value of attribute linked.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def linked @linked end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def members @members end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
10 11 12 |
# File 'lib/bridge_api/api_array.rb', line 10 def status @status end |
Class Method Details
.process_response(response, api_client, result_mapping) ⇒ Object
12 13 14 |
# File 'lib/bridge_api/api_array.rb', line 12 def self.process_response(response, api_client, result_mapping) ApiArray.new(response, api_client, result_mapping) end |
Instance Method Details
#[](i) ⇒ Object
55 56 57 |
# File 'lib/bridge_api/api_array.rb', line 55 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 |
#each ⇒ Object
63 64 65 |
# File 'lib/bridge_api/api_array.rb', line 63 def each @members.each { |member| yield(member) } end |
#each_page {|@members, @linked, @meta| ... } ⇒ Object
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, ) while @next_page response = get_page(@next_page) (response, false) @members = get_response_content(response) yield(@members, @linked, ) end @link_hash = {} end |
#last ⇒ Object
59 60 61 |
# File 'lib/bridge_api/api_array.rb', line 59 def last @members.last end |
#length ⇒ Object
51 52 53 |
# File 'lib/bridge_api/api_array.rb', line 51 def length @members.length end |
#next_page ⇒ Object
71 72 73 |
# File 'lib/bridge_api/api_array.rb', line 71 def next_page load_page(@next_page) end |
#pages? ⇒ Boolean
67 68 69 |
# File 'lib/bridge_api/api_array.rb', line 67 def pages? !@next_page.nil? end |
#set_response_vars(response) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bridge_api/api_array.rb', line 40 def set_response_vars(response) @headers = response.headers @status = response.status @body = response.body @errors = begin response.body['errors'] rescue StandardError [] end end |