arbor 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef88472480a661ea860c484d64e75f9884a2d171
4
- data.tar.gz: 13885de2ac13295ab06788ece47856b6502cf1af
3
+ metadata.gz: 6b4a455a9b06989723d91f8d5419b8e4d1eaa731
4
+ data.tar.gz: 9058985b086831ec3c748bf57f876318607aa216
5
5
  SHA512:
6
- metadata.gz: a19f952f616fb95da9c16d36bf6a4b67d87bf1051096fedd5bf3c11714bdea34611f2277cab870c369276ecaa2f0a4d9aecd466c1eb8fa8b566ad9635c8adbcd
7
- data.tar.gz: ec4b7ba338c051f2d8020bdc4b40e8fc705dc14a3d088aba5724f58fe3eff1e5063c1fa3cfe06fee7218020cf7ce6ad38471794bf2458f9deccc4f6f1a60e00d
6
+ metadata.gz: 6f6971c9eb5ad2bb2812c33092c97122af8cdfa377e6fb732948843ef580c39c9b5385d109c2d39d416c5417485de9f8983ab6b7200a54bb0da196b0dd8a9ee3
7
+ data.tar.gz: 1e811aeb1400e1a056d180668e4929ad37d46421b09c9bac38aff600e8ad195122d23b603f8b4f906c4599b3d899eb39b1c6a3b1fa0c338032a60a23973e078e
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ruby Interface for Arbor API
2
2
 
3
- A Ruby interface to [Arbor Education API](http://www.arbor-education.com/developers.html).
3
+ An unofficial Ruby interface to [Arbor Education API](http://www.arbor-education.com/developers.html).
4
4
 
5
5
  ## Installation
6
6
 
data/lib/arbor/api.rb CHANGED
@@ -3,30 +3,30 @@ require 'arbor/model/serialiser'
3
3
  module Arbor
4
4
  module API
5
5
  def retrieve(type, id)
6
- resource = parse_resource(type)
7
- data = get("/rest-v2/#{resource}/#{id}")
6
+ resource = parse_resource_name(type)
7
+ data = get("/rest-v2/#{resource.dasherize}/#{id}")
8
8
 
9
9
  unmarshall_data(data, resource)
10
10
  end
11
11
 
12
12
  def query(type, query = nil)
13
- resource = parse_resource(type)
13
+ resource = parse_resource_name(type)
14
14
  query_string = query.build_query_string if query
15
15
 
16
- data = get("/rest-v2/#{resource}?#{query_string}")
16
+ data = get("/rest-v2/#{resource.dasherize}?#{query_string}")
17
17
 
18
18
  unmarshall_data(data, resource)
19
19
  end
20
20
 
21
21
  private
22
22
  def unmarshall_data(data, resource)
23
- singular_resource = resource.to_s.singularize
24
- plural_resource = resource.to_s
23
+ singular_resource = resource.singularize.camelize(:lower)
24
+ plural_resource = resource.camelize(:lower)
25
25
 
26
26
  if (res = data[singular_resource])
27
27
  Model::Serialiser.parse_resource(res).tap { |obj|
28
28
  obj.attach_client(self) if obj.respond_to?(:attach_client)
29
- obj.lock_attributes = true
29
+ obj.lock_attributes = true if obj.respond_to?(:lock_attributes)
30
30
  }
31
31
  elsif data[plural_resource]
32
32
  data[plural_resource].map do |res|
@@ -34,8 +34,12 @@ module Arbor
34
34
  obj.attach_client(self) if obj.respond_to?(:attach_client)
35
35
  }
36
36
  end
37
+ elsif data.empty?
38
+ []
39
+ elsif data["errors"]
40
+ raise Errors::APIError, "#{data["status"]["code"]} #{data["status"]["reason"]}: #{data["status"]["errors"].join(", ")}"
37
41
  else
38
- raise Errors::SerialisationError, "Unexpected root key in API data."
42
+ raise Errors::SerialisationError, "Unexpected root key in API data. Expected: #{plural_resource} or #{singular_resource}. Actual: #{data.keys}"
39
43
  end
40
44
  end
41
45
  end
data/lib/arbor/errors.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Arbor
2
2
  module Errors
3
- SerialisationError = Class.new(StandardError)
3
+ SerialisationError = Class.new(StandardError)
4
4
  UnknownAttributeError = Class.new(StandardError)
5
+ APIError = Class.new(StandardError)
5
6
  end
6
7
  end
@@ -24,7 +24,7 @@ module Arbor
24
24
 
25
25
  data = api_client.get(href)
26
26
 
27
- parsed_attributes = Serialiser.parse_attributes(data[entity_type.downcase])
27
+ parsed_attributes = Serialiser.parse_attributes(data[entity_type.camelize(:lower)])
28
28
  load_attributes(parsed_attributes)
29
29
  attach_client(self.api_client)
30
30
 
@@ -64,6 +64,14 @@ module Arbor
64
64
  def unlock_attributes
65
65
  @attribute_lock = false
66
66
  end
67
+
68
+ def attributes
69
+ Hash[attribute_names.map { |a| [a, send(a)] }]
70
+ end
71
+
72
+ def inspect
73
+ "#<#{self.class.name} #{attribute_names.map { |a| "#{a}: #{send(a).inspect}" }.join(', ')}>"
74
+ end
67
75
  end
68
76
  end
69
77
  end
data/lib/arbor/query.rb CHANGED
@@ -1,15 +1,11 @@
1
1
  require 'uri'
2
- require 'arbor/utils'
3
2
  require 'arbor/filter'
4
3
 
5
4
  module Arbor
6
5
  class Query
7
- include Arbor::Utils
8
-
9
6
  attr_accessor :filters, :resource
10
7
 
11
- def initialize(type, filters = [])
12
- @resource = parse_resource(type)
8
+ def initialize(filters = [])
13
9
  @filters = filters
14
10
  end
15
11
 
data/lib/arbor/utils.rb CHANGED
@@ -2,21 +2,22 @@ require 'active_support/core_ext/string'
2
2
 
3
3
  module Arbor
4
4
  module Utils
5
- def parse_resource(type)
6
- validate(get_resource(type), Arbor::RESOURCES)
5
+ def parse_resource_name(type)
6
+ validate(get_resource_name(type), Arbor::RESOURCES)
7
7
  end
8
8
 
9
- def get_resource(type)
9
+ def get_resource_name(type)
10
10
  case type
11
11
  when Class
12
- type.name.pluralize.underscore.to_sym
12
+ # do reverse serialiser lookup instead
13
+ type.name.pluralize.underscore
13
14
  else
14
- type.to_s.pluralize.to_sym
15
+ type.to_s.pluralize
15
16
  end
16
17
  end
17
18
 
18
19
  def validate(choice, options)
19
- unless options.include?(choice)
20
+ unless options.include?(choice.to_sym)
20
21
  raise ArgumentError, "#{choice} is not a valid option: #{options}"
21
22
  end
22
23
  choice
data/lib/arbor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arbor
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Campbell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-19 00:00:00.000000000 Z
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport