Class: StashAPI::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/stash_api/resource.rb

Direct Known Subclasses

Comment, Core, Project, PullRequest, Repo

Constant Summary collapse

@@resources =
[]
@@resource_ids =
[]

Class Method Summary collapse

Class Method Details

.add_resource_to_chain(index, resource, key = nil) ⇒ Object



62
63
64
65
# File 'lib/stash_api/resource.rb', line 62

def self.add_resource_to_chain(index, resource, key = nil)
  @@resources[index] = "#{resource}"
  @@resource_ids[index] = key
end

.create_resource(payload, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/stash_api/resource.rb', line 43

def self.create_resource(payload, options = {})

  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
# File 'lib/stash_api/resource.rb', line 6

def self.fetch(query = {})
  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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stash_api/resource.rb', line 20

def self.fetch_all(query = {})
  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_missingObject



80
81
82
# File 'lib/stash_api/resource.rb', line 80

def self.raise_resource_key_missing
  raise 'previous resource has no key to allow chaining'
end

.reset_resource_chainObject



67
68
69
70
# File 'lib/stash_api/resource.rb', line 67

def self.reset_resource_chain
  @@resources = []
  @@resource_ids = []
end

.resource(pos) ⇒ Object



72
73
74
# File 'lib/stash_api/resource.rb', line 72

def self.resource(pos)
  @@resources[pos]
end

.resource_id(pos) ⇒ Object



76
77
78
# File 'lib/stash_api/resource.rb', line 76

def self.resource_id(pos)
  @@resource_ids[pos]
end

.resource_pathObject



53
54
55
56
57
58
59
60
# File 'lib/stash_api/resource.rb', line 53

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