tagcrumbs-tagcrumbs 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -0
- data/License.txt +22 -0
- data/Manifest.txt +177 -0
- data/PostInstall.txt +2 -0
- data/README.rdoc +45 -0
- data/Rakefile +35 -0
- data/TODO.txt +17 -0
- data/bin/tagcrumbs +5 -0
- data/examples/address_update.rb +15 -0
- data/examples/comment_new.rb +19 -0
- data/examples/favorite_new.rb +17 -0
- data/examples/filter_new.rb +15 -0
- data/examples/friendship_create.rb +19 -0
- data/examples/link_new.rb +16 -0
- data/examples/location_update.rb +16 -0
- data/examples/picture_update.rb +16 -0
- data/examples/placemark_new_simple.rb +24 -0
- data/examples/placemark_new_suggestions.rb +24 -0
- data/examples/profile_link_new.rb +14 -0
- data/examples/profile_update.rb +12 -0
- data/examples/request_authorization.rb +25 -0
- data/examples/settings_update.rb +12 -0
- data/examples/subscription_new.rb +15 -0
- data/examples/tagcrumbs_find.rb +25 -0
- data/examples/tagcrumbs_search.rb +7 -0
- data/examples/user.rb +28 -0
- data/examples/user_recommendation_new.rb +15 -0
- data/lib/tagcrumbs.rb +232 -0
- data/lib/tagcrumbs/builders/builder.rb +45 -0
- data/lib/tagcrumbs/builders/json_builder.rb +38 -0
- data/lib/tagcrumbs/builders/xml_builder.rb +30 -0
- data/lib/tagcrumbs/cli.rb +268 -0
- data/lib/tagcrumbs/config_store.rb +35 -0
- data/lib/tagcrumbs/exceptions.rb +13 -0
- data/lib/tagcrumbs/node.rb +43 -0
- data/lib/tagcrumbs/parsers/json_parser.rb +79 -0
- data/lib/tagcrumbs/parsers/parser.rb +24 -0
- data/lib/tagcrumbs/parsers/xml_parser.rb +54 -0
- data/lib/tagcrumbs/proxy.rb +32 -0
- data/lib/tagcrumbs/requestor.rb +140 -0
- data/lib/tagcrumbs/resources/array.rb +108 -0
- data/lib/tagcrumbs/resources/models/accessors.rb +252 -0
- data/lib/tagcrumbs/resources/models/activity.rb +19 -0
- data/lib/tagcrumbs/resources/models/address.rb +16 -0
- data/lib/tagcrumbs/resources/models/city.rb +13 -0
- data/lib/tagcrumbs/resources/models/comment.rb +12 -0
- data/lib/tagcrumbs/resources/models/country.rb +15 -0
- data/lib/tagcrumbs/resources/models/fanship.rb +12 -0
- data/lib/tagcrumbs/resources/models/favorite.rb +22 -0
- data/lib/tagcrumbs/resources/models/filter.rb +32 -0
- data/lib/tagcrumbs/resources/models/flag.rb +10 -0
- data/lib/tagcrumbs/resources/models/friendship.rb +12 -0
- data/lib/tagcrumbs/resources/models/geoname.rb +8 -0
- data/lib/tagcrumbs/resources/models/link.rb +13 -0
- data/lib/tagcrumbs/resources/models/location.rb +13 -0
- data/lib/tagcrumbs/resources/models/model.rb +148 -0
- data/lib/tagcrumbs/resources/models/picture.rb +28 -0
- data/lib/tagcrumbs/resources/models/place.rb +17 -0
- data/lib/tagcrumbs/resources/models/placemark.rb +30 -0
- data/lib/tagcrumbs/resources/models/profile.rb +16 -0
- data/lib/tagcrumbs/resources/models/profile_link.rb +12 -0
- data/lib/tagcrumbs/resources/models/root.rb +19 -0
- data/lib/tagcrumbs/resources/models/settings.rb +14 -0
- data/lib/tagcrumbs/resources/models/state.rb +14 -0
- data/lib/tagcrumbs/resources/models/subscription.rb +13 -0
- data/lib/tagcrumbs/resources/models/suggestions.rb +11 -0
- data/lib/tagcrumbs/resources/models/tag.rb +6 -0
- data/lib/tagcrumbs/resources/models/tagcrumb.rb +58 -0
- data/lib/tagcrumbs/resources/models/user.rb +101 -0
- data/lib/tagcrumbs/resources/models/user_recommendation.rb +18 -0
- data/lib/tagcrumbs/resources/resource.rb +76 -0
- data/lib/tagcrumbs/validations.rb +301 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/fixtures/activity.json +210 -0
- data/spec/fixtures/activity.xml +66 -0
- data/spec/fixtures/address.json +183 -0
- data/spec/fixtures/address.xml +58 -0
- data/spec/fixtures/city.json +88 -0
- data/spec/fixtures/city.xml +34 -0
- data/spec/fixtures/comment.json +286 -0
- data/spec/fixtures/comment.xml +87 -0
- data/spec/fixtures/country.json +32 -0
- data/spec/fixtures/country.xml +13 -0
- data/spec/fixtures/fanship.json +180 -0
- data/spec/fixtures/fanship.xml +54 -0
- data/spec/fixtures/favorite.json +332 -0
- data/spec/fixtures/favorite.xml +109 -0
- data/spec/fixtures/filter.json +243 -0
- data/spec/fixtures/filter.xml +80 -0
- data/spec/fixtures/friendship.json +180 -0
- data/spec/fixtures/friendship.xml +54 -0
- data/spec/fixtures/geoname.json +18 -0
- data/spec/fixtures/geoname.xml +6 -0
- data/spec/fixtures/json_parser.json +25 -0
- data/spec/fixtures/link.json +288 -0
- data/spec/fixtures/link.xml +88 -0
- data/spec/fixtures/location.json +204 -0
- data/spec/fixtures/location.xml +71 -0
- data/spec/fixtures/picture.json +154 -0
- data/spec/fixtures/picture.png +0 -0
- data/spec/fixtures/picture.xml +49 -0
- data/spec/fixtures/placemark.json +357 -0
- data/spec/fixtures/placemark.xml +58 -0
- data/spec/fixtures/placemarks.json +887 -0
- data/spec/fixtures/placemarks.xml +348 -0
- data/spec/fixtures/profile.json +282 -0
- data/spec/fixtures/profile.xml +100 -0
- data/spec/fixtures/profile_link.json +156 -0
- data/spec/fixtures/profile_link.xml +49 -0
- data/spec/fixtures/root.json +46 -0
- data/spec/fixtures/root.xml +14 -0
- data/spec/fixtures/settings.json +147 -0
- data/spec/fixtures/settings.xml +45 -0
- data/spec/fixtures/state.json +62 -0
- data/spec/fixtures/state.xml +23 -0
- data/spec/fixtures/subscription.json +283 -0
- data/spec/fixtures/subscription.xml +86 -0
- data/spec/fixtures/suggestions.json +1877 -0
- data/spec/fixtures/suggestions.xml +724 -0
- data/spec/fixtures/tag.json +9 -0
- data/spec/fixtures/tag.xml +4 -0
- data/spec/fixtures/user.json +105 -0
- data/spec/fixtures/user.xml +31 -0
- data/spec/fixtures/user_recommendation.json +349 -0
- data/spec/fixtures/user_recommendation.xml +106 -0
- data/spec/fixtures/validation_errors.json +5 -0
- data/spec/fixtures/validation_errors.xml +5 -0
- data/spec/fixtures/xml_parser.xml +13 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +75 -0
- data/spec/tagcrumbs/builders/builder_spec.rb +57 -0
- data/spec/tagcrumbs/builders/json_builder_spec.rb +47 -0
- data/spec/tagcrumbs/builders/xml_builder_spec.rb +34 -0
- data/spec/tagcrumbs/exceptions_spec.rb +16 -0
- data/spec/tagcrumbs/node_spec.rb +42 -0
- data/spec/tagcrumbs/parsers/json_parser_spec.rb +117 -0
- data/spec/tagcrumbs/parsers/parser_spec.rb +25 -0
- data/spec/tagcrumbs/parsers/xml_parser_spec.rb +117 -0
- data/spec/tagcrumbs/proxy_spec.rb +48 -0
- data/spec/tagcrumbs/requestor_spec.rb +62 -0
- data/spec/tagcrumbs/resources/array_spec.rb +62 -0
- data/spec/tagcrumbs/resources/models/accessors_spec.rb +123 -0
- data/spec/tagcrumbs/resources/models/activity_spec.rb +33 -0
- data/spec/tagcrumbs/resources/models/address_spec.rb +24 -0
- data/spec/tagcrumbs/resources/models/city_spec.rb +33 -0
- data/spec/tagcrumbs/resources/models/comment_spec.rb +23 -0
- data/spec/tagcrumbs/resources/models/country_spec.rb +26 -0
- data/spec/tagcrumbs/resources/models/fanship_spec.rb +28 -0
- data/spec/tagcrumbs/resources/models/favorite_spec.rb +28 -0
- data/spec/tagcrumbs/resources/models/filter_spec.rb +43 -0
- data/spec/tagcrumbs/resources/models/flag_spec.rb +5 -0
- data/spec/tagcrumbs/resources/models/friendship_spec.rb +28 -0
- data/spec/tagcrumbs/resources/models/geoname_spec.rb +18 -0
- data/spec/tagcrumbs/resources/models/link_spec.rb +24 -0
- data/spec/tagcrumbs/resources/models/location_spec.rb +28 -0
- data/spec/tagcrumbs/resources/models/model_spec.rb +27 -0
- data/spec/tagcrumbs/resources/models/picture_spec.rb +42 -0
- data/spec/tagcrumbs/resources/models/place_spec.rb +12 -0
- data/spec/tagcrumbs/resources/models/placemark_spec.rb +61 -0
- data/spec/tagcrumbs/resources/models/profile_link_spec.rb +29 -0
- data/spec/tagcrumbs/resources/models/profile_spec.rb +43 -0
- data/spec/tagcrumbs/resources/models/root_spec.rb +57 -0
- data/spec/tagcrumbs/resources/models/settings_spec.rb +32 -0
- data/spec/tagcrumbs/resources/models/state_spec.rb +30 -0
- data/spec/tagcrumbs/resources/models/subscription_spec.rb +28 -0
- data/spec/tagcrumbs/resources/models/suggestions_spec.rb +33 -0
- data/spec/tagcrumbs/resources/models/tag_spec.rb +16 -0
- data/spec/tagcrumbs/resources/models/tagcrumb_spec.rb +21 -0
- data/spec/tagcrumbs/resources/models/user_recommendation_spec.rb +35 -0
- data/spec/tagcrumbs/resources/models/user_spec.rb +128 -0
- data/spec/tagcrumbs/resources/resource_spec.rb +62 -0
- data/spec/tagcrumbs/validations_spec.rb +27 -0
- data/spec/tagcrumbs_spec.rb +103 -0
- data/tagcrumbs.gemspec +59 -0
- data/tasks/rspec.rake +21 -0
- metadata +325 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# Each User has Profile. A Profile can only be updated, it is created automatically with a User and destroyed if the User
|
3
|
+
# decides to leave Tagcrumbs.
|
4
|
+
class Profile < Model
|
5
|
+
can_be :updated
|
6
|
+
|
7
|
+
writeable_attributes :fullname, :about, :description
|
8
|
+
readable_attributes :created_at, :updated_at
|
9
|
+
|
10
|
+
has_one :user
|
11
|
+
has_one :city, :modifiable => true
|
12
|
+
has_one :picture
|
13
|
+
|
14
|
+
has_many :profile_links
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# Each User can set multiple ProfileLinks
|
3
|
+
# e.g. his Facebook account, blog, website etc.
|
4
|
+
class ProfileLink < Model
|
5
|
+
can_be :created, :updated, :destroyed
|
6
|
+
|
7
|
+
writeable_attributes :url, :link_type
|
8
|
+
readable_attributes :created_at, :updated_at
|
9
|
+
|
10
|
+
has_one :profile
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# The Root Object that contains links to all other resources on tagcrumbs.com.
|
3
|
+
# It is used as the starting point for many other queries.
|
4
|
+
class Root < Model
|
5
|
+
has_one :you
|
6
|
+
has_one :location
|
7
|
+
|
8
|
+
has_many :tagcrumbs
|
9
|
+
has_many :nearby_tagcrumbs
|
10
|
+
has_many :countries
|
11
|
+
|
12
|
+
has_many :popular_countries
|
13
|
+
has_many :tags
|
14
|
+
has_many :users
|
15
|
+
|
16
|
+
has_many :search
|
17
|
+
has_many :place_search
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# Each user has a Settings object that can be updated
|
3
|
+
class Settings < Model
|
4
|
+
can_be :updated
|
5
|
+
|
6
|
+
readable_attributes :created_at, :updated_at
|
7
|
+
writeable_attributes :locale, :unit_system, :notification_fan, :notification_recommendation, :newsletter, :facebook_new_placemark_feed,
|
8
|
+
:facebook_new_favorite_feed, :facebook_new_comment_feed
|
9
|
+
|
10
|
+
|
11
|
+
has_one :user
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# A State is a Place and connects Countries and Cities.
|
3
|
+
class State < Place
|
4
|
+
|
5
|
+
has_one :country
|
6
|
+
has_many :cities
|
7
|
+
|
8
|
+
# Search a State by name
|
9
|
+
def self.search(q, args={})
|
10
|
+
super(q, args.merge(:for => :state))
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# A Subscription is used to get updates of Placemarks
|
3
|
+
class Subscription < Model
|
4
|
+
can_be :created, :updated, :destroyed
|
5
|
+
|
6
|
+
readable_attributes :created_at, :updated_at
|
7
|
+
|
8
|
+
has_one :user
|
9
|
+
has_one :tagcrumb, :modifiable => true
|
10
|
+
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# Tagcrumb is the superclass for Placemark and Favorite, they are both a "Tagcrumb" as well
|
3
|
+
class Tagcrumb < Model
|
4
|
+
# Find Tagcrumbs by specifying filter criteria.
|
5
|
+
def self.find(args={})
|
6
|
+
args.assert_valid_keys(
|
7
|
+
:from, # from: you, friends, fans, recommendations, everybody, user
|
8
|
+
:username, # name of the user
|
9
|
+
:tags, # comma separates tag list
|
10
|
+
:tagcrumb_type, # Placemarks, Favorite or both?
|
11
|
+
:city, :state, :country, :middle_of_nowhere, # filter by the place hierarchy
|
12
|
+
:within, # within a certain radius
|
13
|
+
:latitude, :longitude, # per default within is the current location, can be overwritten with latitude and longitude
|
14
|
+
:bbox, # everything between a bounding box
|
15
|
+
:since, :until, # filter by time
|
16
|
+
:per_page, :page, :sort, :order # filter options
|
17
|
+
)
|
18
|
+
|
19
|
+
Array.load(Tagcrumbs.root.tagcrumbs_url, typecast_and_validate_query_parameters(args))
|
20
|
+
end
|
21
|
+
|
22
|
+
# Use a fulltext search to find Tagcrumbs.
|
23
|
+
# This is different from "finding" by filter criteria
|
24
|
+
def self.search(args={})
|
25
|
+
args.assert_valid_keys(:q, :from, :l, :w, :page, :per_page)
|
26
|
+
|
27
|
+
Array.load(Tagcrumbs.root.search_url, typecast_and_validate_query_parameters(args))
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def self.typecast_and_validate_query_parameters(args)
|
32
|
+
query_args = {}
|
33
|
+
|
34
|
+
args.each do |key, value|
|
35
|
+
query_args[key] = case key
|
36
|
+
when :from, :w: value.to_sym
|
37
|
+
when :tagcrumb_type: value.to_s.capitalize
|
38
|
+
when :since, :until: value.iso8601
|
39
|
+
else
|
40
|
+
value
|
41
|
+
end
|
42
|
+
|
43
|
+
Tagcrumbs.validate(key, query_args[key])
|
44
|
+
end
|
45
|
+
|
46
|
+
query_args
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# The main actor in the webservice.
|
3
|
+
class User < Model
|
4
|
+
readable_attributes :username, :created_at, :updated_at
|
5
|
+
|
6
|
+
has_one :profile
|
7
|
+
has_one :picture
|
8
|
+
has_one :settings
|
9
|
+
has_one :location
|
10
|
+
has_one :suggestions
|
11
|
+
|
12
|
+
has_many :tagcrumbs
|
13
|
+
has_many :placemarks
|
14
|
+
has_many :favorites
|
15
|
+
|
16
|
+
has_many :profile_links
|
17
|
+
|
18
|
+
has_many :fanships
|
19
|
+
has_many :friendships
|
20
|
+
|
21
|
+
has_many :recommendations
|
22
|
+
has_many :received_recommendations
|
23
|
+
|
24
|
+
has_many :filters
|
25
|
+
|
26
|
+
has_many :comments
|
27
|
+
has_many :links
|
28
|
+
has_many :subscriptions
|
29
|
+
|
30
|
+
has_many :tags
|
31
|
+
has_many :places
|
32
|
+
|
33
|
+
has_many :activities
|
34
|
+
|
35
|
+
has_many :tagcrumbs_friends
|
36
|
+
has_many :tagcrumbs_fans
|
37
|
+
|
38
|
+
# An array of all friends of a User (people the user is following)
|
39
|
+
def friends
|
40
|
+
array = []
|
41
|
+
|
42
|
+
friendships.each_page do |page|
|
43
|
+
array += page.map{|f| f.friend}
|
44
|
+
end
|
45
|
+
|
46
|
+
array
|
47
|
+
end
|
48
|
+
|
49
|
+
# An array of all fans of a User (people that follow this user)
|
50
|
+
def fans
|
51
|
+
array = []
|
52
|
+
|
53
|
+
fanships.each_page do |page|
|
54
|
+
array += page.map{|f| f.fan}
|
55
|
+
end
|
56
|
+
|
57
|
+
array
|
58
|
+
end
|
59
|
+
|
60
|
+
# The friendship with a specific user
|
61
|
+
def friendship_with(user)
|
62
|
+
f = Friendship.new
|
63
|
+
f.reload("/#{self.username}/friends/#{user}")
|
64
|
+
rescue Net::HTTPServerException => error
|
65
|
+
f.requestor.response.code == '404' ? nil : (raise error) # not found
|
66
|
+
end
|
67
|
+
|
68
|
+
# Is this user friend with user?
|
69
|
+
def friend_with?(user)
|
70
|
+
friendship_with(user) ? true : false
|
71
|
+
end
|
72
|
+
|
73
|
+
# The fanship with a specific user
|
74
|
+
def fanship_with(user)
|
75
|
+
Fanship.load("/#{self.username}/fans/#{user}")
|
76
|
+
rescue Net::HTTPServerException => error
|
77
|
+
self.requestor.response.code == '404' ? nil : (raise error) # not found
|
78
|
+
end
|
79
|
+
|
80
|
+
# Is this user a fan of user?
|
81
|
+
def fan_of?(user)
|
82
|
+
fanship_with(user) ? true : false
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
# Search a User by name or email
|
87
|
+
def self.search(q, args={})
|
88
|
+
Array.load(Tagcrumbs.root.users_url, args.merge(:u => q))
|
89
|
+
end
|
90
|
+
|
91
|
+
# Load a User by its username
|
92
|
+
def self.find_by_username(username)
|
93
|
+
User.load("/#{username}")
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_s
|
97
|
+
self.username
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# A Recommendation of a Placemark between two Users
|
3
|
+
class UserRecommendation < Model
|
4
|
+
can_be :created
|
5
|
+
|
6
|
+
readable_attributes :created_at, :updated_at
|
7
|
+
writeable_attributes :message
|
8
|
+
|
9
|
+
has_one :user
|
10
|
+
has_one :tagcrumb, :modifiable => true
|
11
|
+
has_one :recommended_user, :modifiable => true
|
12
|
+
|
13
|
+
|
14
|
+
def create_url
|
15
|
+
Tagcrumbs.current_user.recommendations_url # has to be overwritten
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
# Superclass for all other Resources
|
3
|
+
class Resource
|
4
|
+
attr_accessor :resource_url, :loaded
|
5
|
+
attr_writer :requestor
|
6
|
+
attr_reader :properties
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
reset!
|
10
|
+
end
|
11
|
+
|
12
|
+
# Clean up the state of the model and make it blank again
|
13
|
+
def reset!
|
14
|
+
self.resource_url = nil
|
15
|
+
self.loaded = false
|
16
|
+
self.properties = {}
|
17
|
+
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
# Is the Resource loaded or is it just a stub and the attributes still need to be loaded from the web?
|
22
|
+
def loaded?
|
23
|
+
self.loaded ? true : false
|
24
|
+
end
|
25
|
+
|
26
|
+
# A requestor that is auto-generated if needed to make requests to the webservice
|
27
|
+
def requestor
|
28
|
+
@requestor ||= Requestor.new
|
29
|
+
end
|
30
|
+
|
31
|
+
# Reset the Resource, reload the data from the web and initialize it again from the document
|
32
|
+
def reload(url = nil)
|
33
|
+
self.requestor.get(url || resource_url)
|
34
|
+
initialize_from_document(requestor.response.body, requestor.response_format)
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
# Set the properties and automatically set the resource_url
|
39
|
+
def properties=(properties)
|
40
|
+
self.resource_url = properties['xlink:href'] if properties['xlink:href']
|
41
|
+
@properties = properties.with_indifferent_access
|
42
|
+
end
|
43
|
+
|
44
|
+
# Load a Resource from a URL with optional query parameters
|
45
|
+
# The correct Resource class is automatically determined.
|
46
|
+
def self.load(path, query_string = {}, requestor_options={})
|
47
|
+
requestor = Requestor.new(requestor_options)
|
48
|
+
requestor.get(path, query_string)
|
49
|
+
|
50
|
+
resource = new_from_document(requestor.response.body, requestor.response_format)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Create a new Resource from a document.
|
54
|
+
# The correct Resource class is automatically determined.
|
55
|
+
def self.new_from_document(document, format = Tagcrumbs.options[:format])
|
56
|
+
parser = Parser.new_for_format(document, format)
|
57
|
+
|
58
|
+
resource = Tagcrumbs.class_by_type(parser.get_attribute('type')).new
|
59
|
+
resource.initialize_from_document(parser, format)
|
60
|
+
|
61
|
+
resource
|
62
|
+
end
|
63
|
+
|
64
|
+
# Initialize the object with data from the document
|
65
|
+
def initialize_from_document(document, format = Tagcrumbs.options[:format])
|
66
|
+
parser = Parser.new_for_format(document, format)
|
67
|
+
reset!
|
68
|
+
|
69
|
+
self.loaded = parser.node_loaded?
|
70
|
+
self.properties = parser.get_attributes || {}
|
71
|
+
|
72
|
+
parser
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,301 @@
|
|
1
|
+
module Tagcrumbs
|
2
|
+
class ResourceInvalid# < ClientError #:nodoc:
|
3
|
+
end
|
4
|
+
|
5
|
+
# == Borrowed from ActiveResource
|
6
|
+
|
7
|
+
# Active Resource validation is reported to and from this object, which is used by Base#save
|
8
|
+
# to determine whether the object in a valid state to be saved. See usage example in Validations.
|
9
|
+
class Errors
|
10
|
+
include Enumerable
|
11
|
+
attr_reader :errors
|
12
|
+
|
13
|
+
delegate :empty?, :to => :errors
|
14
|
+
|
15
|
+
def initialize(base) # :nodoc:
|
16
|
+
@base, @errors = base, {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# Add an error to the base Active Resource object rather than an attribute.
|
20
|
+
#
|
21
|
+
# ==== Examples
|
22
|
+
# my_folder = Folder.find(1)
|
23
|
+
# my_folder.errors.add_to_base("You can't edit an existing folder")
|
24
|
+
# my_folder.errors.on_base
|
25
|
+
# # => "You can't edit an existing folder"
|
26
|
+
#
|
27
|
+
# my_folder.errors.add_to_base("This folder has been tagged as frozen")
|
28
|
+
# my_folder.valid?
|
29
|
+
# # => false
|
30
|
+
# my_folder.errors.on_base
|
31
|
+
# # => ["You can't edit an existing folder", "This folder has been tagged as frozen"]
|
32
|
+
#
|
33
|
+
def add_to_base(msg)
|
34
|
+
add(:base, msg)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Adds an error to an Active Resource object's attribute (named for the +attribute+ parameter)
|
38
|
+
# with the error message in +msg+.
|
39
|
+
#
|
40
|
+
# ==== Examples
|
41
|
+
# my_resource = Node.find(1)
|
42
|
+
# my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base'
|
43
|
+
# my_resource.errors.on('name')
|
44
|
+
# # => 'can not be "base"!'
|
45
|
+
#
|
46
|
+
# my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == ''
|
47
|
+
# my_resource.valid?
|
48
|
+
# # => false
|
49
|
+
# my_resource.errors.on('desc')
|
50
|
+
# # => 'can not be blank!'
|
51
|
+
#
|
52
|
+
def add(attribute, msg)
|
53
|
+
@errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
|
54
|
+
@errors[attribute.to_s] << msg
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns true if the specified +attribute+ has errors associated with it.
|
58
|
+
#
|
59
|
+
# ==== Examples
|
60
|
+
# my_resource = Disk.find(1)
|
61
|
+
# my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main'
|
62
|
+
# my_resource.errors.on('location')
|
63
|
+
# # => 'must be Main!'
|
64
|
+
#
|
65
|
+
# my_resource.errors.invalid?('location')
|
66
|
+
# # => true
|
67
|
+
# my_resource.errors.invalid?('name')
|
68
|
+
# # => false
|
69
|
+
def invalid?(attribute)
|
70
|
+
!@errors[attribute.to_s].nil?
|
71
|
+
end
|
72
|
+
|
73
|
+
# A method to return the errors associated with +attribute+, which returns nil, if no errors are
|
74
|
+
# associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
|
75
|
+
# or an array of error messages if more than one error is associated with the specified +attribute+.
|
76
|
+
#
|
77
|
+
# ==== Examples
|
78
|
+
# my_person = Person.new(params[:person])
|
79
|
+
# my_person.errors.on('login')
|
80
|
+
# # => nil
|
81
|
+
#
|
82
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
83
|
+
# my_person.errors.on('login')
|
84
|
+
# # => 'can not be empty'
|
85
|
+
#
|
86
|
+
# my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10
|
87
|
+
# my_person.errors.on('login')
|
88
|
+
# # => ['can not be empty', 'can not be longer than 10 characters']
|
89
|
+
def on(attribute)
|
90
|
+
errors = @errors[attribute.to_s]
|
91
|
+
return nil if errors.nil?
|
92
|
+
errors.size == 1 ? errors.first : errors
|
93
|
+
end
|
94
|
+
|
95
|
+
alias :[] :on
|
96
|
+
|
97
|
+
# A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are
|
98
|
+
# associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
|
99
|
+
# or an array of error messages if more than one error is associated with the specified +attribute+.
|
100
|
+
#
|
101
|
+
# ==== Examples
|
102
|
+
# my_account = Account.find(1)
|
103
|
+
# my_account.errors.on_base
|
104
|
+
# # => nil
|
105
|
+
#
|
106
|
+
# my_account.errors.add_to_base("This account is frozen")
|
107
|
+
# my_account.errors.on_base
|
108
|
+
# # => "This account is frozen"
|
109
|
+
#
|
110
|
+
# my_account.errors.add_to_base("This account has been closed")
|
111
|
+
# my_account.errors.on_base
|
112
|
+
# # => ["This account is frozen", "This account has been closed"]
|
113
|
+
#
|
114
|
+
def on_base
|
115
|
+
on(:base)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Yields each attribute and associated message per error added.
|
119
|
+
#
|
120
|
+
# ==== Examples
|
121
|
+
# my_person = Person.new(params[:person])
|
122
|
+
#
|
123
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
124
|
+
# my_person.errors.add('password', 'can not be empty') if my_person.password == ''
|
125
|
+
# messages = ''
|
126
|
+
# my_person.errors.each {|attr, msg| messages += attr.to_s.humanize + " " + msg + "<br />"}
|
127
|
+
# messages
|
128
|
+
# # => "Login can not be empty<br />Password can not be empty<br />"
|
129
|
+
#
|
130
|
+
def each
|
131
|
+
@errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
|
132
|
+
end
|
133
|
+
|
134
|
+
# Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned
|
135
|
+
# through iteration as "First name can't be empty".
|
136
|
+
#
|
137
|
+
# ==== Examples
|
138
|
+
# my_person = Person.new(params[:person])
|
139
|
+
#
|
140
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
141
|
+
# my_person.errors.add('password', 'can not be empty') if my_person.password == ''
|
142
|
+
# messages = ''
|
143
|
+
# my_person.errors.each_full {|msg| messages += msg + "<br/>"}
|
144
|
+
# messages
|
145
|
+
# # => "Login can not be empty<br />Password can not be empty<br />"
|
146
|
+
#
|
147
|
+
def each_full
|
148
|
+
full_messages.each { |msg| yield msg }
|
149
|
+
end
|
150
|
+
|
151
|
+
# Returns all the full error messages in an array.
|
152
|
+
#
|
153
|
+
# ==== Examples
|
154
|
+
# my_person = Person.new(params[:person])
|
155
|
+
#
|
156
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
157
|
+
# my_person.errors.add('password', 'can not be empty') if my_person.password == ''
|
158
|
+
# messages = ''
|
159
|
+
# my_person.errors.full_messages.each {|msg| messages += msg + "<br/>"}
|
160
|
+
# messages
|
161
|
+
# # => "Login can not be empty<br />Password can not be empty<br />"
|
162
|
+
#
|
163
|
+
def full_messages
|
164
|
+
full_messages = []
|
165
|
+
|
166
|
+
@errors.each_key do |attr|
|
167
|
+
@errors[attr].each do |msg|
|
168
|
+
next if msg.nil?
|
169
|
+
|
170
|
+
if attr == "base"
|
171
|
+
full_messages << msg
|
172
|
+
else
|
173
|
+
full_messages << [attr.to_s.humanize, msg].join(' ')
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
full_messages
|
178
|
+
end
|
179
|
+
|
180
|
+
def clear
|
181
|
+
@errors = {}
|
182
|
+
end
|
183
|
+
|
184
|
+
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such
|
185
|
+
# with this as well.
|
186
|
+
#
|
187
|
+
# ==== Examples
|
188
|
+
# my_person = Person.new(params[:person])
|
189
|
+
# my_person.errors.size
|
190
|
+
# # => 0
|
191
|
+
#
|
192
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
193
|
+
# my_person.errors.add('password', 'can not be empty') if my_person.password == ''
|
194
|
+
# my_person.error.size
|
195
|
+
# # => 2
|
196
|
+
#
|
197
|
+
def size
|
198
|
+
@errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
|
199
|
+
end
|
200
|
+
|
201
|
+
alias_method :count, :size
|
202
|
+
alias_method :length, :size
|
203
|
+
|
204
|
+
def from_format(document, format)
|
205
|
+
case format
|
206
|
+
when :xml: from_xml(document)
|
207
|
+
when :json: from_json(document)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Grabs errors from the XML response.
|
212
|
+
def from_xml(xml)
|
213
|
+
clear
|
214
|
+
humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.to_s.humanize => attr_name) }
|
215
|
+
messages = ::Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
|
216
|
+
messages.each do |message|
|
217
|
+
attr_message = humanized_attributes.keys.detect do |attr_name|
|
218
|
+
if message[0, attr_name.size + 1] == "#{attr_name} "
|
219
|
+
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
add_to_base message if attr_message.nil?
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# Grabs errors from the JSON response.
|
228
|
+
# Added for Tagcrumbs
|
229
|
+
def from_json(json)
|
230
|
+
clear
|
231
|
+
humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.to_s.humanize => attr_name) }
|
232
|
+
|
233
|
+
messages = JSON.parse(json)['errors']['error'] rescue []
|
234
|
+
messages.each do |message|
|
235
|
+
attr_message = humanized_attributes.keys.detect do |attr_name|
|
236
|
+
if message[0, attr_name.size + 1] == "#{attr_name} "
|
237
|
+
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
add_to_base message if attr_message.nil?
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
# Module to support validation and errors with Active Resource objects. The module overrides
|
247
|
+
# Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
|
248
|
+
# in the web service response. The module also adds an +errors+ collection that mimics the interface
|
249
|
+
# of the errors provided by ActiveRecord::Errors.
|
250
|
+
#
|
251
|
+
# ==== Example
|
252
|
+
#
|
253
|
+
# Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
|
254
|
+
# <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model:
|
255
|
+
#
|
256
|
+
# person = Person.new(:first_name => "Jim", :last_name => "")
|
257
|
+
# person.save # => false (server returns an HTTP 422 status code and errors)
|
258
|
+
# person.valid? # => false
|
259
|
+
# person.errors.empty? # => false
|
260
|
+
# person.errors.count # => 1
|
261
|
+
# person.errors.full_messages # => ["Last name can't be empty"]
|
262
|
+
# person.errors.on(:last_name) # => "can't be empty"
|
263
|
+
# person.last_name = "Halpert"
|
264
|
+
# person.save # => true (and person is now saved to the remote service)
|
265
|
+
#
|
266
|
+
module Validations
|
267
|
+
# def self.included(base) # :nodoc:
|
268
|
+
# base.class_eval do
|
269
|
+
# alias_method_chain :save, :validation
|
270
|
+
# end
|
271
|
+
# end
|
272
|
+
#
|
273
|
+
# # Validate a resource and save (POST) it to the remote web service.
|
274
|
+
# def save_with_validation
|
275
|
+
# save_without_validation
|
276
|
+
# true
|
277
|
+
# rescue ResourceInvalid => error
|
278
|
+
# errors.from_xml(error.response.body)
|
279
|
+
# false
|
280
|
+
# end
|
281
|
+
|
282
|
+
# Checks for errors on an object (i.e., is resource.errors empty?).
|
283
|
+
#
|
284
|
+
# ==== Examples
|
285
|
+
# my_person = Person.create(params[:person])
|
286
|
+
# my_person.valid?
|
287
|
+
# # => true
|
288
|
+
#
|
289
|
+
# my_person.errors.add('login', 'can not be empty') if my_person.login == ''
|
290
|
+
# my_person.valid?
|
291
|
+
# # => false
|
292
|
+
def valid?
|
293
|
+
errors.empty?
|
294
|
+
end
|
295
|
+
|
296
|
+
# Returns the Errors object that holds all information about attribute error messages.
|
297
|
+
def errors
|
298
|
+
@errors ||= Errors.new(self)
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|