lc-api 0.8.7 → 0.8.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lc-api/api.rb +5 -13
- data/lib/lc-api/resource.rb +25 -26
- metadata +1 -1
data/lib/lc-api/api.rb
CHANGED
@@ -11,17 +11,9 @@ module LcApi
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
class ResponseError < StandardError
|
15
|
-
end
|
16
|
-
|
17
|
-
class
|
18
|
-
end
|
19
|
-
|
20
|
-
class Forbidden < ResponseError
|
21
|
-
end
|
22
|
-
|
23
|
-
class InternalServerError < ResponseError
|
24
|
-
end
|
25
|
-
|
14
|
+
class ResponseError < StandardError; end
|
15
|
+
class NotFound < ResponseError; end
|
16
|
+
class Forbidden < ResponseError; end
|
17
|
+
class InternalServerError < ResponseError; end
|
26
18
|
end
|
27
|
-
end
|
19
|
+
end
|
data/lib/lc-api/resource.rb
CHANGED
@@ -6,17 +6,30 @@ module LcApi
|
|
6
6
|
class << self
|
7
7
|
|
8
8
|
def find(id, options={})
|
9
|
-
uri = member_name
|
9
|
+
uri = member_name # ie 'message'
|
10
10
|
uri += "/#{id}" if id
|
11
|
+
puts "uri is: #{uri}"
|
11
12
|
parse_response(API.get(uri, options))
|
12
13
|
end
|
13
14
|
|
14
15
|
def all(options={})
|
15
|
-
uri = member_name
|
16
|
+
uri = member_name # get name of resource
|
16
17
|
parse_response(API.get(uri, options), true)
|
17
18
|
end
|
18
19
|
|
19
20
|
def parse_response(response, multiple=false)
|
21
|
+
error_code_check(response)
|
22
|
+
|
23
|
+
# build a single record and return if there is only one resource (via ".find" method)
|
24
|
+
return build_record(response.parsed_response) unless multiple
|
25
|
+
|
26
|
+
# otherwise, build multiple records for multiple resources (via ".all" method)
|
27
|
+
resources = []
|
28
|
+
response.parsed_response.each { |rec| resources.push build_record(rec) }
|
29
|
+
return resources
|
30
|
+
end
|
31
|
+
|
32
|
+
def error_code_check(response)
|
20
33
|
case response.code.to_i
|
21
34
|
when 404
|
22
35
|
raise LcApi::API::NotFound.new(response), "Resource was not found"
|
@@ -25,57 +38,43 @@ module LcApi
|
|
25
38
|
when 500
|
26
39
|
raise LcApi::API::InternalServerError.new(response), "500 Internal Server error"
|
27
40
|
end
|
28
|
-
|
29
|
-
return build_record(response.parsed_response) unless multiple
|
30
|
-
|
31
|
-
resources = []
|
32
|
-
response.parsed_response.each do |rec|
|
33
|
-
resources.push build_record(rec)
|
34
|
-
end
|
35
|
-
return resources
|
36
41
|
end
|
37
42
|
|
38
43
|
def build_record(response)
|
39
44
|
attributes = {}
|
40
|
-
response.each_pair
|
41
|
-
attributes[key] = val if @attributes.include? key
|
42
|
-
end
|
45
|
+
response.each_pair { |k,v| attributes[k] = v if @attributes.include? k }
|
43
46
|
new(attributes)
|
44
47
|
end
|
45
48
|
|
46
49
|
def member_name
|
47
|
-
name.split('::').last.downcase.pluralize
|
50
|
+
name.split('::').last.downcase.pluralize # pluralize from active_support/inflector
|
48
51
|
end
|
49
52
|
|
50
53
|
def define_attribute_methods(attributes)
|
51
54
|
@attributes = attributes
|
52
|
-
|
53
|
-
@attributes.each do |name|
|
54
|
-
define_method(name) { self[name] }
|
55
|
-
end
|
55
|
+
@attributes.each { |name| define_method(name) { self[name] }}
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
attr_accessor :attributes
|
60
60
|
|
61
61
|
def initialize attributes = {}
|
62
|
+
# raise error if user attempts to initialize a Resource by calling new
|
62
63
|
raise Error, "#{self.class} is an abstract class and cannot be instantiated" if instance_of? Resource
|
63
64
|
@attributes = {}
|
64
|
-
self.attributes = attributes
|
65
|
+
self.attributes = attributes # "attributes=" called
|
65
66
|
end
|
66
67
|
|
67
|
-
def [](
|
68
|
-
@attributes[
|
68
|
+
def [](k) # "self[]" called externally, like so: "message.title" -- getter for keys and values (self[])
|
69
|
+
@attributes[k]
|
69
70
|
end
|
70
71
|
|
71
|
-
def []=(
|
72
|
-
@attributes[
|
72
|
+
def []=(k,v) # called internally -- setter for keys and values
|
73
|
+
@attributes[k] = v if self.respond_to?(k)
|
73
74
|
end
|
74
75
|
|
75
76
|
def attributes=(attributes = {})
|
76
|
-
attributes.each_pair
|
77
|
-
self[key] = val
|
78
|
-
end
|
77
|
+
attributes.each_pair { |k, v| self[k] = v } # []= method call
|
79
78
|
end
|
80
79
|
|
81
80
|
end
|