well_rested-core 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,14 +18,10 @@ module WellRested
18
18
  attr_accessor :default_path_parameters
19
19
  attr_accessor :client
20
20
  attr_reader :last_response
21
- attr_accessor :unique_id
22
- attr_accessor :version
23
21
 
24
- def initialize(path_params = {}, session_params = {}, version = "")
22
+ def initialize(path_params = {})
25
23
  self.default_path_parameters = path_params.with_indifferent_access
26
24
  self.client = RestClient
27
- self.unique_id = session_params.try(:uid) || 'unauthorized'
28
- self.version = version
29
25
  end
30
26
 
31
27
  ##
@@ -101,9 +97,8 @@ module WellRested
101
97
  url = url_for(klass, path_params_or_url, query_params)
102
98
 
103
99
  logger.info "GET #{url}"
104
- # response = client.get(url, request_headers) do |response, request, result, &block|
105
100
  response = client.get(url, request_headers) do |response, request, result, &block|
106
- @last_response = response
101
+ @last_response = response
107
102
  response.return!(request, result, &block)
108
103
  end
109
104
 
@@ -244,32 +239,15 @@ module WellRested
244
239
 
245
240
  # Convenience method. Also allows request_headers to be can be set on a per-instance basis.
246
241
  def request_headers
247
- self.class.request_headers(self.unique_id, self.version)
242
+ self.class.request_headers
248
243
  end
249
244
 
250
245
  # Return the default headers sent with all HTTP requests.
251
- def self.request_headers(unique_id, version)
246
+ def self.request_headers
252
247
  # Accept necessary for fetching results by result ID, but not in most places.
253
- {
254
- :content_type => 'application/json',
255
- :accept => 'application/json',
256
- "Authentication" => unique_id,
257
- :version => version
258
- }
248
+ { :content_type => 'application/json', :accept => 'application/json' }
259
249
  end
260
250
 
261
- # # 20120627 CR: Add authentication to header
262
- # def request_headers_with_authentication(authentication_id)
263
- # self.class.request_headers_with_authentication(authentication_id)
264
- # end
265
-
266
- # # Return the default headers sent with all HTTP requests.
267
- # def self.request_headers_with_authentication(authentication_id)
268
- # # Accept necessary for fetching results by result ID, but not in most places.
269
- # { :content_type => 'application/json', :accept => 'application/json', :Authentication => authentication_id}
270
- # end
271
-
272
-
273
251
  # TODO: Move this into a utility module? It can then be called from Base#fill_path or directly if needed.
274
252
  def self.fill_path(path_template, params)
275
253
  raise "Cannot fill nil path" if path_template.nil?
@@ -320,7 +298,7 @@ module WellRested
320
298
  end
321
299
 
322
300
  # If ID is set in path parameters, do a PUT. Otherwise, do a POST.
323
- method = resource.path_parameters[:key].blank? ? :post : :put
301
+ method = resource.path_parameters[:id].blank? ? :post : :put
324
302
 
325
303
  response = run_update(method, url, payload)
326
304
 
@@ -328,7 +306,7 @@ module WellRested
328
306
  decoded_hash = resource.class.attribute_formatter.decode(hash)
329
307
  logger.info "* Errors: #{decoded_hash['errors'].inspect}" if decoded_hash.include?('errors')
330
308
 
331
- if response.code.between?(200,299)
309
+ if response.code == 200
332
310
  # If save succeeds, replace resource's attributes with the ones returned.
333
311
  return decoded_hash.map { |hash| resource.class.new_from_api(hash) } if decoded_hash.kind_of?(Array)
334
312
  resource.load_from_api(decoded_hash)
@@ -29,7 +29,7 @@ module WellRested
29
29
  # class-level defaults
30
30
  self.protocol = 'http'
31
31
  # a body formatter must respond to the methods encode(hash_or_array) => string and decode(string) => hash_or_array
32
- self.body_formatter = JSONFormatter.new
32
+ self.body_formatter = JSONFormatter.new
33
33
  self.extension = ''
34
34
  # an attribute formatter must respond to encode(attribute_name_string) => string and decode(attribute_name_string) => string
35
35
  self.attribute_formatter = CamelCaseFormatter.new
@@ -41,11 +41,11 @@ module WellRested
41
41
  # Define the schema for this resource.
42
42
  #
43
43
  # Either takes an array, or a list of arguments which we treat as an array.
44
- # Each element of the array should be either a symbol or a hash.
44
+ # Each element of the array should be either a symbol or a hash.
45
45
  # If it's a symbol, we create an attribute using the symol as the name and with a null default value.
46
46
  # If it's a hash, we use the keys as attribute names.
47
47
  # - Any values that are hashes, we use to specify further options (currently, the only option is :default).
48
- # - Any value that is not a hash is treated as a default.
48
+ # - Any value that is not a hash is treated as a default.
49
49
  # e.g.
50
50
  # define_schema :x, :y, :z # x, y, and z all default to nil
51
51
  # define_schema :id, :name => 'John' # id defaults to nil, name defaults to 'John'
@@ -84,7 +84,7 @@ module WellRested
84
84
  def initialize(attrs = {})
85
85
  raise "Attrs must be hash" unless attrs.is_a? Hash
86
86
 
87
- self.load(attrs, false)
87
+ self.load(attrs, false)
88
88
  end
89
89
 
90
90
  # Define an actual method for ID. This is important in Ruby 1.8 where the object_id method is also aliased to id.
@@ -133,9 +133,9 @@ module WellRested
133
133
 
134
134
  #puts "*** Warning: loading a resource without a schema (#{self.class})!" if schema.nil?
135
135
  #raise "Tried to load attributes for a resource with no schema (#{self.class})!" if schema.nil?
136
-
136
+
137
137
  # We mark a record as new if it doesn't come from the API and it doesn't have an ID.
138
- self.new_record = !from_api
138
+ self.new_record = !from_api
139
139
  self.new_record = false if attrs_to_load.include?(:id) or attrs_to_load.include?('id')
140
140
 
141
141
  new_attrs = {}.with_indifferent_access
@@ -192,9 +192,9 @@ module WellRested
192
192
  klass = find_resource_class(class_name)
193
193
  if klass
194
194
  #puts "**** class exists, instantiation"
195
- hash[k] = v.map do |o|
196
- if o.kind_of?(Hash)
197
- from_api ? klass.new_from_api(o) : klass.new(o)
195
+ hash[k] = v.map do |o|
196
+ if o.kind_of?(Hash)
197
+ from_api ? klass.new_from_api(o) : klass.new(o)
198
198
  else
199
199
  o
200
200
  end
@@ -222,7 +222,7 @@ module WellRested
222
222
  hash
223
223
  end
224
224
 
225
- # API should use these to generate the path.
225
+ # API should use these to generate the path.
226
226
  # Override this to control how path variables get inserted.
227
227
  def path_parameters
228
228
  objects_to_attributes(@attributes.reject { |k,v| v.nil? }.with_indifferent_access)
@@ -237,11 +237,11 @@ module WellRested
237
237
  def to_param
238
238
  self.id.nil? ? nil : self.id.to_s
239
239
  end
240
-
240
+
241
241
  # Return a key for rails to use for... not sure exaclty what.
242
242
  # Should be an array, or nil.
243
243
  def to_key
244
- self.id.nil? ? nil : [self.id]
244
+ self.id.nil? ? nil : [self.id]
245
245
  end
246
246
 
247
247
  # The following 3 methods were copied from active_record/persistence.rb
@@ -0,0 +1,87 @@
1
+ require 'well_rested/api'
2
+
3
+ module WellRested
4
+ # All REST requests are made through an API object.
5
+ # API objects store cross-resource settings such as user and password (for HTTP basic auth).
6
+ class CAPI < WellRested::API
7
+ include WellRested # for logger
8
+ include WellRested::Utils
9
+
10
+ attr_accessor :user
11
+ attr_accessor :password
12
+ attr_accessor :default_path_parameters
13
+ attr_accessor :client
14
+ attr_reader :last_response
15
+ attr_accessor :unique_id
16
+ attr_accessor :version
17
+
18
+ def initialize(path_params = {}, session_params = {}, version = "")
19
+ self.default_path_parameters = path_params.with_indifferent_access
20
+ self.client = RestClient
21
+ self.unique_id = session_params.try(:uid) || 'unauthorized'
22
+ self.version = version
23
+ end
24
+
25
+ # Convenience method. Also allows request_headers to be can be set on a per-instance basis.
26
+ def request_headers
27
+ self.class.request_headers(self.unique_id, self.version)
28
+ end
29
+
30
+ # Return the default headers sent with all HTTP requests.
31
+ def self.request_headers(unique_id, version)
32
+ # Accept necessary for fetching results by result ID, but not in most places.
33
+ {
34
+ :content_type => 'application/json',
35
+ :accept => 'application/json',
36
+ "Authentication" => unique_id,
37
+ :version => version
38
+ }
39
+ end
40
+
41
+
42
+ protected # internal methods follow
43
+
44
+ # Create or update a resource.
45
+ # If an ID is set, PUT will be used, else POST.
46
+ # If a 200 is returned, the returned attributes will be loaded into the resource, and the resource returned.
47
+ # Otherwise, the resource will not be modified, and a hash generated from the JSON response will be returned.
48
+ def create_or_update_resource(resource, url = nil)
49
+ return false unless resource.valid?
50
+
51
+ #logger.info "Creating a #{resource.class}"
52
+
53
+ path_params = default_path_parameters.merge(resource.path_parameters)
54
+ payload_hash = resource.class.attribute_formatter.encode(resource.attributes_for_api)
55
+ payload = resource.class.body_formatter.encode(payload_hash)
56
+
57
+ #logger.debug " payload: #{payload.inspect}"
58
+
59
+ if url.nil?
60
+ url = url_for(resource.class, path_params) # allow default URL to be overriden by url argument
61
+ else
62
+ url = url_for(resource.class, url)
63
+ end
64
+
65
+ # Ask the resource if it is new, then POST, otherwise PUT
66
+ method = resource.new? ? :post : :put
67
+ # If ID is set in path parameters, do a PUT. Otherwise, do a POST.
68
+ # method = resource.path_parameters[:key].blank? ? :post : :put
69
+
70
+ response = run_update(method, url, payload)
71
+
72
+ hash = resource.class.body_formatter.decode(response.body)
73
+ decoded_hash = resource.class.attribute_formatter.decode(hash)
74
+ logger.info "* Errors: #{decoded_hash['errors'].inspect}" if decoded_hash.include?('errors')
75
+
76
+ if response.code.between?(200,299)
77
+ # If save succeeds, replace resource's attributes with the ones returned.
78
+ return decoded_hash.map { |hash| resource.class.new_from_api(hash) } if decoded_hash.kind_of?(Array)
79
+ resource.load_from_api(decoded_hash)
80
+ return resource
81
+ elsif decoded_hash.include?('errors')
82
+ resource.handle_errors(decoded_hash['errors'])
83
+ return false
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,5 +1,5 @@
1
1
  module WellRested
2
2
  module Core
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -1,25 +1,9 @@
1
1
  require "well_rested-core/version"
2
2
 
3
- # require "well_rested"
4
-
5
- # require 'well_rested/core_formatter'
6
-
7
-
8
- # # require external dependencies
9
- # require 'active_support/core_ext/hash/indifferent_access'
10
- # require 'active_support/core_ext/hash/reverse_merge'
11
-
12
- # # require internal general-use libs
13
- # require 'key_transformer'
14
- # require 'generic_utils'
15
-
16
- # # require internal libs
17
- require 'well_rested/api'
3
+ # # require adapted libs
4
+ require 'well_rested/capi'
18
5
  require 'well_rested/base'
19
- require 'well_rested/utils'
20
- # require 'well_rested/json_formatter'
21
6
  require 'well_rested/core_formatter'
22
- # require 'well_rested/camel_case_formatter'
23
7
 
24
8
  # Make sure 'bases' singularizes to 'base' instead of 'basis'.
25
9
  # Otherwise, we get an error that no class Basis is found in Base.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: well_rested-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,6 +44,7 @@ files:
44
44
  - lib/well_rested-core/version.rb
45
45
  - lib/well_rested/api.rb
46
46
  - lib/well_rested/base.rb
47
+ - lib/well_rested/capi.rb
47
48
  - lib/well_rested/core_formatter.rb
48
49
  - well_rested-core.gemspec
49
50
  homepage: ''