Skip to content

Commit

Permalink
pattern replacing is working
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 23, 2010
1 parent f8cb851 commit d1326d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/restfulie/client/feature/open_search/pattern_matcher.rb
@@ -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 lib/restfulie/common/representation/open_search/descriptor.rb
@@ -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 spec/unit/client/feature/open_search/pattern_matcher_spec.rb
@@ -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

0 comments on commit d1326d6

Please sign in to comment.