Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8cb851
commit d1326d6
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
lib/restfulie/client/feature/open_search/pattern_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Restfulie::Client::Feature::OpenSearch | ||
|
||
class PatternMatcher | ||
|
||
def match(params, pattern) | ||
params = params.collect do |key, value| | ||
[key, value] | ||
end | ||
pattern = params.inject(pattern) do |pattern, p| | ||
what = "{#{p[0]}}" | ||
if pattern[what] | ||
pattern[what]= "#{p[1]}" | ||
end | ||
what = "{#{p[0]}?}" | ||
if pattern[what] | ||
pattern[what]= "#{p[1]}" | ||
end | ||
pattern | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
32 changes: 32 additions & 0 deletions
32
lib/restfulie/common/representation/open_search/descriptor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Restfulie::Common::Representation::OpenSearch::Descriptor | ||
|
||
def initialize(hash) | ||
@hash = hash["OpenSearchDescription"] | ||
end | ||
|
||
def urls | ||
uris = @hash["Url"] | ||
uris.kind_of?(Array) ? uris : [uris] | ||
end | ||
|
||
def use(content_type) | ||
uri = urls.find do |url| | ||
url["type"]==content_type | ||
end | ||
return nil if uri.nil? | ||
|
||
base_uri, params_pattern = extract_uri(uri) | ||
Restfulie.at(base_uri).open_search.with_pattern(params_pattern) | ||
end | ||
|
||
private | ||
def extract_uri(uri) | ||
uri = uri["template"] | ||
interrogation = uri.index("?") | ||
params = uri[interrogation+1..uri.size] | ||
base_uri = uri[0..interrogation-1] | ||
[base_uri, params] | ||
end | ||
|
||
end | ||
|
15 changes: 15 additions & 0 deletions
15
spec/unit/client/feature/open_search/pattern_matcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') | ||
|
||
describe Restfulie::Client::Feature::OpenSearch::PatternMatcher do | ||
|
||
context "matching patterns" do | ||
|
||
it "should unmarshall opensearch xml descriptions" do | ||
pattern = "q={searchTerms}&pw={startPage?}&format=atom" | ||
matcher = Restfulie::Client::Feature::OpenSearch::PatternMatcher.new | ||
matcher.match({:searchTerms => 12, :startPage => 13},pattern).should == "q=12&pw=13&format=atom" | ||
end | ||
|
||
end | ||
|
||
end |