restfolia 1.0.0 → 1.0.1

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/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
+ ENV["RUBYOPT"] = "rubygems" if ENV["RUBYOPT"].nil?
5
+
4
6
  Rake::TestTask.new do |t|
5
7
  t.libs.push "lib"
6
8
  t.libs.push "test"
data/Readme.md CHANGED
@@ -1,5 +1,7 @@
1
+ ![Logo Restfolia][logo_readme]
1
2
  # Restfolia [![Build Status][travis_status]][travis]
2
3
 
4
+ [logo_readme]: http://rogerleite.github.com/restfolia/images/logo_readme.png
3
5
  [travis_status]: https://secure.travis-ci.org/rogerleite/restfolia.png
4
6
  [travis]: http://travis-ci.org/rogerleite/restfolia
5
7
 
@@ -55,13 +55,13 @@ module Restfolia::HTTP
55
55
 
56
56
  # Public: Creates a Store.
57
57
  def initialize
58
- self.clear
58
+ self.clear!
59
59
  @helpers = Helpers.new
60
60
  end
61
61
 
62
62
  # Public: clear all defined behaviours.
63
63
  # Returns nothing.
64
- def clear
64
+ def clear!
65
65
  @behaviours = {}
66
66
  @behaviours_range = {}
67
67
  nil
@@ -34,23 +34,6 @@ module Restfolia
34
34
  #
35
35
  class ResourceCreator
36
36
 
37
- # Public: By default, returns Restfolia::Resource. You can use
38
- # this method to override and returns a custom Resource. See examples.
39
- #
40
- # Examples
41
- #
42
- # # using a custom Resource
43
- # class Restfolia::ResourceCreator
44
- # def resource_class
45
- # OpenStruct #dont forget to require 'ostruct'
46
- # end
47
- # end
48
- #
49
- # Returns class of Resource to be constructed.
50
- def resource_class
51
- Restfolia::Resource
52
- end
53
-
54
37
  # Public: creates Resource looking recursively for JSON
55
38
  # objects and transforming in Resources. To create Resource,
56
39
  # this method use #resource_class.new(json).
@@ -66,25 +49,63 @@ module Restfolia
66
49
 
67
50
  json_parsed = {}
68
51
  json.each do |attr, value|
69
- json_parsed[attr] = look_for_resource(value)
52
+ json_parsed[attr] = look_for_resource(attr, value)
70
53
  end
71
54
  resource_class.new(json_parsed)
72
55
  end
73
56
 
74
57
  protected
75
58
 
59
+ # Internal: By default, returns Restfolia::Resource. You can use
60
+ # this method to override and returns a custom Resource.
61
+ #
62
+ # Examples
63
+ #
64
+ # # using a custom Resource
65
+ # class Restfolia::ResourceCreator
66
+ # def resource_class
67
+ # OpenStruct #dont forget to require 'ostruct'
68
+ # end
69
+ # end
70
+ #
71
+ # Returns class of Resource to be constructed.
72
+ def resource_class
73
+ Restfolia::Resource
74
+ end
75
+
76
+ # Internal: By default, returns :links or :link. You can use
77
+ # this method to override and returns a custom rule, can be an
78
+ # Array or any object that responds to include?.
79
+ #
80
+ # Examples
81
+ #
82
+ # class Restfolia::ResourceCreator
83
+ # def attributes_to_dont_parse
84
+ # [:links, :link, :_links].freeze
85
+ # end
86
+ # end
87
+ #
88
+ # Returns attributes to be ignored when creating Resource.
89
+ def attributes_to_dont_parse
90
+ [:links, :link].freeze
91
+ end
92
+
76
93
  # Internal: Check if value is eligible to become a Restfolia::Resource.
94
+ # It attr_name exist in #attributes_to_dont_parse, it returns value.
77
95
  # If value is Array object, looks inner contents, using rules below.
78
96
  # If value is Hash object, it becomes a Restfolia::Resource.
79
97
  # Else return itself.
80
98
  #
99
+ # attr_name - attribute name from parsed hash.
81
100
  # value - object to be checked.
82
101
  #
83
102
  # Returns value itself or Resource.
84
- def look_for_resource(value)
103
+ def look_for_resource(attr_name, value)
104
+ return value if attributes_to_dont_parse.include?(attr_name)
105
+
85
106
  if value.is_a?(Array)
86
107
  value = value.inject([]) do |resources, array_obj|
87
- resources << look_for_resource(array_obj)
108
+ resources << look_for_resource(attr_name, array_obj)
88
109
  end
89
110
  elsif value.is_a?(Hash)
90
111
  value = resource_class.new(value)
@@ -1,3 +1,3 @@
1
1
  module Restfolia
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/restfolia.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require "net/http"
2
2
  require "uri"
3
3
 
4
- require "rubygems"
5
4
  require "multi_json"
6
5
 
7
6
  require "restfolia/version"
data/restfolia.gemspec CHANGED
@@ -5,7 +5,7 @@ require "restfolia/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "restfolia"
7
7
  s.version = Restfolia::VERSION
8
- s.authors = `git log --raw | grep Author: | awk -F ': | <|>' '{ print $2 }' | sort | uniq`.split("\n")
8
+ s.authors = ["Roger Leite"]
9
9
  s.email = ["roger.barreto@gmail.com"]
10
10
  s.homepage = "http://rogerleite.github.com/restfolia"
11
11
  s.summary = %q{REST client to consume and interact with Hypermedia API}
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_runtime_dependency "multi_json", "~> 1.3.0"
23
23
 
24
24
  s.add_development_dependency "rake"
25
- s.add_development_dependency "minitest"
26
- s.add_development_dependency "minitest-reporters"
27
- s.add_development_dependency "webmock"
25
+ s.add_development_dependency "minitest", "~> 3"
26
+ s.add_development_dependency "minitest-reporters", "~> 0.7.0"
27
+ s.add_development_dependency "webmock", "~> 1"
28
28
  end
@@ -1,8 +1,7 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/changing_links_parse.rb
3
+ # $ ruby -rubygems samples/changing_links_parse.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
7
 
@@ -1,11 +1,11 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/cookies_options.rb
3
+ # $ ruby -rubygems samples/cookies_options.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
7
 
8
+ # Running https://github.com/rogerleite/simple_api
9
9
  SERVICE_URL = "http://localhost:9292/recursos/busca"
10
10
 
11
11
  sample_cookie = "PREF=ID=988f14fa5edd3243:TM=1335470032:LM=1335470032:S=KVBslNbyz6bG0DqU; expires=Sat, 26-Apr-2014 19:53:52 GMT; path=/; domain=.google.com, NID=59=peUyZQuLWQ_0gELr1yDf0FT4ZlT7ZdITNrO5OhkEnAvp_8MZ4TT6pHq7_q_Su-puTw7vGml_Ok6du8fLreGHzfpMs4Qh1v-qBCFYGuCNbzpwN670x5MFbGKy0KUXA3WP; expires=Fri, 26-Oct-2012 19:53:52 GMT; path=/; domain=.google.com; HttpOnly"
@@ -1,11 +1,11 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/headers_options.rb
3
+ # $ ruby -rubygems samples/headers_options.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
7
 
8
+ # Running https://github.com/rogerleite/simple_api
9
9
  SERVICE_URL = "http://localhost:9292/recursos/busca"
10
10
 
11
11
  # accessing headers attribute
@@ -1,14 +1,14 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/http_behaviour.rb
3
+ # $ ruby -rubygems samples/http_behaviour.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
- require "ostruct"
9
7
 
10
8
  Restfolia::HTTP.behaviours do
11
9
 
10
+ clear! #clear all defined behaviours
11
+
12
12
  on(200) do |http_response|
13
13
  content_type = (http_response["content-type"] =~ /application\/json/)
14
14
  if !content_type
@@ -36,17 +36,13 @@ Restfolia::HTTP.behaviours do
36
36
  # custom_helper
37
37
  #end
38
38
 
39
- #helpers do
40
- # def custom_helper
41
- # 'lixo'
42
- # end
43
- #end
44
-
45
39
  end
46
40
 
41
+ # Running https://github.com/rogerleite/simple_api
47
42
  SERVICE_URL = "http://localhost:9292/recursos/busca"
43
+
48
44
  resource = Restfolia.at(SERVICE_URL).get
49
- puts resource.inspect
45
+ puts resource.inspect # => #<Restfolia::Resource ...>
50
46
 
51
47
  Restfolia.at("http://google.com").get
52
48
  # => "3xx error"
@@ -0,0 +1,31 @@
1
+ # This is a small sample to show how to consume search from http://n0tice.org API.
2
+ # Reference: http://n0tice.org/developers/
3
+ #
4
+ # Run this sample from root project:
5
+ # $ ruby -rubygems samples/n0tice_api.rb
6
+
7
+ $LOAD_PATH << "lib"
8
+ require "restfolia"
9
+
10
+ class Restfolia::Resource
11
+
12
+ # Return "self" link, if "apiUrl" exists.
13
+ def parse_links(json)
14
+ self_url = json[:apiUrl]
15
+ return [] if self_url.nil?
16
+
17
+ [Restfolia::EntryPoint.new(self_url, "self")]
18
+ end
19
+
20
+ end
21
+
22
+ result = Restfolia.at("http://n0ticeapis.com/2/search?type=report&location=Brazil").get
23
+
24
+ puts result.place.name # => "Brazil"
25
+
26
+ first_report = result.results.first
27
+ puts first_report.headline # => "Camp Nectar ..."
28
+
29
+ # Request report info. Ex: http://n0ticeapis.com/2/report/4220
30
+ report = first_report.links("self").get
31
+ puts report.webUrl # => "http://..."
@@ -1,16 +1,16 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/using_custom_factory.rb
3
+ # $ ruby -rubygems samples/using_custom_factory.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
7
  require "ostruct"
9
8
 
9
+ # Running https://github.com/rogerleite/simple_api
10
10
  SERVICE_URL = "http://localhost:9292/recursos/busca"
11
11
 
12
12
  resource = Restfolia.at(SERVICE_URL).get
13
- puts resource.inspect # => #<Restfolia::Resource ...>
13
+ #puts resource.inspect # => #<Restfolia::Resource ...>
14
14
 
15
15
  # Here you have the "pure" json from response body.
16
16
  # You can do anything.
@@ -1,16 +1,16 @@
1
1
 
2
2
  # Run this sample from root project:
3
- # $ ruby samples/using_custom_resource.rb
3
+ # $ ruby -rubygems samples/using_custom_resource.rb
4
4
 
5
- require "rubygems"
6
5
  $LOAD_PATH << "lib"
7
6
  require "restfolia"
8
7
  require "ostruct"
9
8
 
9
+ # Running https://github.com/rogerleite/simple_api
10
10
  SERVICE_URL = "http://localhost:9292/recursos/busca"
11
11
 
12
12
  resource = Restfolia.at(SERVICE_URL).get
13
- puts resource.inspect # => #<Restfolia::Resource ...>
13
+ #puts resource.inspect # => #<Restfolia::Resource ...>
14
14
 
15
15
  # Here you have the advantage to use a custom resource
16
16
  # and the same time you have the recursive lookup at hash
@@ -35,8 +35,10 @@ describe Restfolia::HTTP::Behaviour do
35
35
  end
36
36
 
37
37
  it "should call #default_behaviour for non match code" do
38
- @http_mock.expect(:code, "666")
39
- lambda { subject.execute(@http_mock) }.
38
+ http_mock = Object.new
39
+ http_mock.instance_eval { def code; "666"; end }
40
+
41
+ lambda { subject.execute(http_mock) }.
40
42
  must_raise(Restfolia::ResponseError)
41
43
  end
42
44
  end
@@ -28,6 +28,16 @@ describe Restfolia::ResourceCreator do
28
28
  resource.attr_test.nested.must_equal("nested")
29
29
  end
30
30
 
31
+ it "should not parse if attribute is on attributes_to_dont_parse" do
32
+ def subject.attributes_to_dont_parse
33
+ [:to_ignore]
34
+ end
35
+ resource = subject.create(:attr_test => {:nested => "nested"},
36
+ :to_ignore => {:rel => "test"})
37
+ resource.must_be_instance_of(OpenStruct)
38
+ resource.to_ignore.must_be_instance_of(Hash)
39
+ end
40
+
31
41
  it "transforms nested hash from Arrays in Resource" do
32
42
  resource = subject.create(:attr_test => [{:nested_array => "object"}],
33
43
  :attr_test2 => ["not object"])
metadata CHANGED
@@ -1,23 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restfolia
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
- - Nadilson
14
13
  - Roger Leite
15
14
  autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2012-05-16 00:00:00 -03:00
20
- default_executable:
18
+ date: 2012-05-28 00:00:00 Z
21
19
  dependencies:
22
20
  - !ruby/object:Gem::Dependency
23
21
  name: multi_json
@@ -55,12 +53,12 @@ dependencies:
55
53
  requirement: &id003 !ruby/object:Gem::Requirement
56
54
  none: false
57
55
  requirements:
58
- - - ">="
56
+ - - ~>
59
57
  - !ruby/object:Gem::Version
60
- hash: 3
58
+ hash: 5
61
59
  segments:
62
- - 0
63
- version: "0"
60
+ - 3
61
+ version: "3"
64
62
  type: :development
65
63
  version_requirements: *id003
66
64
  - !ruby/object:Gem::Dependency
@@ -69,12 +67,14 @@ dependencies:
69
67
  requirement: &id004 !ruby/object:Gem::Requirement
70
68
  none: false
71
69
  requirements:
72
- - - ">="
70
+ - - ~>
73
71
  - !ruby/object:Gem::Version
74
72
  hash: 3
75
73
  segments:
76
74
  - 0
77
- version: "0"
75
+ - 7
76
+ - 0
77
+ version: 0.7.0
78
78
  type: :development
79
79
  version_requirements: *id004
80
80
  - !ruby/object:Gem::Dependency
@@ -83,12 +83,12 @@ dependencies:
83
83
  requirement: &id005 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
- - - ">="
86
+ - - ~>
87
87
  - !ruby/object:Gem::Version
88
- hash: 3
88
+ hash: 1
89
89
  segments:
90
- - 0
91
- version: "0"
90
+ - 1
91
+ version: "1"
92
92
  type: :development
93
93
  version_requirements: *id005
94
94
  description: REST client to consume and interact with Hypermedia API
@@ -120,11 +120,11 @@ files:
120
120
  - lib/restfolia/resource_creator.rb
121
121
  - lib/restfolia/version.rb
122
122
  - restfolia.gemspec
123
- - samples/changing_behaviour.rb
124
123
  - samples/changing_links_parse.rb
125
124
  - samples/cookies_options.rb
126
125
  - samples/headers_options.rb
127
126
  - samples/http_behaviour.rb
127
+ - samples/n0tice_api.rb
128
128
  - samples/using_custom_factory.rb
129
129
  - samples/using_custom_resource.rb
130
130
  - test/restfolia/entry_point_test.rb
@@ -136,7 +136,6 @@ files:
136
136
  - test/support/json_samples.rb
137
137
  - test/support/stub_helpers.rb
138
138
  - test/test_helper.rb
139
- has_rdoc: true
140
139
  homepage: http://rogerleite.github.com/restfolia
141
140
  licenses:
142
141
  - MIT
@@ -166,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
165
  requirements: []
167
166
 
168
167
  rubyforge_project: restfolia
169
- rubygems_version: 1.4.2
168
+ rubygems_version: 1.8.15
170
169
  signing_key:
171
170
  specification_version: 3
172
171
  summary: REST client to consume and interact with Hypermedia API
@@ -1,32 +0,0 @@
1
-
2
- # Run this sample from root project:
3
- # $ ruby samples/changing_behaviour.rb
4
-
5
- require "rubygems"
6
- $LOAD_PATH << "lib"
7
- require "restfolia"
8
-
9
- begin
10
- # Default behaviour to redirect 3xx is raise Error
11
- Restfolia.at("http://google.com.br").get
12
- rescue Restfolia::ResponseError => ex
13
- puts ex.message
14
- end
15
-
16
- module Restfolia::HTTP::Behaviour
17
-
18
- # We can change behaviour of many HTTP status
19
- # on_2xx(http_response)
20
- # on_3xx(http_response)
21
- # on_4xx(http_response)
22
- # on_5xx(http_response)
23
-
24
- # Here we change 3xx behaviour to return a Resource
25
- def on_3xx(http_response)
26
- Restfolia.create_resource(:redirected => "I'm free! :D")
27
- end
28
-
29
- end
30
-
31
- resource = Restfolia.at("http://google.com.br").get
32
- puts resource.redirected