Class: Clubhouse::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/clubhouse2/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: 'https://api.clubhouse.io/api/v2/') ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/clubhouse2/client.rb', line 7

def initialize(api_key:, base_url: 'https://api.clubhouse.io/api/v2/')
	@api_key = api_key
	@base_url = base_url
	@resources = {}
end

Instance Method Details

#api_request(method, *params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/clubhouse2/client.rb', line 17

def api_request(method, *params)
	response = HTTP.headers(content_type: 'application/json').send(method, *params)
	case response.code
	when 429
		sleep 30
		api_request(method, *params)
	when 200
	when 201
	else
		raise ClubhouseAPIError.new(response)
	end

	response
end

#categories(**args) ⇒ Object



110
# File 'lib/clubhouse2/client.rb', line 110

def categories(**args); get_objects(:category, args); end

#category(**args) ⇒ Object



111
# File 'lib/clubhouse2/client.rb', line 111

def category(**args); get_object(:category, args); end

#create_category(**args) ⇒ Object



109
# File 'lib/clubhouse2/client.rb', line 109

def create_category(**args); create_object(:category, args); end

#create_epic(**args) ⇒ Object



105
# File 'lib/clubhouse2/client.rb', line 105

def create_epic(**args); create_object(:epic, args); end

#create_file(**args) ⇒ Object



118
# File 'lib/clubhouse2/client.rb', line 118

def create_file(**args); create_object(:file, args); end

#create_label(**args) ⇒ Object



113
# File 'lib/clubhouse2/client.rb', line 113

def create_label(**args); create_object(:label, args); end

#create_linked_file(**args) ⇒ Object



122
# File 'lib/clubhouse2/client.rb', line 122

def create_linked_file(**args); create_object(:linkedfile, args); end

#create_member(**args) ⇒ Object



97
# File 'lib/clubhouse2/client.rb', line 97

def create_member(**args); create_object(:member, args); end

#create_milestone(**args) ⇒ Object




75
# File 'lib/clubhouse2/client.rb', line 75

def create_milestone(**args); create_object(:milestone, args); end

#create_object(resource_class, args) ⇒ Object

or v.empty? if v.respond_to?(:empty?)



48
49
50
51
52
53
54
55
# File 'lib/clubhouse2/client.rb', line 48

def create_object(resource_class, args)
	this_class = Clubhouse::ClubhouseResource.subclass(resource_class)
	this_class.validate(args)
	flush(this_class)
	new_params = args.compact.reject { |k, v| this_class.property_filter_create.include? k.to_sym }
	response = api_request(:post, url(this_class.api_url), :json => new_params)
	JSON.parse(response.to_s)
end

#create_project(**args) ⇒ Object



79
# File 'lib/clubhouse2/client.rb', line 79

def create_project(**args); create_object(:project, args); end

#create_story(**args) ⇒ Object



83
# File 'lib/clubhouse2/client.rb', line 83

def create_story(**args); create_object(:story, args); end

#create_story_link(**args) ⇒ Object



90
# File 'lib/clubhouse2/client.rb', line 90

def create_story_link(**args); create_object(:storylink, args); end

#create_team(**args) ⇒ Object



101
# File 'lib/clubhouse2/client.rb', line 101

def create_team(**args); create_object(:team, args); end

#create_workflow(**args) ⇒ Object



126
# File 'lib/clubhouse2/client.rb', line 126

def create_workflow(**args); create_object(:workflow, args); end

#epic(**args) ⇒ Object



107
# File 'lib/clubhouse2/client.rb', line 107

def epic(**args); get_object(:epic, args); end

#epics(**args) ⇒ Object



106
# File 'lib/clubhouse2/client.rb', line 106

def epics(**args); get_objects(:epic, args); end

#file(**args) ⇒ Object



120
# File 'lib/clubhouse2/client.rb', line 120

def file(**args); get_object(:file, args); end

#files(**args) ⇒ Object



119
# File 'lib/clubhouse2/client.rb', line 119

def files(**args); get_objects(:file, args); end

#filter(object_array, args) ⇒ Object

Take all the provided properties, and filter out any resources that don’t match. If the value of a property is an object with an ID, match on that ID instead (makes for tidier queries)



38
39
40
# File 'lib/clubhouse2/client.rb', line 38

def filter(object_array, args)
	object_array.reject { |s| args.collect { |k, v| not resolve_to_ids([ *s.send(k) ]).include? resolve_to_ids(v) }.reduce(:|) }
end

#flush(resource_class) ⇒ Object



32
33
34
# File 'lib/clubhouse2/client.rb', line 32

def flush(resource_class)
	@resources[resource_class] = nil
end

#get_object(resource_class, args = {}) ⇒ Object



69
70
71
# File 'lib/clubhouse2/client.rb', line 69

def get_object(resource_class, args = {})
	get_objects(resource_class, args).first
end

#get_objects(resource_class, args = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/clubhouse2/client.rb', line 57

def get_objects(resource_class, args = {})
	this_class = Clubhouse::ClubhouseResource.subclass(resource_class)
	unless @resources[this_class]
		response = api_request(:get, url(this_class.api_url))
		@resources[this_class] = JSON.parse(response.to_s).collect do |resource|
			this_class.new(client: self, object: resource)
		end
	end

	filter(@resources[this_class], args)
end

#label(**args) ⇒ Object



116
# File 'lib/clubhouse2/client.rb', line 116

def label(**args); get_object(:label, args); end

#labels(**args) ⇒ Object



115
# File 'lib/clubhouse2/client.rb', line 115

def labels(**args); get_objects(:label, args); end

#linked_file(**args) ⇒ Object



124
# File 'lib/clubhouse2/client.rb', line 124

def linked_file(**args); get_object(:linkedfile, args); end

#linked_files(**args) ⇒ Object



123
# File 'lib/clubhouse2/client.rb', line 123

def linked_files(**args); get_objects(:linkedfile, args); end

#member(**args) ⇒ Object



99
# File 'lib/clubhouse2/client.rb', line 99

def member(**args); get_object(:member, args); end

#members(**args) ⇒ Object



98
# File 'lib/clubhouse2/client.rb', line 98

def members(**args); get_objects(:member, args); end

#milestone(**args) ⇒ Object



77
# File 'lib/clubhouse2/client.rb', line 77

def milestone(**args); get_object(:milestone, args); end

#milestones(**args) ⇒ Object



76
# File 'lib/clubhouse2/client.rb', line 76

def milestones(**args); get_objects(:milestone, args); end

#project(**args) ⇒ Object



81
# File 'lib/clubhouse2/client.rb', line 81

def project(**args); get_object(:project, args); end

#projects(**args) ⇒ Object



80
# File 'lib/clubhouse2/client.rb', line 80

def projects(**args); get_objects(:project, args); end

#resolve_to_ids(object) ⇒ Object



42
43
44
45
# File 'lib/clubhouse2/client.rb', line 42

def resolve_to_ids(object)
	return object.collect { |o| resolve_to_ids(o) } if object.is_a? Array
	(object.respond_to?(:id) ? object.id : object)
end

#stories(**args) ⇒ Object



84
85
86
# File 'lib/clubhouse2/client.rb', line 84

def stories(**args)
	filter(get_objects(:project).collect(&:stories).flatten, args)
end

#story(**args) ⇒ Object



88
# File 'lib/clubhouse2/client.rb', line 88

def story(**args); stories(**args).first; end

#story_link(**args) ⇒ Object



95
# File 'lib/clubhouse2/client.rb', line 95

def story_link(**args); story_links(**args).first; end

#story_links(**args) ⇒ Object



91
92
93
# File 'lib/clubhouse2/client.rb', line 91

def story_links(**args)
	filter(stories.collect(&:story_links).flatten, args)
end

#team(**args) ⇒ Object



103
# File 'lib/clubhouse2/client.rb', line 103

def team(**args); get_object(:team, args); end

#teams(**args) ⇒ Object



102
# File 'lib/clubhouse2/client.rb', line 102

def teams(**args); get_objects(:team, args); end

#update_label(**args) ⇒ Object



114
# File 'lib/clubhouse2/client.rb', line 114

def update_label(**args); update_object(:label, args); end

#url(endpoint) ⇒ Object



13
14
15
# File 'lib/clubhouse2/client.rb', line 13

def url(endpoint)
	URI.join(@base_url, endpoint,'?token=%s' % @api_key)
end

#workflow(**args) ⇒ Object



128
# File 'lib/clubhouse2/client.rb', line 128

def workflow(**args); get_object(:workflow, args); end

#workflows(**args) ⇒ Object



127
# File 'lib/clubhouse2/client.rb', line 127

def workflows(**args); get_objects(:workflow, args); end