nestful 0.0.8 → 1.0.0.pre

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.
@@ -1,38 +1,155 @@
1
+ require 'uri'
2
+
1
3
  module Nestful
2
4
  class Resource
3
- attr_reader :url
4
-
5
- def initialize(url, options = {})
6
- @url = url
7
- @options = options
5
+ def self.endpoint(value = nil)
6
+ @endpoint = value if value
7
+ @endpoint || ''
8
+ end
9
+
10
+ def self.url(value = nil)
11
+ @url = value if value
12
+ @url ? URI.join(endpoint, @url) : raise('Must define url')
13
+ end
14
+
15
+ def self.options(value = nil)
16
+ @options = value if value
17
+ @options || {}
18
+ end
19
+
20
+ def self.get(url, params = {}, options = {})
21
+ request(url, options.merge(:method => :get, :params => params))
22
+ end
23
+
24
+ def self.put(url, params = {}, options = {})
25
+ request(url, options.merge(:method => :put, :params => params))
26
+ end
27
+
28
+ def self.post(url, params = {}, options = {})
29
+ request(url, options.merge(:method => :post, :params => params))
30
+ end
31
+
32
+ def self.delete(url, params = {}, options = {})
33
+ request(url, options.merge(:method => :delete, :params => params))
34
+ end
35
+
36
+ def self.request(url, options = {})
37
+ Request.new(url, self.options.merge(options)).execute
38
+ end
39
+
40
+ def self.all
41
+ self.new(get(url))
42
+ end
43
+
44
+ def self.find(id)
45
+ self.new(get(URI.join(url, id.to_s)))
46
+ end
47
+
48
+ def self.new(attributes = {}, options = {})
49
+ if attributes.is_a?(Array)
50
+ attributes.map {|set| super(set, options) }
51
+ else
52
+ super
53
+ end
54
+ end
55
+
56
+ attr_reader :attributes, :options
57
+
58
+ def initialize(attributes = {}, options = {})
59
+ @attributes = indifferent_attributes(attributes.to_hash)
60
+ @options = self.class.options.merge(options)
61
+ end
62
+
63
+ def get(*args)
64
+ self.class.get(url, *args)
8
65
  end
9
-
10
- def [](suburl)
11
- return self if suburl.nil?
12
- suburl = suburl.to_s
13
- base = url
14
- base += "/" unless base =~ /\/$/
15
- self.class.new(URI.join(base, suburl).to_s, @options)
66
+
67
+ def put(*args)
68
+ self.class.put(url, *args)
69
+ end
70
+
71
+ def post(*args)
72
+ self.class.post(url, *args)
73
+ end
74
+
75
+ def delete(*args)
76
+ self.class.delete(url, *args)
77
+ end
78
+
79
+ alias_method :destroy, :delete
80
+
81
+ def url
82
+ URI.join(self.class.url, self.id.to_s)
83
+ end
84
+
85
+ def id #:nodoc:
86
+ self['id']
87
+ end
88
+
89
+ def type #:nodoc:
90
+ self['type']
91
+ end
92
+
93
+ def [](key)
94
+ attributes[key]
16
95
  end
17
-
18
- def get(options = {})
19
- Nestful.get(url, options.merge(@options))
96
+
97
+ def []=(key,value)
98
+ attributes[key] = value
20
99
  end
21
100
 
22
- def post(options = {})
23
- Nestful.post(url, options.merge(@options))
101
+ def to_hash
102
+ @attributes.dup
24
103
  end
25
-
26
- def json_get(params = nil)
27
- get(:format => :json, :params => params)
104
+
105
+ alias_method :as_json, :to_hash
106
+
107
+ def to_json(*)
108
+ as_json.to_json
28
109
  end
29
-
30
- def json_post(params = nil)
31
- post(:format => :json, :params => params)
110
+
111
+ alias_method :respond_to_without_attributes?, :respond_to?
112
+
113
+ def respond_to?(method, include_priv = false)
114
+ method_name = method.to_s
115
+ if attributes.nil?
116
+ super
117
+ elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`)
118
+ true
119
+ else
120
+ super
121
+ end
32
122
  end
33
-
34
- def request(options = {})
35
- Request.new(url, options.merge(@options)).execute
123
+
124
+ protected
125
+
126
+ def indifferent_attributes(attributes)
127
+ attributes = indifferent_hash.merge(attributes)
128
+ attributes.each do |key, value|
129
+ next unless value.is_a?(Hash)
130
+ attributes[key] = indifferent_attributes(value)
131
+ end
132
+ end
133
+
134
+ # Creates a Hash with indifferent access.
135
+ def indifferent_hash
136
+ Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
137
+ end
138
+
139
+ def method_missing(method_symbol, *arguments) #:nodoc:
140
+ method_name = method_symbol.to_s
141
+
142
+ if method_name =~ /(=|\?)$/
143
+ case $1
144
+ when "="
145
+ attributes[$`] = arguments.first
146
+ when "?"
147
+ attributes[$`]
148
+ end
149
+ else
150
+ return attributes[method_name] if attributes.include?(method_name)
151
+ super
152
+ end
36
153
  end
37
154
  end
38
- end
155
+ end
@@ -0,0 +1,31 @@
1
+ module Nestful
2
+ class Response #:nodoc:
3
+ class Headers
4
+ include ::Net::HTTPHeader
5
+
6
+ def initialize(header = {})
7
+ @header = header
8
+ end
9
+
10
+ def ==(other)
11
+ @header == other
12
+ end
13
+
14
+ def inspect
15
+ @header.inspect
16
+ end
17
+
18
+ def method_missing(name, *args, &block)
19
+ if @header.respond_to?(name)
20
+ @header.send(name, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def respond_to?(method)
27
+ super || @header.respond_to?(method)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ module Nestful
2
+ class Response
3
+ attr_reader :response, :body, :headers, :parser
4
+
5
+ def initialize(response, parser = nil)
6
+ @response = response
7
+ @body = response.body
8
+ @headers = Headers.new(response.to_hash)
9
+ @format = Formats.for(headers.content_type)
10
+ @parser ||= @format && @format.new
11
+ end
12
+
13
+ def as_json
14
+ decoded
15
+ end
16
+
17
+ def to_json(*)
18
+ as_json.to_json
19
+ end
20
+
21
+ def code
22
+ response.code.to_i
23
+ end
24
+
25
+ def decoded
26
+ @decoded ||= parser ? parser.decode(body) : body
27
+ end
28
+
29
+ def respond_to?(name)
30
+ super || decoded.respond_to?(name) || response.respond_to?(name)
31
+ end
32
+
33
+ protected
34
+
35
+ def method_missing(name, *args, &block)
36
+ if decoded.respond_to?(name)
37
+ decoded.send(name, *args, &block)
38
+ elsif response.respond_to?(name)
39
+ response.send(name, *args, &block)
40
+ else
41
+ super
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ require 'nestful/response/headers'
@@ -0,0 +1,3 @@
1
+ module Nestful
2
+ VERSION = "1.0.0.pre"
3
+ end
data/lib/nestful.rb CHANGED
@@ -1,43 +1,30 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'tempfile'
4
-
5
- require 'active_support/core_ext'
6
- require 'active_support/inflector'
7
-
8
- $:.unshift(File.dirname(__FILE__))
9
-
1
+ require 'nestful/version'
10
2
  require 'nestful/exceptions'
11
- require 'nestful/formats'
12
- require 'nestful/connection'
13
- require 'nestful/request/callbacks'
14
- require 'nestful/request'
15
- require 'nestful/resource'
16
3
 
17
4
  module Nestful
5
+ autoload :Endpoint, 'nestful/endpoint'
6
+ autoload :Formats, 'nestful/formats'
7
+ autoload :Connection, 'nestful/connection'
8
+ autoload :Helpers, 'nestful/helpers'
9
+ autoload :Request, 'nestful/request'
10
+ autoload :Response, 'nestful/response'
11
+ autoload :Resource, 'nestful/resource'
12
+
18
13
  extend self
19
14
 
20
- def get(url, options = {})
21
- Request.new(url, ({:method => :get}).merge(options)).execute
15
+ def get(url, *args)
16
+ Endpoint[url].get(*args)
22
17
  end
23
-
18
+
24
19
  def post(url, options = {})
25
- Request.new(url, ({:method => :post, :format => :form}).merge(options)).execute
20
+ Endpoint[url].post(*args)
26
21
  end
27
-
22
+
28
23
  def put(url, options = {})
29
- Request.new(url, ({:method => :put}).merge(options)).execute
24
+ Endpoint[url].put(*args)
30
25
  end
31
-
26
+
32
27
  def delete(url, options = {})
33
- Request.new(url, ({:method => :delete}).merge(options)).execute
34
- end
35
-
36
- def json_get(url, params = nil)
37
- get(url, :format => :json, :params => params)
38
- end
39
-
40
- def json_post(url, params = nil)
41
- post(url, :format => :json, :params => params)
28
+ Endpoint[url].delete(*args)
42
29
  end
43
30
  end
data/nestful.gemspec CHANGED
@@ -1,58 +1,18 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nestful/version'
5
5
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{nestful}
8
- s.version = "0.0.6"
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "nestful"
8
+ gem.version = Nestful::VERSION
9
+ gem.authors = ["Alex MacCaw"]
10
+ gem.email = ["info@eribium.org"]
11
+ gem.summary = %q{Simple Ruby HTTP/REST client with a sane API}
12
+ gem.homepage = "https://github.com/maccman/nestful"
9
13
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Alex MacCaw"]
12
- s.date = %q{2010-10-15}
13
- s.description = %q{Simple Ruby HTTP/REST client with a sane API}
14
- s.email = %q{info@eribium.org}
15
- s.extra_rdoc_files = [
16
- "README.markdown"
17
- ]
18
- s.files = [
19
- ".gitignore",
20
- "MIT-LICENSE",
21
- "README.markdown",
22
- "Rakefile",
23
- "VERSION",
24
- "lib/nestful.rb",
25
- "lib/nestful/connection.rb",
26
- "lib/nestful/exceptions.rb",
27
- "lib/nestful/formats.rb",
28
- "lib/nestful/formats/blank_format.rb",
29
- "lib/nestful/formats/form_format.rb",
30
- "lib/nestful/formats/json_format.rb",
31
- "lib/nestful/formats/multipart_format.rb",
32
- "lib/nestful/formats/xml_format.rb",
33
- "lib/nestful/oauth.rb",
34
- "lib/nestful/request.rb",
35
- "lib/nestful/request/callbacks.rb",
36
- "lib/nestful/resource.rb",
37
- "nestful.gemspec"
38
- ]
39
- s.homepage = %q{http://github.com/maccman/nestful}
40
- s.rdoc_options = ["--charset=UTF-8"]
41
- s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.7}
43
- s.summary = %q{Simple Ruby HTTP/REST client with a sane API}
44
-
45
- if s.respond_to? :specification_version then
46
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
- s.specification_version = 3
48
-
49
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
- s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta"])
51
- else
52
- s.add_dependency(%q<activesupport>, [">= 3.0.0.beta"])
53
- end
54
- else
55
- s.add_dependency(%q<activesupport>, [">= 3.0.0.beta"])
56
- end
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
57
18
  end
58
-
metadata CHANGED
@@ -1,54 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
5
- prerelease:
4
+ version: 1.0.0.pre
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alex MacCaw
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-05 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: activesupport
16
- requirement: &70352776847120 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 3.0.0.beta
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70352776847120
25
- description: Simple Ruby HTTP/REST client with a sane API
26
- email: info@eribium.org
12
+ date: 2013-04-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - info@eribium.org
27
17
  executables: []
28
18
  extensions: []
29
- extra_rdoc_files:
30
- - README.markdown
19
+ extra_rdoc_files: []
31
20
  files:
21
+ - .gitignore
22
+ - Gemfile
32
23
  - MIT-LICENSE
33
24
  - README.markdown
34
25
  - Rakefile
35
26
  - VERSION
27
+ - examples/resource.rb
36
28
  - lib/nestful.rb
29
+ - lib/nestful/.DS_Store
37
30
  - lib/nestful/connection.rb
31
+ - lib/nestful/endpoint.rb
38
32
  - lib/nestful/exceptions.rb
39
33
  - lib/nestful/formats.rb
40
- - lib/nestful/formats/blank_format.rb
41
34
  - lib/nestful/formats/form_format.rb
42
35
  - lib/nestful/formats/json_format.rb
43
36
  - lib/nestful/formats/multipart_format.rb
44
- - lib/nestful/formats/text_format.rb
45
- - lib/nestful/formats/xml_format.rb
46
- - lib/nestful/oauth.rb
37
+ - lib/nestful/helpers.rb
47
38
  - lib/nestful/request.rb
48
- - lib/nestful/request/callbacks.rb
49
39
  - lib/nestful/resource.rb
40
+ - lib/nestful/response.rb
41
+ - lib/nestful/response/headers.rb
42
+ - lib/nestful/version.rb
50
43
  - nestful.gemspec
51
- homepage: http://github.com/maccman/nestful
44
+ homepage: https://github.com/maccman/nestful
52
45
  licenses: []
53
46
  post_install_message:
54
47
  rdoc_options: []
@@ -63,12 +56,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
56
  required_rubygems_version: !ruby/object:Gem::Requirement
64
57
  none: false
65
58
  requirements:
66
- - - ! '>='
59
+ - - ! '>'
67
60
  - !ruby/object:Gem::Version
68
- version: '0'
61
+ version: 1.3.1
69
62
  requirements: []
70
63
  rubyforge_project:
71
- rubygems_version: 1.8.6
64
+ rubygems_version: 1.8.24
72
65
  signing_key:
73
66
  specification_version: 3
74
67
  summary: Simple Ruby HTTP/REST client with a sane API