tagcrumbs-tagcrumbs 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/History.txt +9 -0
  2. data/License.txt +22 -0
  3. data/Manifest.txt +177 -0
  4. data/PostInstall.txt +2 -0
  5. data/README.rdoc +45 -0
  6. data/Rakefile +35 -0
  7. data/TODO.txt +17 -0
  8. data/bin/tagcrumbs +5 -0
  9. data/examples/address_update.rb +15 -0
  10. data/examples/comment_new.rb +19 -0
  11. data/examples/favorite_new.rb +17 -0
  12. data/examples/filter_new.rb +15 -0
  13. data/examples/friendship_create.rb +19 -0
  14. data/examples/link_new.rb +16 -0
  15. data/examples/location_update.rb +16 -0
  16. data/examples/picture_update.rb +16 -0
  17. data/examples/placemark_new_simple.rb +24 -0
  18. data/examples/placemark_new_suggestions.rb +24 -0
  19. data/examples/profile_link_new.rb +14 -0
  20. data/examples/profile_update.rb +12 -0
  21. data/examples/request_authorization.rb +25 -0
  22. data/examples/settings_update.rb +12 -0
  23. data/examples/subscription_new.rb +15 -0
  24. data/examples/tagcrumbs_find.rb +25 -0
  25. data/examples/tagcrumbs_search.rb +7 -0
  26. data/examples/user.rb +28 -0
  27. data/examples/user_recommendation_new.rb +15 -0
  28. data/lib/tagcrumbs.rb +232 -0
  29. data/lib/tagcrumbs/builders/builder.rb +45 -0
  30. data/lib/tagcrumbs/builders/json_builder.rb +38 -0
  31. data/lib/tagcrumbs/builders/xml_builder.rb +30 -0
  32. data/lib/tagcrumbs/cli.rb +268 -0
  33. data/lib/tagcrumbs/config_store.rb +35 -0
  34. data/lib/tagcrumbs/exceptions.rb +13 -0
  35. data/lib/tagcrumbs/node.rb +43 -0
  36. data/lib/tagcrumbs/parsers/json_parser.rb +79 -0
  37. data/lib/tagcrumbs/parsers/parser.rb +24 -0
  38. data/lib/tagcrumbs/parsers/xml_parser.rb +54 -0
  39. data/lib/tagcrumbs/proxy.rb +32 -0
  40. data/lib/tagcrumbs/requestor.rb +140 -0
  41. data/lib/tagcrumbs/resources/array.rb +108 -0
  42. data/lib/tagcrumbs/resources/models/accessors.rb +252 -0
  43. data/lib/tagcrumbs/resources/models/activity.rb +19 -0
  44. data/lib/tagcrumbs/resources/models/address.rb +16 -0
  45. data/lib/tagcrumbs/resources/models/city.rb +13 -0
  46. data/lib/tagcrumbs/resources/models/comment.rb +12 -0
  47. data/lib/tagcrumbs/resources/models/country.rb +15 -0
  48. data/lib/tagcrumbs/resources/models/fanship.rb +12 -0
  49. data/lib/tagcrumbs/resources/models/favorite.rb +22 -0
  50. data/lib/tagcrumbs/resources/models/filter.rb +32 -0
  51. data/lib/tagcrumbs/resources/models/flag.rb +10 -0
  52. data/lib/tagcrumbs/resources/models/friendship.rb +12 -0
  53. data/lib/tagcrumbs/resources/models/geoname.rb +8 -0
  54. data/lib/tagcrumbs/resources/models/link.rb +13 -0
  55. data/lib/tagcrumbs/resources/models/location.rb +13 -0
  56. data/lib/tagcrumbs/resources/models/model.rb +148 -0
  57. data/lib/tagcrumbs/resources/models/picture.rb +28 -0
  58. data/lib/tagcrumbs/resources/models/place.rb +17 -0
  59. data/lib/tagcrumbs/resources/models/placemark.rb +30 -0
  60. data/lib/tagcrumbs/resources/models/profile.rb +16 -0
  61. data/lib/tagcrumbs/resources/models/profile_link.rb +12 -0
  62. data/lib/tagcrumbs/resources/models/root.rb +19 -0
  63. data/lib/tagcrumbs/resources/models/settings.rb +14 -0
  64. data/lib/tagcrumbs/resources/models/state.rb +14 -0
  65. data/lib/tagcrumbs/resources/models/subscription.rb +13 -0
  66. data/lib/tagcrumbs/resources/models/suggestions.rb +11 -0
  67. data/lib/tagcrumbs/resources/models/tag.rb +6 -0
  68. data/lib/tagcrumbs/resources/models/tagcrumb.rb +58 -0
  69. data/lib/tagcrumbs/resources/models/user.rb +101 -0
  70. data/lib/tagcrumbs/resources/models/user_recommendation.rb +18 -0
  71. data/lib/tagcrumbs/resources/resource.rb +76 -0
  72. data/lib/tagcrumbs/validations.rb +301 -0
  73. data/script/console +10 -0
  74. data/script/destroy +14 -0
  75. data/script/generate +14 -0
  76. data/spec/fixtures/activity.json +210 -0
  77. data/spec/fixtures/activity.xml +66 -0
  78. data/spec/fixtures/address.json +183 -0
  79. data/spec/fixtures/address.xml +58 -0
  80. data/spec/fixtures/city.json +88 -0
  81. data/spec/fixtures/city.xml +34 -0
  82. data/spec/fixtures/comment.json +286 -0
  83. data/spec/fixtures/comment.xml +87 -0
  84. data/spec/fixtures/country.json +32 -0
  85. data/spec/fixtures/country.xml +13 -0
  86. data/spec/fixtures/fanship.json +180 -0
  87. data/spec/fixtures/fanship.xml +54 -0
  88. data/spec/fixtures/favorite.json +332 -0
  89. data/spec/fixtures/favorite.xml +109 -0
  90. data/spec/fixtures/filter.json +243 -0
  91. data/spec/fixtures/filter.xml +80 -0
  92. data/spec/fixtures/friendship.json +180 -0
  93. data/spec/fixtures/friendship.xml +54 -0
  94. data/spec/fixtures/geoname.json +18 -0
  95. data/spec/fixtures/geoname.xml +6 -0
  96. data/spec/fixtures/json_parser.json +25 -0
  97. data/spec/fixtures/link.json +288 -0
  98. data/spec/fixtures/link.xml +88 -0
  99. data/spec/fixtures/location.json +204 -0
  100. data/spec/fixtures/location.xml +71 -0
  101. data/spec/fixtures/picture.json +154 -0
  102. data/spec/fixtures/picture.png +0 -0
  103. data/spec/fixtures/picture.xml +49 -0
  104. data/spec/fixtures/placemark.json +357 -0
  105. data/spec/fixtures/placemark.xml +58 -0
  106. data/spec/fixtures/placemarks.json +887 -0
  107. data/spec/fixtures/placemarks.xml +348 -0
  108. data/spec/fixtures/profile.json +282 -0
  109. data/spec/fixtures/profile.xml +100 -0
  110. data/spec/fixtures/profile_link.json +156 -0
  111. data/spec/fixtures/profile_link.xml +49 -0
  112. data/spec/fixtures/root.json +46 -0
  113. data/spec/fixtures/root.xml +14 -0
  114. data/spec/fixtures/settings.json +147 -0
  115. data/spec/fixtures/settings.xml +45 -0
  116. data/spec/fixtures/state.json +62 -0
  117. data/spec/fixtures/state.xml +23 -0
  118. data/spec/fixtures/subscription.json +283 -0
  119. data/spec/fixtures/subscription.xml +86 -0
  120. data/spec/fixtures/suggestions.json +1877 -0
  121. data/spec/fixtures/suggestions.xml +724 -0
  122. data/spec/fixtures/tag.json +9 -0
  123. data/spec/fixtures/tag.xml +4 -0
  124. data/spec/fixtures/user.json +105 -0
  125. data/spec/fixtures/user.xml +31 -0
  126. data/spec/fixtures/user_recommendation.json +349 -0
  127. data/spec/fixtures/user_recommendation.xml +106 -0
  128. data/spec/fixtures/validation_errors.json +5 -0
  129. data/spec/fixtures/validation_errors.xml +5 -0
  130. data/spec/fixtures/xml_parser.xml +13 -0
  131. data/spec/spec.opts +1 -0
  132. data/spec/spec_helper.rb +75 -0
  133. data/spec/tagcrumbs/builders/builder_spec.rb +57 -0
  134. data/spec/tagcrumbs/builders/json_builder_spec.rb +47 -0
  135. data/spec/tagcrumbs/builders/xml_builder_spec.rb +34 -0
  136. data/spec/tagcrumbs/exceptions_spec.rb +16 -0
  137. data/spec/tagcrumbs/node_spec.rb +42 -0
  138. data/spec/tagcrumbs/parsers/json_parser_spec.rb +117 -0
  139. data/spec/tagcrumbs/parsers/parser_spec.rb +25 -0
  140. data/spec/tagcrumbs/parsers/xml_parser_spec.rb +117 -0
  141. data/spec/tagcrumbs/proxy_spec.rb +48 -0
  142. data/spec/tagcrumbs/requestor_spec.rb +62 -0
  143. data/spec/tagcrumbs/resources/array_spec.rb +62 -0
  144. data/spec/tagcrumbs/resources/models/accessors_spec.rb +123 -0
  145. data/spec/tagcrumbs/resources/models/activity_spec.rb +33 -0
  146. data/spec/tagcrumbs/resources/models/address_spec.rb +24 -0
  147. data/spec/tagcrumbs/resources/models/city_spec.rb +33 -0
  148. data/spec/tagcrumbs/resources/models/comment_spec.rb +23 -0
  149. data/spec/tagcrumbs/resources/models/country_spec.rb +26 -0
  150. data/spec/tagcrumbs/resources/models/fanship_spec.rb +28 -0
  151. data/spec/tagcrumbs/resources/models/favorite_spec.rb +28 -0
  152. data/spec/tagcrumbs/resources/models/filter_spec.rb +43 -0
  153. data/spec/tagcrumbs/resources/models/flag_spec.rb +5 -0
  154. data/spec/tagcrumbs/resources/models/friendship_spec.rb +28 -0
  155. data/spec/tagcrumbs/resources/models/geoname_spec.rb +18 -0
  156. data/spec/tagcrumbs/resources/models/link_spec.rb +24 -0
  157. data/spec/tagcrumbs/resources/models/location_spec.rb +28 -0
  158. data/spec/tagcrumbs/resources/models/model_spec.rb +27 -0
  159. data/spec/tagcrumbs/resources/models/picture_spec.rb +42 -0
  160. data/spec/tagcrumbs/resources/models/place_spec.rb +12 -0
  161. data/spec/tagcrumbs/resources/models/placemark_spec.rb +61 -0
  162. data/spec/tagcrumbs/resources/models/profile_link_spec.rb +29 -0
  163. data/spec/tagcrumbs/resources/models/profile_spec.rb +43 -0
  164. data/spec/tagcrumbs/resources/models/root_spec.rb +57 -0
  165. data/spec/tagcrumbs/resources/models/settings_spec.rb +32 -0
  166. data/spec/tagcrumbs/resources/models/state_spec.rb +30 -0
  167. data/spec/tagcrumbs/resources/models/subscription_spec.rb +28 -0
  168. data/spec/tagcrumbs/resources/models/suggestions_spec.rb +33 -0
  169. data/spec/tagcrumbs/resources/models/tag_spec.rb +16 -0
  170. data/spec/tagcrumbs/resources/models/tagcrumb_spec.rb +21 -0
  171. data/spec/tagcrumbs/resources/models/user_recommendation_spec.rb +35 -0
  172. data/spec/tagcrumbs/resources/models/user_spec.rb +128 -0
  173. data/spec/tagcrumbs/resources/resource_spec.rb +62 -0
  174. data/spec/tagcrumbs/validations_spec.rb +27 -0
  175. data/spec/tagcrumbs_spec.rb +103 -0
  176. data/tagcrumbs.gemspec +59 -0
  177. data/tasks/rspec.rake +21 -0
  178. metadata +325 -0
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ pic = Tagcrumbs.current_user.picture
6
+
7
+ pic.get_file(ENV['HOME'] + '/Pictures/ich/ich.jpg')
8
+
9
+ if pic.save
10
+ puts "Uploaded a new Picture!"
11
+ else
12
+ puts "There were errors when saving the Picture:"
13
+ puts pic.errors.full_messages.join("\n")
14
+ end
15
+
16
+ # pic.destroy
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+
6
+ # Just create a Placemark without any suggestions
7
+ placemark = Tagcrumbs::Placemark.new(:name => "Tagcrumbs Gem Test", :notes => "My Notes", :tag_list => 'tag1, tag2',
8
+ :private => true, :latitude => 49.12, :longitude => 9.01, :guess_city => true,
9
+ :address_attributes => {
10
+ :street_address => 'Street Name',
11
+ :postal_code => '1234',
12
+ :city => 'City Name',
13
+ :state => 'State Name',
14
+ :country => 'Country Name'
15
+ })
16
+
17
+ if placemark.save
18
+ puts "Saved Placemark successfully at #{placemark.created_at}. URL: #{placemark.resource_url}"
19
+ else
20
+ puts "There were errors when saving the Placemark:"
21
+ puts placemark.errors.full_messages.join("\n")
22
+ end
23
+
24
+ # placemark.destroy # would destroy the placemark
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ # Create a Placemark with Suggestions
6
+ placemark = Tagcrumbs::Placemark.new
7
+
8
+ # Latitude and Longitude must be set first to get suggestions
9
+ placemark.latitude = 49
10
+ placemark.longitude = 9
11
+
12
+ suggestions = placemark.suggestions # load suggestions from the webservice
13
+
14
+ # Of course you would let the user pick from the suggestions
15
+ placemark.name = suggestions.names.first || 'no suggestions found'
16
+ placemark.city = suggestions.cities.first
17
+ placemark.tag_list = suggestions.popular_tags.first.name
18
+
19
+ if placemark.save
20
+ puts "Saved Placemark successfully at #{placemark.created_at}. URL: #{placemark.resource_url}"
21
+ else
22
+ puts "There were errors when saving the Placemark:"
23
+ puts placemark.errors.full_messages.join("\n")
24
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ profile_link = Tagcrumbs::ProfileLink.new(:url => 'http://www.tagcrumbs.com', :link_type => 'Homepage')
6
+
7
+ if profile_link.save
8
+ puts "Saved profile link. URL: #{profile_link.resource_url}"
9
+ else
10
+ puts "Could not save profile link."
11
+ puts profile_link.errors.full_messages.join("\n")
12
+ end
13
+
14
+ # profile_link.destroy
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ Tagcrumbs.current_user.profile.fullname = 'Testing Webservice'
6
+ Tagcrumbs.current_user.profile.city = Tagcrumbs::City.search('stuttgart').first
7
+
8
+ if Tagcrumbs.current_user.profile.save
9
+ puts "Updated Fullname"
10
+ else
11
+ puts "Could not update Fullname."
12
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/../lib/tagcrumbs'
4
+
5
+ config_store = Tagcrumbs::ConfigStore.load(ENV['HOME'] + '/.tagcrumbs')
6
+ Tagcrumbs.configure(config_store.options) # you can also set up your own consumer credentials at this point
7
+
8
+ unless Tagcrumbs.valid_credentials?
9
+ puts "Please authorize the Gem to have access to your Tagcrumbs account on: #{Tagcrumbs.authorize_url}"
10
+
11
+ %x(open #{Tagcrumbs.authorize_url}) # open browser
12
+
13
+ print "Press Enter to continue..."
14
+ gets
15
+
16
+ if Tagcrumbs.exchange_request_token!
17
+ puts "Authorization succeeded."
18
+
19
+ config_store.update(Tagcrumbs.options.slice(:access_token, :access_secret))
20
+
21
+ else
22
+ puts "Authorization failed."
23
+ end
24
+
25
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ Tagcrumbs.current_user.settings.locale = 'en-US'
6
+
7
+ if Tagcrumbs.current_user.settings.save
8
+ puts "Updated settings successfully"
9
+ else
10
+ puts "Could not update settings"
11
+ puts Tagcrumbs.current_user.settings.errors.full_messages.join("\n")
12
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ placemark = Tagcrumbs::Placemark.find(:from => :friends, :per_page => 1).first # get the last placemark of your friends
6
+ subscription = Tagcrumbs::Subscription.new(:placemark => placemark)
7
+
8
+ if subscription.save
9
+ puts "Saved subscription. URL: #{subscription.resource_url}"
10
+ else
11
+ puts "Could not save subscription."
12
+ puts subscription.errors.full_messages.join("\n")
13
+ end
14
+
15
+ # profile_link.destroy
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/../lib/tagcrumbs' # this would not be needed if we only search for public Tagcrumbs
4
+
5
+ # Find the latest Tagcrumbs in Stuttgart, Germany
6
+ stuttgart = Tagcrumbs::City.search('stuttgart').first
7
+ in_stuttgart = Tagcrumbs::Tagcrumb.find(:from => :everybody, :city => stuttgart)
8
+
9
+ puts "Total Tagcrumbs in Stuttgart: #{in_stuttgart.total_entries}"
10
+
11
+ # Find Favorites of one user in the last two weeks
12
+ from_cornelius = Tagcrumbs::Tagcrumb.find(:username => 'cornelius', :from => :user, :since => 2.weeks.ago)
13
+ puts "Total Favorites of Cornelius in the last two weeks: #{from_cornelius.total_entries}"
14
+
15
+ # Find all Placemarks near the current location
16
+ nearby = Tagcrumbs::Tagcrumb.find(:from => :everybody, :within => 0.55)
17
+ puts "A total of #{nearby.total_entries} Tagcrumbs near your current location"
18
+
19
+ # Find all Placemarks near any other location
20
+ nearby_other = Tagcrumbs::Tagcrumb.find(:from => :everybody, :within => 0.55, :latitude => 49, :longitude => 9, :sort => :distance, :order => :asc, :per_page => 50)
21
+ puts "A total of #{nearby_other.total_entries} Tagcrumbs nearby (49, 9)"
22
+
23
+ # Find Placemarks with certain tags
24
+ tagged = Tagcrumbs::Tagcrumb.find(:from => :friends, :tags => 'bar, beer')
25
+ puts "Your friends have #{tagged.total_entries} Tagcrumbs tagged bar and beer."
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/../lib/tagcrumbs' # this would not be needed if we only search for public Tagcrumbs
4
+
5
+ # This is doing a fulltext search, searching name, tags, description, etc.
6
+ restaurants = Tagcrumbs::Tagcrumb.search(:q => 'restaurant', :l => 'berlin', :w => 'city')
7
+ puts "#{restaurants.total_entries} Placemarks that contain the word 'restaurant' in Berlin."
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ user = Tagcrumbs.current_user
6
+
7
+ puts "== Current active user: #{user}, Fullname: #{user.profile.fullname}"
8
+
9
+ puts "== You have #{user.tagcrumbs.total_entries} Tagcrumbs."
10
+
11
+ puts "== Your friends:"
12
+
13
+ user.friends.each do |friend|
14
+ puts friend.username
15
+ end
16
+
17
+ puts "== Your last comments:"
18
+
19
+ user.comments.each do |comment|
20
+ puts comment.text
21
+ end
22
+
23
+ puts "== Your last activities:"
24
+
25
+ user.activities.each do |activity|
26
+ puts "Type: #{activity.event_type}, Subject: #{activity.subject.resource_url} Secondary Subject: #{activity.secondary_subject ? activity.secondary_subject.resource_url : 'none'}"
27
+ end
28
+
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ require File.dirname(__FILE__) + '/request_authorization'
4
+
5
+ placemark = Tagcrumbs.current_user.placemarks.first
6
+ friend = Tagcrumbs.current_user.friends.first
7
+
8
+ recommendation = Tagcrumbs::UserRecommendation.new(:placemark => placemark, :recommended_user => friend, :message => 'very interesting place, check it out!')
9
+
10
+ if recommendation.save
11
+ puts "You sent a recommendation to #{friend}!"
12
+ else
13
+ puts "Could not sent a recommendation to #{friend}!"
14
+ puts recommendation.errors.full_messages.join("\n")
15
+ end
@@ -0,0 +1,232 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ #require 'ruby-debug'
6
+
7
+ gem 'activesupport', '2.3.2'
8
+ require 'active_support'
9
+
10
+ gem 'hpricot', '0.8.1'
11
+ require 'hpricot'
12
+
13
+ gem 'htmlentities', '4.0.0'
14
+ require 'htmlentities'
15
+
16
+ gem 'json', '1.1.6'
17
+ require 'JSON'
18
+
19
+ gem 'oauth', '0.3.4'
20
+ require 'oauth'
21
+
22
+ gem 'mime-types', '1.16'
23
+ require 'mime/types'
24
+
25
+ gem 'builder', '2.1.2'
26
+ require 'builder'
27
+
28
+ # Inflections must be available before classes are loaded, needed to automatically find the correct classes
29
+ ActiveSupport::Inflector.inflections do |inflect|
30
+ inflect.irregular 'address', 'address'
31
+ inflect.uncountable "settings"
32
+ inflect.uncountable "suggestions"
33
+ end
34
+
35
+ require 'tagcrumbs/validations'
36
+ require 'tagcrumbs/config_store'
37
+ require 'tagcrumbs/exceptions'
38
+
39
+ # parsers
40
+ require 'tagcrumbs/parsers/parser'
41
+ require 'tagcrumbs/parsers/json_parser'
42
+ require 'tagcrumbs/parsers/xml_parser'
43
+
44
+ # builders
45
+ require 'tagcrumbs/builders/builder'
46
+ require 'tagcrumbs/builders/json_builder'
47
+ require 'tagcrumbs/builders/xml_builder'
48
+
49
+ # general
50
+ require 'tagcrumbs/node'
51
+ require 'tagcrumbs/requestor'
52
+ require 'tagcrumbs/proxy'
53
+
54
+ # resources
55
+ require 'tagcrumbs/resources/resource'
56
+ require 'tagcrumbs/resources/array'
57
+
58
+ # models
59
+ require 'tagcrumbs/resources/models/accessors'
60
+ require 'tagcrumbs/resources/models/model'
61
+ require 'tagcrumbs/resources/models/tagcrumb'
62
+ require 'tagcrumbs/resources/models/place'
63
+
64
+ require 'tagcrumbs/resources/models/address'
65
+ require 'tagcrumbs/resources/models/activity'
66
+ require 'tagcrumbs/resources/models/city'
67
+ require 'tagcrumbs/resources/models/comment'
68
+ require 'tagcrumbs/resources/models/country'
69
+ require 'tagcrumbs/resources/models/fanship'
70
+ require 'tagcrumbs/resources/models/favorite'
71
+ require 'tagcrumbs/resources/models/filter'
72
+ require 'tagcrumbs/resources/models/flag'
73
+ require 'tagcrumbs/resources/models/friendship'
74
+ require 'tagcrumbs/resources/models/geoname'
75
+ require 'tagcrumbs/resources/models/link'
76
+ require 'tagcrumbs/resources/models/location'
77
+ require 'tagcrumbs/resources/models/picture'
78
+ require 'tagcrumbs/resources/models/placemark'
79
+ require 'tagcrumbs/resources/models/profile'
80
+ require 'tagcrumbs/resources/models/profile_link'
81
+ require 'tagcrumbs/resources/models/root'
82
+ require 'tagcrumbs/resources/models/settings'
83
+ require 'tagcrumbs/resources/models/state'
84
+ require 'tagcrumbs/resources/models/subscription'
85
+ require 'tagcrumbs/resources/models/suggestions'
86
+ require 'tagcrumbs/resources/models/tag'
87
+ require 'tagcrumbs/resources/models/user'
88
+ require 'tagcrumbs/resources/models/user_recommendation'
89
+
90
+
91
+ module Tagcrumbs
92
+ VERSION = '0.3.1'
93
+
94
+ class << self
95
+ # Returns the default configuration options of the gem. You can overwrite the options with configure
96
+ def default_options
97
+ {
98
+ :consumer_token => 'ZQAR3nW5D3S5JgU8EAKEfA',
99
+ :consumer_secret => 'vMc7NA77eraJc9TwyImshEEMVgrEy4C8YURVlwoyrHE',
100
+ :format => :xml,
101
+ :consumer_options =>
102
+ {
103
+ :site => 'http://www.tagcrumbs.com'
104
+ #:site => 'http://localhost:2999'
105
+ }
106
+ }
107
+ end
108
+
109
+ # Returns the current configuration options
110
+ def options
111
+ @@options ||= default_options.with_indifferent_access
112
+ end
113
+
114
+ # Resets the configuration options to the default_options
115
+ def reset_options!
116
+ @@options = default_options.with_indifferent_access
117
+ end
118
+
119
+ # Overwrite the default_options with your own options. You can specify:
120
+ #
121
+ # consumer_secret:: The OAuth consumer secret of your client that you registered on www.tagcrumbs.com
122
+ # consumer_token:: The OAuth consumer token of your client that you registered on www.tagcrumbs.com
123
+ # consumer_options:: Further Options that will be passed to the OAuth Gem
124
+ # request_token:: The OAuth request token that you received that you can exchange against an access token
125
+ # request_secret:: The OAuth request secret that you received that you can exchange against an access token
126
+ # format:: The Tagcrumbs webservice supports XML and JSON. You can specify the format of your requests here
127
+ def configure(new_options={})
128
+ new_options.symbolize_keys!
129
+ new_options.assert_valid_keys(:consumer_secret, :consumer_token, :consumer_options,
130
+ :access_token, :access_secret,
131
+ :request_token, :request_secret,
132
+ :format)
133
+
134
+ self.options.merge!(new_options)
135
+ end
136
+
137
+
138
+ # A request_token that you can let the user authorize, alternatively you can directly use the authorize_url
139
+ def request_token
140
+ return @@request_token if defined?(@@request_token)
141
+ @@request_token = Requestor.new.request_token
142
+
143
+ options[:request_token] = @@request_token.token
144
+ options[:request_secret] = @@request_token.secret
145
+
146
+ @@request_token
147
+ end
148
+
149
+ # An URL that you should redirect the user to or that you should open in a browser on which the user has to approve access
150
+ # for your application
151
+ def authorize_url
152
+ request_token.authorize_url
153
+ end
154
+
155
+ # After you redirected the user to the authorize_url and after the user authorized your application you can exchange
156
+ # the request_token against an access_token that you can use to make all further signed requests.
157
+ # You should use configure to pass in your access_token and access_secret for all further web service calls.
158
+ def exchange_request_token!
159
+ return @@access_token if defined?(@@access_token)
160
+ @@access_token = request_token.get_access_token
161
+
162
+ options[:access_token] = @@access_token.token
163
+ options[:access_secret] = @@access_token.secret
164
+
165
+ @@access_token
166
+ rescue OAuth::Unauthorized
167
+ nil
168
+ end
169
+
170
+ # Returns the current user that approved the application and owns the current access_token
171
+ # This is also used internally to automatically request all the URLs to POST new resources to.
172
+ def current_user
173
+ @@current_user ||= User.load('/you')
174
+ end
175
+
176
+ # Reads the index.(xml|json) file and automatically useses the URLs for further other queries
177
+ def root
178
+ @@root ||= Root.load('/index')
179
+ end
180
+
181
+ # Constants that are used to validate query parameters in find and search queries
182
+ def constants
183
+ @@constants ||= {
184
+ :per_page => [1, 5, 7, 10, 25, 50, 100],
185
+ :from => [:you, :friends, :fans, :recommendations, :everybody, :user],
186
+ :tagcrumb_type => ['Placemark', 'Favorite'],
187
+ :within => [0.015, 0.15, 0.55],
188
+ :sort => [:date, :distance, :name, :favorites, :comments],
189
+ :order => [:asc, :desc],
190
+ :w => [:unknown, :country, :state, :city, :zip, :street, :intersection, :address, :premise, :nearby, :everywhere]
191
+ }.with_indifferent_access
192
+ end
193
+
194
+ # Validate that key has a valid value. Returns an error if it does not. The constants method returns the valid values.
195
+ def validate(key, value)
196
+ if constants[key]
197
+ raise ArgumentError.new("#{key} must be one of the following: #{constants[key].join(', ')}") unless constants[key].include?(value)
198
+ true
199
+ end
200
+ end
201
+
202
+ # Returns a Tagcrumbs Class by name
203
+ def class_by_type(class_name)
204
+ "Tagcrumbs::#{class_name.classify}".constantize
205
+ end
206
+
207
+ # The supported formats of the Gem and their MIME types
208
+ def supported_formats
209
+ {
210
+ :xml => ['application/xml', 'text/xml'],
211
+ :json => ['application/json', 'text/json']
212
+ }
213
+ end
214
+
215
+ # Checks whether there is a valid access_token and access_secret that can be used to make all further calls to the webservice.
216
+ def valid_credentials?
217
+ if options[:access_token] && options[:access_secret]
218
+ requestor = Requestor.new
219
+ requestor.get('/you')
220
+
221
+ true
222
+ else
223
+ false
224
+ end
225
+ rescue Net::HTTPServerException => error
226
+ requestor.response.code == '401' ? false : (raise error) # unauthorized
227
+ end
228
+ end
229
+
230
+ end
231
+
232
+