resty 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +24 -0
- data/bin/resty +24 -0
- data/lib/resty/actions.rb +27 -0
- data/lib/resty/attributes.rb +44 -0
- data/lib/resty/transport.rb +24 -0
- data/lib/resty.rb +72 -0
- metadata +75 -0
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Copyright (c) 2011, Simon Russell
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted
|
5
|
+
provided that the following conditions are met:
|
6
|
+
|
7
|
+
Redistributions of source code must retain the above copyright notice, this list of conditions and
|
8
|
+
the following disclaimer.
|
9
|
+
|
10
|
+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
11
|
+
and the following disclaimer in the documentation and/or other materials provided with the
|
12
|
+
distribution.
|
13
|
+
|
14
|
+
Neither the name of Simon Russell nor the names of its contributors may be used to endorse or
|
15
|
+
promote products derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
22
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
23
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
24
|
+
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/bin/resty
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '../lib/resty')
|
6
|
+
|
7
|
+
def json_puts(o)
|
8
|
+
o = o._populated_data if o.is_a?(Resty)
|
9
|
+
puts JSON.pretty_generate(o)
|
10
|
+
end
|
11
|
+
|
12
|
+
if ARGV.length < 1
|
13
|
+
$stderr.puts "usage: #{$0} <resource-url>"
|
14
|
+
exit 1
|
15
|
+
end
|
16
|
+
|
17
|
+
href = ARGV.shift
|
18
|
+
object = Resty.href(href)
|
19
|
+
|
20
|
+
until ARGV.empty?
|
21
|
+
object = object.send(ARGV.first)
|
22
|
+
end
|
23
|
+
|
24
|
+
json_puts object
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Resty::Actions
|
2
|
+
|
3
|
+
def initialize(definitions)
|
4
|
+
@definitions = definitions || {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def perform!(name, parameters = nil)
|
8
|
+
raise "unknown action #{name}" unless exist?(name)
|
9
|
+
href = @definitions[name].fetch(':href') { raise "no href for action #{name}" }
|
10
|
+
method = @definitions[name].fetch(':method') { raise "no method for action #{name}" }
|
11
|
+
|
12
|
+
if parameters
|
13
|
+
body = parameters.to_json
|
14
|
+
mimetype = 'application/json'
|
15
|
+
else
|
16
|
+
body = nil
|
17
|
+
mimetype = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
Resty.wrap(Resty::Transport.request_json(href, method, body, mimetype))
|
21
|
+
end
|
22
|
+
|
23
|
+
def exist?(name)
|
24
|
+
@definitions.key?(name)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Resty::Attributes
|
2
|
+
|
3
|
+
attr_reader :href
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@href = data[':href']
|
7
|
+
@populated = !@href || data.length > 1 # a hack for now
|
8
|
+
@data = data
|
9
|
+
end
|
10
|
+
|
11
|
+
def key?(name)
|
12
|
+
populate! unless populated?
|
13
|
+
@data.key?(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](name)
|
17
|
+
populate! unless populated?
|
18
|
+
Resty.wrap(@data[name])
|
19
|
+
end
|
20
|
+
|
21
|
+
def populated?
|
22
|
+
@populated
|
23
|
+
end
|
24
|
+
|
25
|
+
def populate!
|
26
|
+
@data = Resty::Transport.request_json(@href)
|
27
|
+
@populated = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def populated_data
|
31
|
+
populate!
|
32
|
+
@data
|
33
|
+
end
|
34
|
+
|
35
|
+
def actions
|
36
|
+
unless @actions
|
37
|
+
populate! unless populated?
|
38
|
+
@actions = Resty::Actions.new(@data[':actions'])
|
39
|
+
end
|
40
|
+
|
41
|
+
@actions
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Resty::Transport
|
2
|
+
|
3
|
+
def self.request_json(href, method = 'GET', body = nil, content_type = nil)
|
4
|
+
|
5
|
+
headers = content_type ? { :content_type => content_type } : nil
|
6
|
+
payload = body || ''
|
7
|
+
|
8
|
+
response = RestClient::Request.execute(
|
9
|
+
:url => href,
|
10
|
+
:method => method,
|
11
|
+
:payload => payload,
|
12
|
+
:headers => headers
|
13
|
+
)
|
14
|
+
|
15
|
+
if !(response.nil? || response =~ /\A\s*\Z/)
|
16
|
+
JSON.parse(response)
|
17
|
+
elsif response.headers[:location]
|
18
|
+
{ ':href' => response.headers[:location] }
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/resty.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
class Resty
|
6
|
+
|
7
|
+
def initialize(attributes)
|
8
|
+
@attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
def _href
|
12
|
+
@attributes.href
|
13
|
+
end
|
14
|
+
|
15
|
+
def _populated_data
|
16
|
+
@attributes.populated_data
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to_missing?(name, include_private)
|
20
|
+
@attributes.key?(name.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_param
|
24
|
+
Resty.encode_param(_href)
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(name, *args)
|
28
|
+
if name =~ /^(.+)!$/
|
29
|
+
if @attributes.actions.exist?($1)
|
30
|
+
@attributes.actions.perform!($1, *args)
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
elsif @attributes.key?(name.to_s)
|
35
|
+
@attributes[name.to_s]
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.from(data)
|
42
|
+
new(Resty::Attributes.new(data))
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.wrap(object)
|
46
|
+
case object
|
47
|
+
when Hash
|
48
|
+
from(object)
|
49
|
+
when Array
|
50
|
+
from(':items' => object)
|
51
|
+
else
|
52
|
+
object
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.href(href)
|
57
|
+
from(':href' => href)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.encode_param(s)
|
61
|
+
s && Base64.urlsafe_encode64(s.to_s)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.decode_param(s)
|
65
|
+
s && Base64.urlsafe_decode64(s.to_s)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
%w(attributes transport actions).each do |f|
|
71
|
+
require File.join(File.dirname(__FILE__), 'resty', f)
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simon Russell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-07 00:00:00.000000000 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rest-client
|
17
|
+
requirement: &18825360 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.6.3
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *18825360
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: json
|
28
|
+
requirement: &18824880 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.5.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *18824880
|
37
|
+
description:
|
38
|
+
email:
|
39
|
+
executables:
|
40
|
+
- resty
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- lib/resty/attributes.rb
|
45
|
+
- lib/resty/actions.rb
|
46
|
+
- lib/resty/transport.rb
|
47
|
+
- lib/resty.rb
|
48
|
+
- bin/resty
|
49
|
+
- LICENSE
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/simonrussell/resty
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.9.2
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.6.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Simple JSON REST API client wrapper
|
75
|
+
test_files: []
|