frederick_api 0.3.0 → 0.3.1

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: 7585f21ab85ccf59525a4d78a455952b54d8f902
4
- data.tar.gz: 38057990239e4c0af5b99ad07c36aa2cf6aea783
3
+ metadata.gz: 5ba4333ebf8f9acb3b07440cceb399011c16d04e
4
+ data.tar.gz: 626edbc12159446fa20ebb8cae9a7e9e70492ae3
5
5
  SHA512:
6
- metadata.gz: 6b603fee87f814268d6b9eb6a163e1941996a33e819c983a152227a44bd3e36077a4341021533b97323dabff808683af25eaa47e57a40103bc756b3c89a08e2b
7
- data.tar.gz: a3a0a5a8b77cdb1e76010a78a0079ca1733938cb1aaa973d8d412c3e0287d7c63d05ad36f5d2a53c0c3f460d5e124fad661664808b3404cc1eba507293dfaead
6
+ metadata.gz: f5176eee73f6cc0d5cc54169eb4cf7c431b249652c8b0b6f6fa0f0508b52a8b2a4321ff373a8084958245c3f8c5ed040a18a6ad445422b2aa68231bc2595e54c
7
+ data.tar.gz: 3988226a53e5b7696fe0aaed9c90feabc6b1cf535a40f86a8c2568c7c13df705c91d971f611b3fe35328dba925ffeb46b4ca254247d637708a1fa0c2b2c36367
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FrederickAPI
4
+ module V2
5
+ module Helpers
6
+ # HasMany association that supports returning a query builder instead of a simple array
7
+ # Allows querying of the association such as `contact_list.contacts.select('properties.external_id').all`
8
+ module HasMany
9
+ extend ActiveSupport::Concern
10
+
11
+ # Class methods added to resource
12
+ module ClassMethods
13
+ def has_many(attr_name, options = {})
14
+ self.associations = self.associations + [HasMany::Association.new(attr_name, self, options)]
15
+ end
16
+ end
17
+
18
+ # Class used to request data for has_many association
19
+ class Association < JsonApiClient::Associations::BaseAssociation
20
+ def query_builder(url)
21
+ association_class.query_builder.new(
22
+ association_class,
23
+ association_class.requestor_class.new(association_class, url)
24
+ )
25
+ end
26
+
27
+ def data(url)
28
+ query_builder(url)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -7,6 +7,13 @@ module FrederickAPI
7
7
  module Helpers
8
8
  # Used to convert nested params to dot notation for Frederick API
9
9
  class QueryBuilder < JsonApiClient::Query::Builder
10
+ attr_reader :requestor
11
+
12
+ def initialize(klass, requestor = nil)
13
+ super(klass)
14
+ @requestor = requestor || klass.requestor
15
+ end
16
+
10
17
  def params
11
18
  to_dot_params(
12
19
  filter_params.merge(pagination_params.merge(includes_params).merge(select_params))
@@ -16,6 +23,17 @@ module FrederickAPI
16
23
  .merge(additional_params)
17
24
  end
18
25
 
26
+ def find(args = {})
27
+ case args
28
+ when Hash
29
+ where(args)
30
+ else
31
+ @primary_key = args
32
+ end
33
+
34
+ requestor.get(params)
35
+ end
36
+
19
37
  def filter_params
20
38
  super_filter_params = super
21
39
 
@@ -5,6 +5,22 @@ module FrederickAPI
5
5
  module Helpers
6
6
  # Requestor for v2 client to use built on top of JsonApiClient::Query::Requestor
7
7
  class Requestor < JsonApiClient::Query::Requestor
8
+ attr_reader :path
9
+
10
+ def initialize(klass, path = nil)
11
+ @klass = klass
12
+ @path = path
13
+ end
14
+
15
+ def resource_path(parameters)
16
+ base_path = path || klass.path(parameters)
17
+ if (resource_id = parameters[klass.primary_key])
18
+ File.join(base_path, encode_part(resource_id))
19
+ else
20
+ base_path
21
+ end
22
+ end
23
+
8
24
  # Retry once on unhandled server errors
9
25
  def request(type, path, params)
10
26
  super
@@ -5,6 +5,8 @@ module FrederickAPI
5
5
  # Class from which Frederick V2 Resources inherit
6
6
  # Inherits functionality from JsonApiClient::Resource
7
7
  class Resource < JsonApiClient::Resource
8
+ include FrederickAPI::V2::Helpers::HasMany
9
+
8
10
  self.query_builder = FrederickAPI::V2::Helpers::QueryBuilder
9
11
  self.paginator = FrederickAPI::V2::Helpers::Paginator
10
12
  self.requestor_class = FrederickAPI::V2::Helpers::Requestor
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FrederickAPI
4
4
  # Current gem version
5
- VERSION = '0.3.0'
5
+ VERSION = '0.3.1'
6
6
  end
data/lib/frederick_api.rb CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  # Third-party libs
4
4
  require 'json_api_client'
5
- require 'active_support/core_ext/class/attribute.rb'
5
+ require 'active_support/core_ext/class/attribute'
6
+ require 'active_support/concern'
6
7
 
7
8
  # FrederickAPI libs
8
9
  require 'frederick_api/configuration'
10
+ require 'frederick_api/v2/helpers/has_many'
9
11
  require 'frederick_api/v2/helpers/paginator'
10
12
  require 'frederick_api/v2/helpers/query_builder'
11
13
  require 'frederick_api/v2/helpers/requestor'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frederick_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederick Engineering
@@ -41,6 +41,7 @@ files:
41
41
  - lib/frederick_api/v2/contact_list.rb
42
42
  - lib/frederick_api/v2/contact_property.rb
43
43
  - lib/frederick_api/v2/contact_type.rb
44
+ - lib/frederick_api/v2/helpers/has_many.rb
44
45
  - lib/frederick_api/v2/helpers/paginator.rb
45
46
  - lib/frederick_api/v2/helpers/query_builder.rb
46
47
  - lib/frederick_api/v2/helpers/requestor.rb