storage_room 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === Version 0.1.1
2
+
3
+ * Added rdoc
4
+
1
5
  === Version 0.1.0
2
6
 
3
7
  * Initial Version
data/README.rdoc CHANGED
@@ -28,6 +28,8 @@ This is a walkthrough with all steps you need to setup a devise resource, includ
28
28
  puts "Could not save Resource: #{resource.errors.join(', )}"
29
29
  end
30
30
 
31
+ You can find the documentation at http://rdoc.info/github/thriventures/storage_room_gem.
32
+
31
33
  == More Examples
32
34
 
33
35
  See the examples folder.
data/TODO CHANGED
@@ -1,6 +1,4 @@
1
1
  - rdoc
2
- - gemcutter
3
-
4
2
 
5
3
 
6
4
  == Version X
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/storage_room.rb CHANGED
@@ -33,7 +33,8 @@ module StorageRoom
33
33
  attr_reader :ssl
34
34
  attr_reader :proxy_server
35
35
  attr_reader :proxy_port
36
-
36
+
37
+ # Authenticate once before making any requests with your account id and the application's api key
37
38
  def authenticate(account_id, api_key)
38
39
  Base.basic_auth(api_key, 'X')
39
40
  @api_key = api_key
@@ -41,37 +42,36 @@ module StorageRoom
41
42
  update_uri
42
43
  end
43
44
 
45
+ # Change the user agent that is sent with all requests
44
46
  def user_agent=(agent)
45
47
  Base.headers.merge!('User-Agent' => agent)
46
48
  @user_agent = agent
47
49
  end
48
-
49
- def server=(server)
50
+
51
+ # Change the server to connect to. This should only be relevant for developers of the library.
52
+ def server=(server) #:nodoc:
50
53
  @server = server
51
54
  update_uri
52
55
  end
53
56
 
54
- def server
57
+ def server #:nodoc:
55
58
  @server || 'api.storageroomapp.com'
56
59
  end
57
-
60
+
61
+ # Requests are made with SSL
58
62
  def ssl=(ssl)
59
63
  @ssl = ssl
60
64
  update_uri
61
65
  end
62
66
 
67
+ # Set an HTTP proxy server
63
68
  def http_proxy(server, port)
64
69
  @proxy_server = server
65
70
  @proxy_port = port
66
71
  Base.http_proxy(server, port)
67
72
  end
68
73
 
69
- def update_uri
70
- protocol = self.ssl == true ? 'https' : 'http'
71
-
72
- Base.base_uri "#{protocol}://#{self.server}/accounts/#{self.account_id}"
73
- end
74
-
74
+ # Return a Ruby class for a StorageRoom type
75
75
  def class_for_name(name)
76
76
  if StorageRoom.const_defined?(name)
77
77
  "StorageRoom::#{name}".constantize
@@ -85,5 +85,13 @@ module StorageRoom
85
85
  end
86
86
  end
87
87
 
88
+ private
89
+
90
+ def update_uri
91
+ protocol = self.ssl == true ? 'https' : 'http'
92
+
93
+ Base.base_uri "#{protocol}://#{self.server}/accounts/#{self.account_id}"
94
+ end
95
+
88
96
  end
89
97
  end
@@ -1,17 +1,15 @@
1
1
  module StorageRoom
2
+ # A container object that contains many models (collections or resources)
2
3
  class Array < Base
3
4
  attr_accessor :items
4
5
 
5
- class << self
6
-
7
- end
8
-
9
-
6
+
10
7
  def initialize(attributes = {})
11
8
  self.items = []
12
9
  super
13
10
  end
14
11
 
12
+ # Set the array with the attributes from the API
15
13
  def set_from_api(attributes)
16
14
  super(attributes)
17
15
 
@@ -19,11 +17,13 @@ module StorageRoom
19
17
  attributes.delete('items')
20
18
  end
21
19
 
20
+ # Reset the Array to its default state with all attributes unset
22
21
  def reset!
23
22
  super
24
23
  @items = []
25
24
  end
26
25
 
26
+ # Replaces the objects content with the next page of the array if a next page exists
27
27
  def load_next_page!
28
28
  if self[:@next_page].present?
29
29
  reload(self[:@next_page])
@@ -1,33 +1,42 @@
1
1
  module StorageRoom
2
+
3
+ # Module that contains attributes methods shared between StorageRoom::Base and StorageRoom::Embedded
2
4
  module Attributes
5
+ # Optionally pass attributes to set up the object
3
6
  def initialize(attributes={})
4
7
  self.attributes = attributes
5
8
  end
6
9
 
10
+ # Set the attributes of an object with a hash from the API
7
11
  def set_from_api(attributes)
8
12
  reset!
9
13
 
10
14
  self.attributes = attributes
11
15
  end
12
16
 
17
+ # Shortcut to get an attribute.
13
18
  def [](name)
14
19
  self.attributes[name]
15
20
  end
16
21
 
22
+ # Shortcut to set an attribute
17
23
  def []=(name, value)
18
24
  self.attributes[name] = value
19
25
  end
20
26
 
27
+ # Return all of the objects attributes
21
28
  def attributes
22
29
  @attributes ||= Hash.new.with_indifferent_access
23
30
  end
24
31
 
32
+ # Set the objects attributes with a hash. Only attributes passed in the hash are changed, existing ones are not overridden.
25
33
  def attributes=(hash = {})
26
34
  hash.each do |k, v|
27
35
  self.attributes[k] = v
28
36
  end
29
37
  end
30
38
 
39
+ # Reset an object to its initial state with all attributes unset
31
40
  def reset!
32
41
  @attributes = Hash.new.with_indifferent_access
33
42
  end
@@ -1,4 +1,5 @@
1
1
  module StorageRoom
2
+ # The superclass of all the classes that load data from the API
2
3
  class Base
3
4
  include Attributes
4
5
  include HTTParty
@@ -8,6 +9,7 @@ module StorageRoom
8
9
 
9
10
 
10
11
  class << self
12
+ # Load an object with the specified URL from the API
11
13
  def load(url, parameters = {})
12
14
  httparty = get(url, parameters)
13
15
 
@@ -15,7 +17,8 @@ module StorageRoom
15
17
  create_from_api(httparty.parsed_response.first[1])
16
18
  end
17
19
 
18
- def handle_critical_response_errors(httparty)
20
+ # Handle known server errors
21
+ def handle_critical_response_errors(httparty) # :nodoc:
19
22
  case httparty.response.code
20
23
  when '200', '201', '422' then true
21
24
  else
@@ -23,7 +26,8 @@ module StorageRoom
23
26
  end
24
27
  end
25
28
 
26
- def create_from_api(hash)
29
+ # Creates a new object with a hash from the API with a @type attribute
30
+ def create_from_api(hash) # :nodoc:
27
31
  type = hash['@type']
28
32
 
29
33
  object = case type
@@ -36,18 +40,21 @@ module StorageRoom
36
40
  object.set_from_api(hash)
37
41
  object
38
42
  end
39
-
40
- def meta_data?(key)
43
+
44
+ # Find out if a key is user-defined or meta-data (begins with @)
45
+ def meta_data?(key)
41
46
  key[0...1] == '@'
42
47
  end
43
48
  end
44
49
 
50
+ # Reload an object from the API. Optionally pass an URL.
45
51
  def reload(url = nil, parameters = {})
46
52
  httparty = self.class.get(url || self[:@url], parameters)
47
53
  set_from_api(httparty.parsed_response.first[1])
48
54
  true
49
55
  end
50
-
56
+
57
+ # Returns the remote URL of the object
51
58
  def url
52
59
  self[:@url]
53
60
  end
@@ -1,4 +1,5 @@
1
1
  module StorageRoom
2
+ # An embedded object that is not only a simple type (like an integer, string)
2
3
  class Embedded
3
4
  include Attributes
4
5
 
@@ -1,4 +1,5 @@
1
1
  module StorageRoom
2
+ # A file that is embedded in a resource
2
3
  class File < Embedded
3
4
 
4
5
  end
@@ -1,4 +1,5 @@
1
1
  module StorageRoom
2
+ # A location with coordinates that is embedded in a resource
2
3
  class Location < Embedded
3
4
 
4
5
  end
@@ -1,4 +1,5 @@
1
1
  module StorageRoom
2
+ # A field that is part of a collection
2
3
  class Field
3
4
 
4
5
 
@@ -1,33 +1,39 @@
1
1
  module StorageRoom
2
+ # Abstract superclass for classes that can persist to the remote servers
2
3
  class Model < Base
3
4
  class << self
5
+ # Create a new model with the passed attributes
4
6
  def create(attributes={})
5
7
  resource = new(attributes)
6
8
  resource.create
7
9
  resource
8
10
  end
9
11
 
12
+ # Get all models (could be paginated)
10
13
  def all
11
14
  load(index_path)
12
15
  end
13
16
 
17
+ # Find a model with the specified id
14
18
  def find(id)
15
19
  load(show_path(id))
16
20
  end
17
-
18
- def index_path
21
+
22
+ #
23
+ def index_path # :nodoc:
19
24
  raise StorageRoom::AbstractMethod.new
20
25
  end
21
26
 
22
- def show_path(id)
27
+ def show_path(id) # :nodoc:
23
28
  raise StorageRoom::AbstractMethod.new
24
29
  end
25
30
 
26
- def json_name
31
+ def json_name # :nodoc:
27
32
  raise StorageRoom::AbstractMethod.new
28
33
  end
29
34
  end
30
35
 
36
+ # Create a new model and set its attributes
31
37
  def initialize(attributes={})
32
38
  @new_record = true
33
39
  @errors = []
@@ -35,6 +41,7 @@ module StorageRoom
35
41
  super
36
42
  end
37
43
 
44
+ # Set the attributes of the model from the API
38
45
  def set_from_api(attributes)
39
46
  super
40
47
  @new_record = false
@@ -42,6 +49,7 @@ module StorageRoom
42
49
  self.attributes
43
50
  end
44
51
 
52
+ # Reset the model to its default state
45
53
  def reset!
46
54
  super
47
55
  @new_record = true
@@ -49,26 +57,31 @@ module StorageRoom
49
57
  true
50
58
  end
51
59
 
60
+ # Has this model been saved to the API already?
52
61
  def new_record?
53
62
  @new_record ? true : false
54
63
  end
55
64
 
65
+ # Create a new model or update an existing model on the server
56
66
  def save
57
67
  new_record? ? create : update
58
68
  end
59
69
 
70
+ # Create a new model on the server
60
71
  def create
61
72
  return false unless new_record?
62
73
  httparty = self.class.post(self.class.index_path, :body => to_json)
63
74
  handle_save_response(httparty)
64
75
  end
65
76
 
77
+ # Update an existing model on the server
66
78
  def update
67
79
  return false if new_record?
68
80
  httparty = self.class.put(url, :body => to_json)
69
81
  handle_save_response(httparty)
70
82
  end
71
83
 
84
+ # Delete an existing model on the server
72
85
  def destroy
73
86
  return false if new_record?
74
87
 
@@ -78,14 +91,16 @@ module StorageRoom
78
91
  true
79
92
  end
80
93
 
94
+ # Is the model valid or were there validation errors on the server?
81
95
  def valid?
82
96
  self.errors.empty?
83
97
  end
84
98
 
85
- def as_json(args = {})
99
+ def as_json(args = {}) # :nodoc:
86
100
  {self.class.json_name => self.attributes.reject{|k, v| self.class.meta_data?(k)}}
87
101
  end
88
102
 
103
+ # The validation errors that were returned by the server
89
104
  def errors
90
105
  @errors ||= []
91
106
  end
@@ -95,7 +110,7 @@ module StorageRoom
95
110
  # ===================
96
111
 
97
112
  private
98
- def handle_save_response(httparty)
113
+ def handle_save_response(httparty) # :nodoc:
99
114
  self.class.handle_critical_response_errors(httparty)
100
115
 
101
116
  if httparty.response.code == '200' || httparty.response.code == '201'
@@ -1,27 +1,30 @@
1
1
  module StorageRoom
2
+ # A collection is used to define the structure of a data set.
2
3
  class Collection < Model
3
4
  class << self
4
- def index_path
5
+ def index_path # :nodoc:
5
6
  '/collections'
6
7
  end
7
8
 
8
- def show_path(collection_id)
9
+ def show_path(collection_id) # :nodoc:
9
10
  "#{index_path}/#{collection_id}"
10
11
  end
11
12
 
12
- def resources_path(collection_id)
13
+ def resources_path(collection_id) # :nodoc:
13
14
  "#{show_path(collection_id)}/resources"
14
15
  end
15
16
 
16
- def json_name
17
+ def json_name # :nodoc:
17
18
  'collection'
18
19
  end
19
20
  end
20
21
 
22
+ # Load all the resources of a collection
21
23
  def resources
22
24
  Array.load(self[:@resources_url])
23
25
  end
24
26
 
27
+ # The class of the collection's objects
25
28
  def resource_class
26
29
  StorageRoom.class_for_name(self[:identifier].classify)
27
30
  end
@@ -1,35 +1,37 @@
1
1
  module StorageRoom
2
2
  class Resource < Model
3
3
  class << self
4
- def index_path
4
+ def index_path # :nodoc:
5
5
  "/collections/#{collection_id}/resources"
6
6
  end
7
7
 
8
- def show_path(resource_id)
8
+ def show_path(resource_id) # :nodoc:
9
9
  "#{index_path}/#{resource_id}"
10
10
  end
11
11
 
12
- def collection_path
12
+ def collection_path # :nodoc:
13
13
  "/collections/#{collection_id}"
14
14
  end
15
15
 
16
- def collection_id
16
+ def collection_id # :nodoc:
17
17
  self.name.gsub('StorageRoom::', '').tableize
18
18
  end
19
19
 
20
- def json_name
20
+ def json_name # :nodoc:
21
21
  'resource'
22
22
  end
23
23
 
24
- def search_path(parameters = {})
24
+ def search_path(parameters = {}) # :nodoc:
25
25
  parameters.present? ? "#{index_path}?#{parameters.to_query}" : index_path
26
26
  end
27
27
 
28
+ # Search for objects with specific parameters
28
29
  def search(parameters = {})
29
30
  Array.load(search_path(parameters))
30
31
  end
31
32
  end
32
33
 
34
+ # Sets a resource with a hash from the API.
33
35
  def set_from_api(attributes)
34
36
  super(attributes)
35
37
 
@@ -44,6 +46,7 @@ module StorageRoom
44
46
  self.attributes
45
47
  end
46
48
 
49
+ # The collection of a resource
47
50
  def collection
48
51
  Collection.load(self[:@collection_url] || self.class.collection_path)
49
52
  end
@@ -0,0 +1,113 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{storage_room}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sascha Konietzke"]
12
+ s.date = %q{2010-11-08}
13
+ s.description = %q{StorageRoom is a CMS system for Mobile Applications (iPhone, Android, BlackBerry, ...). This library gives you an ActiveModel-like interface to your data.}
14
+ s.email = %q{sascha@thriventures.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc",
18
+ "TODO"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "History.txt",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "TODO",
27
+ "VERSION",
28
+ "examples/authentication.rb",
29
+ "examples/create_resource.rb",
30
+ "examples/destroy_resource.rb",
31
+ "examples/get_collections.rb",
32
+ "examples/guidebooks.csv",
33
+ "examples/import_csv.rb",
34
+ "examples/search_resources.rb",
35
+ "examples/update_resource.rb",
36
+ "lib/storage_room.rb",
37
+ "lib/storage_room/array.rb",
38
+ "lib/storage_room/attributes.rb",
39
+ "lib/storage_room/base.rb",
40
+ "lib/storage_room/embedded.rb",
41
+ "lib/storage_room/embeddeds/file.rb",
42
+ "lib/storage_room/embeddeds/location.rb",
43
+ "lib/storage_room/field.rb",
44
+ "lib/storage_room/model.rb",
45
+ "lib/storage_room/models/collection.rb",
46
+ "lib/storage_room/models/resource.rb",
47
+ "spec/fixtures/collection.json",
48
+ "spec/fixtures/collections.json",
49
+ "spec/fixtures/validation_error.json",
50
+ "spec/spec_helper.rb",
51
+ "spec/storage_room/array_spec.rb",
52
+ "spec/storage_room/attributes_spec.rb",
53
+ "spec/storage_room/base_spec.rb",
54
+ "spec/storage_room/embedded_spec.rb",
55
+ "spec/storage_room/embeddeds/file_spec.rb",
56
+ "spec/storage_room/embeddeds/location_spec.rb",
57
+ "spec/storage_room/field_spec.rb",
58
+ "spec/storage_room/model_spec.rb",
59
+ "spec/storage_room/models/collection_spec.rb",
60
+ "spec/storage_room/models/resource_spec.rb",
61
+ "spec/storage_room_spec.rb",
62
+ "storage_room.gemspec"
63
+ ]
64
+ s.homepage = %q{http://github.com/thriventures/storage_room_gem}
65
+ s.rdoc_options = ["--charset=UTF-8"]
66
+ s.require_paths = ["lib"]
67
+ s.rubygems_version = %q{1.3.7}
68
+ s.summary = %q{StorageRoom API Wrapper (ActiveModel style)}
69
+ s.test_files = [
70
+ "spec/spec_helper.rb",
71
+ "spec/storage_room/array_spec.rb",
72
+ "spec/storage_room/attributes_spec.rb",
73
+ "spec/storage_room/base_spec.rb",
74
+ "spec/storage_room/embedded_spec.rb",
75
+ "spec/storage_room/embeddeds/file_spec.rb",
76
+ "spec/storage_room/embeddeds/location_spec.rb",
77
+ "spec/storage_room/field_spec.rb",
78
+ "spec/storage_room/model_spec.rb",
79
+ "spec/storage_room/models/collection_spec.rb",
80
+ "spec/storage_room/models/resource_spec.rb",
81
+ "spec/storage_room_spec.rb",
82
+ "examples/authentication.rb",
83
+ "examples/create_resource.rb",
84
+ "examples/destroy_resource.rb",
85
+ "examples/get_collections.rb",
86
+ "examples/import_csv.rb",
87
+ "examples/search_resources.rb",
88
+ "examples/update_resource.rb"
89
+ ]
90
+
91
+ if s.respond_to? :specification_version then
92
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
93
+ s.specification_version = 3
94
+
95
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
96
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
97
+ s.add_development_dependency(%q<webmock>, [">= 0"])
98
+ s.add_runtime_dependency(%q<httparty>, [">= 0.6.1"])
99
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
100
+ else
101
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
102
+ s.add_dependency(%q<webmock>, [">= 0"])
103
+ s.add_dependency(%q<httparty>, [">= 0.6.1"])
104
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
105
+ end
106
+ else
107
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
108
+ s.add_dependency(%q<webmock>, [">= 0"])
109
+ s.add_dependency(%q<httparty>, [">= 0.6.1"])
110
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
111
+ end
112
+ end
113
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storage_room
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sascha Konietzke
@@ -132,6 +132,7 @@ files:
132
132
  - spec/storage_room/models/collection_spec.rb
133
133
  - spec/storage_room/models/resource_spec.rb
134
134
  - spec/storage_room_spec.rb
135
+ - storage_room.gemspec
135
136
  has_rdoc: true
136
137
  homepage: http://github.com/thriventures/storage_room_gem
137
138
  licenses: []