Skip to content

Commit

Permalink
Added ResultIterator filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed Mar 16, 2018
1 parent 806ee50 commit 30562d9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.6.1

Feature:

- Added `where` filtering to result iterators (thanks to Jeljer te Wies for the feature).

## 1.6.0

Feature:
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,19 @@ The following attributes will pass validation since they explicitly `allow_nil`:
- `:retirement_age`
- `:favorite_authors`

### Debugging
### Filtering result lists

If the API returns a JSON list of items, this is retured to you as a `Flexirest::ResultIterator` object. A `ResultIterator` sorts simple filtering of the list based on values matching a specified criteria (or matching using regular expressions):

```ruby
class Article < Flexirest::Base
get :all, "/articles"
end

Article.all.where(published: true, department: /technical\-/)
```

## Debugging

You can turn on verbose debugging to see what is sent to the API server and what is returned in one of these two ways:

Expand Down
14 changes: 14 additions & 0 deletions lib/flexirest/result_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ def delete_if
self
end

def where(criteria={})
@items.select do |object|
select = true
criteria.each do |k, v|
if v.is_a?(Regexp)
select = false if !object[k][v]
else
select = false if object[k] != v
end
end
select
end
end

def parallelise(method=nil)
collected_responses = []
threads = []
Expand Down
2 changes: 1 addition & 1 deletion lib/flexirest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Flexirest
VERSION = "1.6.0"
VERSION = "1.6.1"
end
23 changes: 23 additions & 0 deletions spec/lib/result_iterator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,30 @@
expect(result.items).to eq(["a"])
end

it "can filter results by simple matching criteria" do
class Something < Flexirest::Base
end

results = Flexirest::ResultIterator.new
results << Something.new(type: "foo")
results << Something.new(type: "foo")
results << Something.new(type: "bar")

expect(results.where(type: "foo").count).to eq(2)
end

it "can filter results using regular expression matching criteria" do
class Something < Flexirest::Base
end

results = Flexirest::ResultIterator.new
results << Something.new(type: "fooo")
results << Something.new(type: "foo")
results << Something.new(type: "fo")
results << Something.new(type: "bar")

expect(results.where(type: /foo+/).count).to eq(2)
end

it "can parallelise calls to each item" do
result = Flexirest::ResultIterator.new
Expand Down

0 comments on commit 30562d9

Please sign in to comment.