Class: StashAPI::Resource
- Inherits:
-
Object
- Object
- StashAPI::Resource
show all
- Defined in:
- lib/stash_api/resource.rb
Constant Summary
collapse
- @@resources =
[]
- @@resource_ids =
[]
Class Method Summary
collapse
Class Method Details
.add_resource_to_chain(index, resource, key = nil) ⇒ Object
66
67
68
69
|
# File 'lib/stash_api/resource.rb', line 66
def self.add_resource_to_chain(index, resource, key = nil)
@@resources[index] = "#{resource}"
@@resource_ids[index] = key
end
|
.create_resource(payload, options = {}) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/stash_api/resource.rb', line 46
def self.create_resource(payload, options = {})
raise "set a domain first 'StashAPI::Base.domain(<domain>)'" unless StashAPI::Options.option(:domain)
options[:body] = payload.to_json
options[:headers] = {'Content-Type' => 'application/json'}
response = HTTP::Client.post resource_path, options
response
end
|
.fetch(query = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/stash_api/resource.rb', line 6
def self.fetch(query = {})
raise "set a domain first 'StashAPI::Base.domain(<domain>)'" unless StashAPI::Options.option(:domain)
options = {}
options[:query] = query
response = HTTP::Client.get resource_path, options
reset_resource_chain
if response.code == 200
response.parsed_response
else
response.code
end
end
|
.fetch_all(query = {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/stash_api/resource.rb', line 22
def self.fetch_all(query = {})
raise "set a domain first 'StashAPI::Base.domain(<domain>)'" unless StashAPI::Options.option(:domain)
options = {}
query[:limit] = 1000
query[:start] = 0
options[:query] = query
results = []
begin
response = HTTP::Client.get(resource_path, options)
if response.code == 200
response = response.parsed_response
results.concat response['values']
options[:query][:start] = response['start'] + response['limit']
else
results == response
end
end while !response['isLastPage']
results
end
|
.raise_resource_key_missing ⇒ Object
84
85
86
|
# File 'lib/stash_api/resource.rb', line 84
def self.raise_resource_key_missing
raise 'previous resource has no key to allow chaining'
end
|
.reset_resource_chain ⇒ Object
71
72
73
74
|
# File 'lib/stash_api/resource.rb', line 71
def self.reset_resource_chain
@@resources = []
@@resource_ids = []
end
|
.resource(pos) ⇒ Object
76
77
78
|
# File 'lib/stash_api/resource.rb', line 76
def self.resource(pos)
@@resources[pos]
end
|
.resource_id(pos) ⇒ Object
80
81
82
|
# File 'lib/stash_api/resource.rb', line 80
def self.resource_id(pos)
@@resource_ids[pos]
end
|
.resource_path ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/stash_api/resource.rb', line 57
def self.resource_path
path = '/api/1.0'
for i in 0..@@resources.size
path << "/#{@@resources[i]}" if @@resources[i]
path << "/#{@@resource_ids[i]}" if @@resource_ids[i]
end
path
end
|