pathbuilder 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm 1.9.2
2
+ export rvm_pretty_print_flag=1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pathbuilder.gemspec
4
+ gemspec
@@ -0,0 +1,43 @@
1
+ Pathbuilder
2
+ ===========
3
+
4
+ A simple helper to build url-paths (think restful) in a manner that looks like a method call. This gem is intended as
5
+ a component in rpc-style http client libraries.
6
+
7
+ PathBuilder.new.users[5].deactivate.to_s
8
+ => "users/5/deactivate"
9
+
10
+ Installation
11
+ ============
12
+
13
+ Not yet published as a gem, so you'll have to
14
+
15
+ git clone git@github.com:simen/pathbuilder.git
16
+ cd pathbuilder
17
+ rake install
18
+
19
+ Or if you use the awesomeness that is bundler, you stick this in your Gemfile:
20
+
21
+ gem "pathbuilder", :git => git@github.com:simen/pathbuilder.git
22
+
23
+ Usage
24
+ =====
25
+
26
+ PathBuilder.new.users.path
27
+ => "/users"
28
+
29
+ PathBulder.new.users[5]
30
+ => "/users/5"
31
+
32
+ PathBulder.new.geopoints.closest(10.0, 51.2)
33
+ => "/geopoints/closest/10.0/51.2"
34
+
35
+ PathBuilder.new[5].user_timeline.path
36
+ => "/5/user_timeline"
37
+
38
+ p = PathBuilder.new.identities(:nick => "sigve")
39
+ p.path
40
+ => "/identities"
41
+ p.params
42
+ => {:nick => "sigve"}
43
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ class PathBuilder
2
+ attr_reader :params
3
+
4
+ def initialize(*args)
5
+ @path = []
6
+ @params = {}
7
+ end
8
+
9
+ def [](*args)
10
+ @path += args
11
+ self
12
+ end
13
+
14
+ def method_missing(method, *args)
15
+ @path << method
16
+ args.each { |arg| arg.is_a?(Hash) ? @params.merge!(arg) : @path << arg }
17
+ self
18
+ end
19
+
20
+ def to_s
21
+ "/#{@path.join("/")}"
22
+ end
23
+ alias :path :to_s
24
+
25
+ def inspect
26
+ "#<#{self.class} @path=\"#{self.to_s}\" @params=#{@params.inspect}>"
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Pathbuilder
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pathbuilder/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pathbuilder"
7
+ s.version = Pathbuilder::VERSION
8
+ s.authors = ["Simen Svale Skogsrud"]
9
+ s.email = ["simen@bengler.no"]
10
+ s.homepage = ""
11
+ s.summary = %q{A class for building restful urls in a ruby-methodic way}
12
+ s.description = %q{A class for building restful urls in a ruby-methodic way}
13
+
14
+ s.rubyforge_project = "pathbuilder"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe PathBuilder do
4
+ it "returns blank for blank paths" do
5
+ subject.to_s.should eq('/')
6
+ end
7
+
8
+ it "yields relative paths" do
9
+ subject.simplepath.to_s.should eq('/simplepath')
10
+ end
11
+
12
+ it "puts slashes between elements" do
13
+ subject.first.second.to_s.should eq('/first/second')
14
+ end
15
+
16
+ it "adds indexes as path elements" do
17
+ subject.users[5].to_s.should eq('/users/5')
18
+ end
19
+
20
+ it "adds multiple indexes as separate path elements" do
21
+ subject.location[10.0,20.0].to_s.should eq('/location/10.0/20.0')
22
+ end
23
+
24
+ it "pushes method params to the path" do
25
+ subject.three(10,20)
26
+ subject.path.should eq("/three/10/20")
27
+ end
28
+
29
+ it "merges hash method params into the params hash" do
30
+ subject.three.two(:a => 3)
31
+ subject.path.should eq "/three/two"
32
+ subject.params.should eq({:a => 3})
33
+ end
34
+
35
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__)+'/../lib/pathbuilder.rb'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simen Svale Skogsrud
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70167972652560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70167972652560
25
+ description: A class for building restful urls in a ruby-methodic way
26
+ email:
27
+ - simen@bengler.no
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rvmrc
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - README.markdown
37
+ - Rakefile
38
+ - lib/pathbuilder.rb
39
+ - lib/pathbuilder/version.rb
40
+ - pathbuilder.gemspec
41
+ - spec/pathbuilder_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: ''
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project: pathbuilder
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A class for building restful urls in a ruby-methodic way
67
+ test_files:
68
+ - spec/pathbuilder_spec.rb
69
+ - spec/spec_helper.rb