Class: TrelloUtils::TrelloClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kwargs = { organizations: [], # an array of organizations from which to derive board ids key: nil, # oauth key token: nil, # oauth secret board_ids: [], fields: PARAM_DEFAULT_FIELDS, filters: {}, port: 443 # trello REST port }) ⇒ TrelloClient

Returns a new instance of TrelloClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trello_utils.rb', line 13

def initialize(kwargs={
					organizations:  [], # an array of organizations from which to derive board ids
					key:            nil, # oauth key
					token:          nil, # oauth secret
					board_ids:      [],
					fields:         PARAM_DEFAULT_FIELDS,
					filters:        {},
					port:           443 # trello REST port
	})

	@organizations = kwargs[:organizations]
	@key           = kwargs[:key]
	@token         = kwargs[:token]
	@board_ids     = kwargs[:board_ids]
	@fields        = kwargs[:fields]
	@filters       = kwargs[:filters]
	@port          = kwargs[:port]	

	# get board ids if none are provided
	if @board_ids.empty?
		board_filter = "all"
		if @filters.has_key?("boards")
			if not @filters["boards"].empty?
				board_filter = array_to_uri( @filters["boards"] )
			end
		end
		@board_ids = get_board_ids(@board_ids, board_filter)
	end

	# create entities hash from select fields in fields
	entities = {}
	@fields.each do |field|
		if ALL_ENTITIES.include?(field)
			state = ALL_ENTITIES[field]
			if state == "false"
				entities[field] = "true"
			elsif state == "none"
				if field == "boardStars"
					entities[field] = "mine"
				else
					entities[field] = "all"
				end
			end
		end
	end
	
	# mutate entities hash according to filters
	@filters.each do |key, val|
		if entities.include?(key)
			if val != "none"
				entities[key] = array_to_uri(filters[val])
			end
		end
	end

	# create fields hash
	@fields = Set.new(@fields)
	new_fields = ALL_FIELDS.clone
	new_fields.to_a.each do |key, val|
		temp = @fields.intersection(val).to_a
           new_fields[key] = array_to_uri(temp)
	end
	@fields = new_fields

	# merge fields and entities into params
	params = new_fields.merge(entities)
	
	# switch out board_fields with fields
	params["fields"] = params["board_fields"]
	params.delete("board_fields")

	@parameters = params
end

Instance Attribute Details

#board_idsObject (readonly)

Returns the value of attribute board_ids.



10
11
12
# File 'lib/trello_utils.rb', line 10

def board_ids
  @board_ids
end

#entitiesObject (readonly)

Returns the value of attribute entities.



10
11
12
# File 'lib/trello_utils.rb', line 10

def entities
  @entities
end

#fieldsObject (readonly)

Returns the value of attribute fields.



10
11
12
# File 'lib/trello_utils.rb', line 10

def fields
  @fields
end

#filtersObject (readonly)

Returns the value of attribute filters.



10
11
12
# File 'lib/trello_utils.rb', line 10

def filters
  @filters
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/trello_utils.rb', line 10

def host
  @host
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/trello_utils.rb', line 10

def key
  @key
end

#organizationsObject (readonly)

Returns the value of attribute organizations.



10
11
12
# File 'lib/trello_utils.rb', line 10

def organizations
  @organizations
end

#parametersObject (readonly)

Returns the value of attribute parameters.



10
11
12
# File 'lib/trello_utils.rb', line 10

def parameters
  @parameters
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/trello_utils.rb', line 10

def port
  @port
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/trello_utils.rb', line 10

def token
  @token
end

Instance Method Details

#get_uri(board_id, actions_since) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello_utils.rb', line 118

def get_uri(board_id, actions_since)
	uri = "/1/boards/#{board_id}?"
	args = {
		"actions_format" => "list",
		"actions_since"  => actions_since,
		"actions_limit"  => "1000",
		"labels_limit"   => "1000",
		"key"            => @key,
		"token"          => @token
	}
	uri += URI.encode_www_form(@parameters.merge(args))
	return uri
end

#issue_request(uri) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/trello_utils.rb', line 133

def issue_request(uri)
	response = Net::HTTP.new("api.trello.com", @port)
	response.use_ssl = true
	response = response.request_get(uri, {"Connection" => "close"})
	code = response.code.to_i()
	if 199 < code and code < 300
		return JSON.load(response.body)
	else
		@logger.warn("HTTP request error: ", + response)
		raise RuntimeError, "HTTP request failed with code #{code}: #{response}"
	end
end