Skip to content

Commit

Permalink
Include story child items in results and sort result lists
Browse files Browse the repository at this point in the history
Closes #41, #38
  • Loading branch information
samrayner committed Jan 26, 2014
1 parent 3a85277 commit b03bf60
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sprintly Workflow for Alfred 2 - [Download](https://github.com/samrayner/Sprintly-for-Alfred/releases/download/v1.0.2/Sprintly.alfredworkflow)
#Sprintly Workflow for Alfred 2 - [Download](https://github.com/samrayner/Sprintly-for-Alfred/releases/download/v1.0.3/Sprintly.alfredworkflow)

Installation
------------
Expand Down
9 changes: 5 additions & 4 deletions lib/sly/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def authenticated_request(url, params={}, post=false)

response = Net::HTTP.start(
uri.host,
uri.port,
:use_ssl => (uri.scheme == 'https'),
uri.port,
:use_ssl => (uri.scheme == 'https'),
:verify_mode => OpenSSL::SSL::VERIFY_NONE
) do |https|
https.request(request)
end

if(response.class.body_permitted?)
begin
begin
JSON(response.body)
rescue JSON::ParserError
false
Expand Down Expand Up @@ -70,6 +70,7 @@ def person(id)
end

def items(filters={})
filters[:children] = true
authenticated_request(@api_url+"/products/#{@config.product_id}/items.json", filters)
end

Expand All @@ -84,4 +85,4 @@ def add_item(attributes)
def update_item(id, attributes)
authenticated_request(@api_url+"/products/#{@config.product_id}/items/#{id}.json", attributes, true)
end
end
end
31 changes: 21 additions & 10 deletions lib/sly/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ def people(query="")
#JSON error message returned
return [] unless people.kind_of?(Array)

#convert to objects
people.map! { |person| Sly::Person.new(person) }

#filter by query
people.select do |person|
query.empty? ||
people.select! do |person|
query.empty? ||
person.full_name.downcase.include?(query.downcase) ||
(query.downcase == "me" && person.email == @connector.config.email)
end

people.sort_by { |person| [person.last_name, person.first_name] }
end

def products(query="")
Expand All @@ -60,11 +61,12 @@ def products(query="")
#JSON error message returned
return [] unless products.kind_of?(Array)

#convert to objects
products.map! { |product| Sly::Product.new(product) }

#filter by query
products.select { |product| query.empty? || product.name.downcase.match(/^#{query.downcase}/) }
products.select! { |product| query.empty? || product.name.downcase.match(/^#{query.downcase}/) }

products.sort_by { |product| product.name }
end

def items(filters={}, query="")
Expand All @@ -73,7 +75,6 @@ def items(filters={}, query="")
#JSON error message returned
return [] unless items.kind_of?(Array)

#convert to appropriate objects
items.map! { |item| Sly::Item.new_typed(item) }

person_filter = query.match(/\@([^\s]*)/)
Expand All @@ -84,14 +85,24 @@ def items(filters={}, query="")
end

#filter by person
items.select! do |item|
person_filter.to_s.empty? ||
items.select! do |item|
person_filter.to_s.empty? ||
item.assigned_to.full_name.downcase.include?(person_filter) ||
(person_filter == "me" && item.assigned_to.email == @connector.config.email)
end

#filter by query
items.select { |item| query.empty? || item.title.downcase.include?(query.downcase) }
items.select! { |item| query.empty? || item.title.downcase.include?(query.downcase) }

#add arrow to title of sub-items
items.map! do |item|
if items.member?(item.parent)
item.title = [0x21B3].pack("U")+" "+item.title
end
item
end

items.sort_by { |item| item.index }
end

def product(id)
Expand Down Expand Up @@ -146,4 +157,4 @@ def update_item(id, attributes)

@connector.update_item(id, item_hash)
end
end
end
22 changes: 18 additions & 4 deletions lib/sly/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ class Sly::Item < Sly::Object
attr_accessor :status, :product, :description, :tags, :last_modified
attr_accessor :number, :archived, :title, :short_url, :created_at
attr_accessor :created_by, :score, :assigned_to, :type, :progress
attr_accessor :parent, :index

def initialize(attributes={})
super(attributes)
@score = @score.upcase if @score
@tags = [] unless @tags

if @parent
@parent = Sly::Item.new_typed(@parent)
@index = "#{@parent.number}.#{@number}"
else
@index = @number.to_s
end
end

def self.new_typed(attributes={})
type = self.hash_value(attributes, :type)
if type

if type
Sly::const_get("#{type.capitalize}Item").new(attributes)
else
self.new(attributes)
Expand All @@ -31,6 +39,12 @@ def self.hash_value(hash, key)
value
end

def ==(item)
return false unless item
self.number == item.number
end
alias_method :eql?, :==

def str_to_slug(str)
str.strip.downcase.gsub(/(&|&amp;)/, ' and ').gsub(/[\s\/\\]/, '-').gsub(/[^\w-]/, '').gsub(/[-]{2,}/, '-')
end
Expand All @@ -45,6 +59,6 @@ def alfred_result
@tags.each { |tag| subtitle << " #"+tag }

icon = "images/#{@type}-#{@score}.png".downcase
Sly::WorkflowUtils.item(@number, "#"+@number.to_s, self.title, subtitle, icon)
Sly::WorkflowUtils.item("#"+@number.to_s, self.title, subtitle, icon)
end
end
end
4 changes: 2 additions & 2 deletions lib/sly/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def full_name
end

def alfred_result
Sly::WorkflowUtils.item(@id, "#"+@id.to_s, self.full_name, @email)
Sly::WorkflowUtils.item("#"+@id.to_s, self.full_name, @email)
end
end
end
4 changes: 2 additions & 2 deletions lib/sly/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Sly::Product < Sly::Object

def alfred_result
subtitle = "Created "+@created_at.strftime("%B %d, %Y")
Sly::WorkflowUtils.item(@id, "#"+@id.to_s, @name, subtitle)
Sly::WorkflowUtils.item("#"+@id.to_s, @name, subtitle)
end
end
end
8 changes: 4 additions & 4 deletions lib/sly/workflow_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ def self.array_to_xml(array)
doc.to_s
end

def self.item(uid, arg, title, subtitle, icon="icon.png", valid="yes", autocomplete="")
def self.item(arg, title, subtitle, icon="icon.png", uid="", valid="yes", autocomplete="")
return {
uid:uid,
arg:arg,
title:title,
subtitle:subtitle,
icon:icon,
uid:uid,
valid:valid,
autocomplete:autocomplete
}
end

def self.autocomplete_item(title, subtitle, autocomplete, icon="icon.png")
return {
uid:Time.now.to_f.to_s.sub(/\./, ""),
arg:"",
title:title,
subtitle:subtitle,
icon:icon,
uid:"",
valid:"no",
autocomplete:autocomplete
}
Expand Down Expand Up @@ -69,4 +69,4 @@ def self.results_feed(items)

self.array_to_xml(items)
end
end
end
3 changes: 1 addition & 2 deletions spec/sly/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
describe :alfred_result do
it "returns valid alfred search result for product" do
alfred_result = @product.alfred_result
alfred_result[:uid].should == @product.id
alfred_result[:arg].should == "#"+@product.id.to_s
alfred_result[:title].should == @product.name
end
end
end
end

0 comments on commit b03bf60

Please sign in to comment.