Class: StashAPI::Resource

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

Direct Known Subclasses

Comment, Core, Project, PullRequest, Repo, User

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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



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

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

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stash_api/resource.rb', line 18

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
  reset_resource_chain

  response
end

.fetch(query = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# 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

  response
end

.raise_resource_key_missingObject



57
58
59
# File 'lib/stash_api/resource.rb', line 57

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

.reset_resource_chainObject



44
45
46
47
# File 'lib/stash_api/resource.rb', line 44

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

.resource(pos) ⇒ Object



49
50
51
# File 'lib/stash_api/resource.rb', line 49

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

.resource_id(pos) ⇒ Object



53
54
55
# File 'lib/stash_api/resource.rb', line 53

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

.resource_pathObject



30
31
32
33
34
35
36
37
# File 'lib/stash_api/resource.rb', line 30

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