xapix_client 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e2ef3a66d02954b0aeb78b65145aa903f924052
4
- data.tar.gz: 814d2afc1230a9439688f026d84784cad27639b5
3
+ metadata.gz: c82ce14c9367dad160e8e37eff4c961bffec62b5
4
+ data.tar.gz: ceb51e8ee044bcd907eb5eb01bd0b80e658ea551
5
5
  SHA512:
6
- metadata.gz: 050af28fb9b28cf7aab546a93a21655af58b92f3e094a76122bc78da8deb5b05a2b2022c5c598d4e02fec9422466b20578c55fb20c6657e46deb571d736e293a
7
- data.tar.gz: 92086d633fb5e19f2e538e5e019400df140ac7745041d22bc1e2ee106e58ab20ec3fd7b7bdf4274d7f9a514a429c8e8a060538ac105b1a99b9df487119268d08
6
+ metadata.gz: 326fd2ffb6c06172c00f8687b5f30eee7792b0040c093fd770c28c31eff3b917dde63ec3cb7da09f853348e35c983047fdc804d11803a9c8bf1a88a3956d6f48
7
+ data.tar.gz: 2fd066510d9445168500914f4d78e7d3fed6b20c0c70a4136d7f11343096751e0f2c8cd863b34498449e4d6104f22ed6494322f0544afda1f2988333f1720ddd
data/lib/xapix_client.rb CHANGED
@@ -1,7 +1,31 @@
1
1
  require "xapix_client/version"
2
2
  require "xapix_client/config"
3
3
  require "xapix_client/connection"
4
+ require "xapix_client/requestor"
4
5
  require "xapix_client/resource"
5
6
 
7
+ class InputEndpoint < XapixClient::Resource; end
8
+ class Schema < XapixClient::Resource; end
9
+ class SchemaRelationship < XapixClient::Resource; end
10
+
6
11
  module XapixClient
12
+ def self.autoload_models
13
+ Schema.includes(:schema_relationships).map do |schema|
14
+ model_name = schema[:id].classify
15
+ resource_class = Class.new(Resource) do
16
+ schema.attributes.except(:id, :type).each do |name, data_type|
17
+ property(name.to_sym, type: data_type.to_sym)
18
+ end
19
+ relations_by_cardinality = schema.schema_relationships.group_by(&:cardinality)
20
+ (relations_by_cardinality['to_one'] || {}).each do |schema_relationship|
21
+ has_one(schema_relationship.name.to_sym, class_name: schema_relationship.referenced_endpoint_name.classify)
22
+ end
23
+ (relations_by_cardinality['to_many'] || {}).each do |schema_relationship|
24
+ has_many(schema_relationship.name.to_sym, class_name: schema_relationship.referenced_endpoint_name.classify)
25
+ end
26
+ end
27
+ Object.const_set(model_name, resource_class)
28
+ resource_class
29
+ end
30
+ end
7
31
  end
@@ -2,16 +2,17 @@ module XapixClient
2
2
  class BadConfigurationError < StandardError; end
3
3
  class NoConfigurationError < StandardError; end
4
4
 
5
+ class Configuration
6
+ attr_accessor :project_name, :auth_token, :autoload_models
7
+ end
8
+
5
9
  class << self
6
- attr_accessor :configuration
10
+ attr_accessor :configuration, :autoloaded_models
7
11
  end
8
12
 
9
13
  def self.configure
10
14
  self.configuration ||= Configuration.new
11
15
  yield(configuration)
12
- end
13
-
14
- class Configuration
15
- attr_accessor :project_name, :auth_token
16
+ self.autoloaded_models = configuration.autoload_models ? autoload_models : []
16
17
  end
17
18
  end
@@ -2,6 +2,8 @@ require 'json_api_client'
2
2
 
3
3
  module XapixClient
4
4
  class Connection < JsonApiClient::Connection
5
+ TOKEN = 'Authorization-Token'
6
+
5
7
  def initialize(options = {})
6
8
  fail(XapixClient::NoConfigurationError) if XapixClient.configuration.nil?
7
9
  fail(XapixClient::BadConfigurationError) if XapixClient.configuration.project_name.nil?
@@ -10,7 +12,7 @@ module XapixClient
10
12
 
11
13
  def run(request_method, path, params = {}, headers = {})
12
14
  fail(XapixClient::BadConfigurationError) if XapixClient.configuration.auth_token.nil?
13
- super(request_method, path, params, headers.merge(auth_token: XapixClient.configuration.auth_token))
15
+ super(request_method, path, params, headers.merge(TOKEN => XapixClient.configuration.auth_token))
14
16
  end
15
17
  end
16
18
  end
@@ -0,0 +1,7 @@
1
+ class XapixClient::Requestor < JsonApiClient::Query::Requestor
2
+ def transactional_create(records)
3
+ request(:post, 'transactions', {
4
+ data: records.map(&:as_json_api)
5
+ })
6
+ end
7
+ end
@@ -3,5 +3,54 @@ require 'json_api_client'
3
3
  module XapixClient
4
4
  class Resource < JsonApiClient::Resource
5
5
  self.connection_class = XapixClient::Connection
6
+ self.requestor_class = XapixClient::Requestor
7
+
8
+ class << self
9
+ def transactional_create(attributes = {}, associated_records)
10
+ new(attributes).tap do |resource|
11
+ resource.transactional_save(associated_records)
12
+ end
13
+ end
14
+ end
15
+
16
+ # Commit the current changes to the resource to the remote server.
17
+ # If the resource was previously loaded from the server, we will
18
+ # try to update the record. Otherwise if it's a new record, then
19
+ # we will try to create it
20
+ #
21
+ # @return [Boolean] Whether or not the save succeeded
22
+ def transactional_save(associated_records)
23
+ return false unless valid?
24
+ return false unless associated_records.all?(&:valid?)
25
+
26
+ self.last_result_set = if persisted?
27
+ #TODO
28
+ raise 'NOT YET IMPLEMENTED'
29
+ self.class.requestor.update(self)
30
+ else
31
+ self.class.requestor.transactional_create([self] + associated_records)
32
+ end
33
+
34
+ if last_result_set.has_errors?
35
+ last_result_set.errors.each do |error|
36
+ if error.source_parameter
37
+ errors.add(error.source_parameter, error.title)
38
+ else
39
+ errors.add(:base, error.title)
40
+ end
41
+ end
42
+ false
43
+ else
44
+ self.errors.clear if self.errors
45
+ mark_as_persisted!
46
+ associated_records.each(&:mark_as_persisted!)
47
+ if updated = last_result_set.first
48
+ self.attributes = updated.attributes
49
+ self.relationships.attributes = updated.relationships.attributes
50
+ clear_changes_information
51
+ end
52
+ true
53
+ end
54
+ end
6
55
  end
7
56
  end
@@ -1,3 +1,3 @@
1
1
  module XapixClient
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xapix_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Thamm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-23 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.0.0.beta6
47
+ version: 1.0.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 1.0.0.beta6
54
+ version: 1.0.1
55
55
  description: Access xapix.io hosted projects with the ActiveResource based json_api_client
56
56
  library.
57
57
  email:
@@ -63,6 +63,7 @@ files:
63
63
  - lib/xapix_client.rb
64
64
  - lib/xapix_client/config.rb
65
65
  - lib/xapix_client/connection.rb
66
+ - lib/xapix_client/requestor.rb
66
67
  - lib/xapix_client/resource.rb
67
68
  - lib/xapix_client/version.rb
68
69
  homepage: http://www.xapix.io