Class: TrelloUtils::TrelloClient
- Inherits:
-
Object
- Object
- TrelloUtils::TrelloClient
- Defined in:
- lib/trello_utils.rb
Instance Attribute Summary collapse
-
#board_ids ⇒ Object
readonly
Returns the value of attribute board_ids.
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#organizations ⇒ Object
readonly
Returns the value of attribute organizations.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #get_uri(board_id, actions_since) ⇒ Object
-
#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
constructor
A new instance of TrelloClient.
- #issue_request(uri) ⇒ Object
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_ids ⇒ Object (readonly)
Returns the value of attribute board_ids.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def board_ids @board_ids end |
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def entities @entities end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def fields @fields end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def filters @filters end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def key @key end |
#organizations ⇒ Object (readonly)
Returns the value of attribute organizations.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def organizations @organizations end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def parameters @parameters end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/trello_utils.rb', line 10 def port @port end |
#token ⇒ Object (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 |