zaikio-hub 0.3.0 → 0.6.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
  SHA256:
3
- metadata.gz: 578ae236058ff3d1e4191cf07f794d361f2234097fee9130f060d6056d52760c
4
- data.tar.gz: 1e4b12ea5289e4735323eeedf06467fffd80753696373929985bd74046ff4c35
3
+ metadata.gz: 76ec2ffbec46a39d7132335f3855db80abbe10eaf2c8aebcedb31c1785715f6b
4
+ data.tar.gz: d69171f7abc873c12c766ddfdd188d7f0cd7be0df4c81ab0206cc9ef9cc255c4
5
5
  SHA512:
6
- metadata.gz: cd4cc812ec6b153ee9af72774c52f9d64eb38f6d2eb976fe92547c140f1913ece9e65aea1b6f8c7e02c649a54cdf756639ccb71c3e5e2df42dff56a96d2eeabf
7
- data.tar.gz: 171bf01b4ad7bc0a6e1c9b3acb9972d89f2287c795d3e9a401b06786bc3bf8fbb87b07f5c6b95c7410d39300d799de3e88466d5d6b8674f160b85459d66379a5
6
+ metadata.gz: 5290f7d1e9edfb3f08522e1098b663fe7f10cde3978a3ae9c48680b9ff90bc6b599d356c5536277311398716088039eda6046cb234ae1d14f9813c1ad27fdc0d
7
+ data.tar.gz: c88e8b7d077797ae90a843f42ceeb9e0c999ff69f1add278a70b5acd875ddd86934ab59f917af0547b1cf7031b8dc7847faf653a7f1476fdee7da8196f2402cb
data/CHANGELOG.md CHANGED
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.6.1] - 2020-04-12
11
+
12
+ * Fixed that `each_page` works correctly with client instance
13
+
14
+ ## [0.6.0] - 2020-04-12
15
+
16
+ * Added pagination features for `Subscription` and `Connection`
17
+
18
+ ## [0.5.0] - 2020-04-08
19
+
20
+ * Added `granted_oauth_scopes`, `requested_oauth_scopes`, and `requested_oauth_scopes_waiting_for_approval` to Person and Organization
21
+ * Added `requested_oauth_scopes`, and `requested_oauth_scopes_waiting_for_approval` to Connection
22
+
23
+ ## [0.4.1] - 2020-03-04
24
+
25
+ * Add `state` property for `Zaikio::Hub::Address` model
26
+
27
+ ## [0.4.0] - 2020-02-23
28
+
29
+ ### Changed
30
+
31
+ * Only log Faraday req/response status lines, not headers
32
+ * Added nested attributes for `address_attributes` in `Site`
33
+
10
34
  ## [0.3.0] - 2020-02-12
11
35
 
12
36
  ### Changed
@@ -33,7 +57,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
33
57
  ### Added
34
58
  - Added subscriptions (migration required)
35
59
 
36
- [Unreleased]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.3.0...HEAD
60
+ [Unreleased]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.1..HEAD
61
+ [0.6.1]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.0..v0.6.1
62
+ [0.6.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.5.0..v0.6.0
63
+ [0.5.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.4.1..v0.5.0
64
+ [0.4.1]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.4.0...v0.4.1
65
+ [0.4.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.3.0...v0.4.0
37
66
  [0.3.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.2.0...v.0.3.0
38
67
  [0.2.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.1.1...v.0.2.0
39
68
  [0.1.1]: https://github.com/zaikio/zaikio-hub-ruby/compare/5c6cb4dbcac316733560ddb2b1e13b53e55eb66e...v0.1.1
data/README.md CHANGED
@@ -122,7 +122,17 @@ client.test_accounts.create(
122
122
 
123
123
  ```rb
124
124
  Zaikio::Hub.with_basic_auth(client_id, client_secret) do
125
- connections = Zaikio::Hub::Connection.all
125
+ # Iterate through pages
126
+ Zaikio::Hub::Connection.per_page(10).each_page do |connections|
127
+ connections.size # e.g. 10
128
+ connections.total_count
129
+ connections.total_pages
130
+ end
131
+ # Load 2nd page
132
+ Zaikio::Hub::Connection.per_page(10).page(2)
133
+ # All
134
+ all_connections = Zaikio::Hub::Connection.all.each_page.flat_map(&:to_a)
135
+
126
136
  subscription = Zaikio::Hub::Subscription.find("Organization-b1475f65-236c-58b8-96e1-e1778b43beb7")
127
137
  subscription.plan # => "advanced"
128
138
  subscription.activate!
data/lib/zaikio/hub.rb CHANGED
@@ -5,6 +5,9 @@ require "zaikio/hub/json_parser"
5
5
  require "zaikio/hub/authorization_middleware"
6
6
  require "zaikio/hub/basic_auth_middleware"
7
7
 
8
+ # Pagination
9
+ require "zaikio/hub/pagination"
10
+
8
11
  # Models
9
12
  require "zaikio/error"
10
13
  require "zaikio/hub/client"
@@ -17,6 +20,7 @@ require "zaikio/hub/person"
17
20
  require "zaikio/hub/machine"
18
21
  require "zaikio/hub/software"
19
22
  require "zaikio/hub/specialist"
23
+ require "zaikio/hub/address"
20
24
  require "zaikio/hub/site"
21
25
  require "zaikio/hub/membership"
22
26
  require "zaikio/hub/current_person"
@@ -68,7 +72,8 @@ module Zaikio
68
72
  self.connection = Faraday.new(url: "#{configuration.host}/api/v1",
69
73
  ssl: { verify: configuration.environment != :test }) do |c|
70
74
  c.request :json
71
- c.response :logger, configuration&.logger
75
+ c.response :logger, configuration&.logger, headers: false
76
+ c.use Zaikio::Hub::Pagination::HeaderParser
72
77
  c.use JSONParser
73
78
  c.use AuthorizationMiddleware
74
79
  c.use BasicAuthMiddleware
@@ -0,0 +1,18 @@
1
+ module Zaikio
2
+ module Hub
3
+ class Address < Base
4
+ # Attributes
5
+ attributes :addressee,
6
+ :addition,
7
+ :country_code,
8
+ :state,
9
+ :street_and_number,
10
+ :text,
11
+ :town,
12
+ :zip_code
13
+
14
+ # Associations
15
+ belongs_to :site, uri: nil, class_name: "Zaikio::Hub::Site"
16
+ end
17
+ end
18
+ end
@@ -19,6 +19,13 @@ module Zaikio
19
19
  result
20
20
  end
21
21
  end
22
+
23
+ def each_page
24
+ super() do |page|
25
+ page = RequestWrapper.new(page, @client)
26
+ yield(page)
27
+ end
28
+ end
22
29
  end
23
30
 
24
31
  class Client
@@ -1,13 +1,16 @@
1
1
  module Zaikio
2
2
  module Hub
3
3
  class Connection < Base
4
+ include Zaikio::Hub::Pagination::Scopes
5
+
4
6
  uri "connections(/:id)"
5
7
 
6
8
  include_root_in_json :connection
7
9
 
8
10
  # Attributes
9
11
  attributes :updated_at, :created_at, :connectable_type, :connectable_id,
10
- :app_name, :granted_oauth_scopes
12
+ :app_name, :granted_oauth_scopes, :requested_oauth_scopes,
13
+ :requested_oauth_scopes_waiting_for_approval
11
14
 
12
15
  def initialize(attributes = {})
13
16
  if attributes["connectable_id"]
@@ -18,7 +18,9 @@ module Zaikio
18
18
  # Attributes
19
19
  attributes :name, :slug, :logo_url, :connected, :subscription,
20
20
  :created_at, :updated_at, :country_code, :kinds,
21
- :sections, :currency, :brand_color, :test_account_owner_id
21
+ :sections, :currency, :brand_color, :test_account_owner_id,
22
+ :granted_oauth_scopes, :requested_oauth_scopes,
23
+ :requested_oauth_scopes_waiting_for_approval
22
24
 
23
25
  # Associations
24
26
  has_many :memberships, class_name: "Zaikio::Hub::Membership",
@@ -10,7 +10,8 @@ module Zaikio
10
10
  :pronoun, :locale, :country_code, :currency, :unit_system, :connected,
11
11
  :test_account_owner_id, :time_zone, :email_confirmed,
12
12
  :two_factor_authentication_enabled, :avatar_url, :subscription,
13
- :accessible_apps
13
+ :accessible_apps, :granted_oauth_scopes, :requested_oauth_scopes,
14
+ :requested_oauth_scopes_waiting_for_approval
14
15
 
15
16
  def self.find
16
17
  all.find_one
@@ -8,7 +8,9 @@ module Zaikio
8
8
  # Attributes
9
9
  attributes :name, :slug, :logo_url, :connected, :subscription,
10
10
  :created_at, :updated_at, :country_code, :kinds,
11
- :sections, :currency, :brand_color, :test_account_owner_id
11
+ :sections, :currency, :brand_color, :test_account_owner_id,
12
+ :granted_oauth_scopes, :requested_oauth_scopes,
13
+ :requested_oauth_scopes_waiting_for_approval
12
14
 
13
15
  # Associations
14
16
  has_many :memberships, class_name: "Zaikio::Hub::Membership",
@@ -0,0 +1,19 @@
1
+ # adapted from https://github.com/balvig/spyke-kaminari
2
+
3
+ module Zaikio
4
+ module Hub
5
+ module Pagination
6
+ HEADERS = {
7
+ total_count: "Total-Count",
8
+ total_pages: "Total-Pages",
9
+ current_page: "Current-Page"
10
+ }.freeze
11
+
12
+ METADATA_KEY = :pagination
13
+ end
14
+ end
15
+ end
16
+
17
+ require "zaikio/hub/pagination/scopes"
18
+ require "zaikio/hub/pagination/relation"
19
+ require "zaikio/hub/pagination/header_parser"
@@ -0,0 +1,25 @@
1
+ module Zaikio
2
+ module Hub
3
+ module Pagination
4
+ class HeaderParser < Faraday::Response::Middleware
5
+ def on_complete(env)
6
+ @env = env
7
+
8
+ metadata = Zaikio::Hub::Pagination::HEADERS.transform_values do |key|
9
+ header(key)
10
+ end
11
+
12
+ @env.body[:metadata] ||= {}
13
+ @env.body[:metadata][Zaikio::Hub::Pagination::METADATA_KEY] = metadata
14
+ end
15
+
16
+ private
17
+
18
+ def header(key)
19
+ value = @env.response_headers[key]
20
+ value.try(:to_i)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module Zaikio
2
+ module Hub
3
+ module Pagination
4
+ class Relation < Spyke::Relation
5
+ Zaikio::Hub::Pagination::HEADERS.each_key do |symbol|
6
+ define_method(symbol) do
7
+ find_some.metadata[Zaikio::Hub::Pagination::METADATA_KEY][symbol]
8
+ end
9
+ end
10
+
11
+ def first_page?
12
+ current_page == 1
13
+ end
14
+
15
+ def next_page
16
+ current_page + 1
17
+ end
18
+
19
+ def last_page?
20
+ current_page == total_pages
21
+ end
22
+
23
+ def out_of_range?
24
+ current_page > total_pages
25
+ end
26
+
27
+ def each_page
28
+ return to_enum(:each_page) unless block_given?
29
+
30
+ relation = clone
31
+ yield relation
32
+ return if relation.last_page? || relation.out_of_range?
33
+
34
+ (relation.next_page..relation.total_pages).each do |number|
35
+ yield clone.page(number)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ module Zaikio
2
+ module Hub
3
+ module Pagination
4
+ module Scopes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ %i[page per_page].each do |symbol|
9
+ scope symbol, ->(value) { where(symbol => value) }
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ # Overrides the method included by Spyke::Scoping to return paginated
15
+ # relations.
16
+ def all
17
+ current_scope || Zaikio::Hub::Pagination::Relation.new(self, uri: uri)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,7 +5,9 @@ module Zaikio
5
5
  attributes :updated_at, :created_at, :first_name, :name, :full_name, :email,
6
6
  :pronoun, :locale, :country_code, :currency, :unit_system, :connected,
7
7
  :test_account_owner_id, :time_zone, :email_confirmed,
8
- :two_factor_authentication_enabled, :avatar_url, :subscription
8
+ :two_factor_authentication_enabled, :avatar_url, :subscription,
9
+ :granted_oauth_scopes, :requested_oauth_scopes,
10
+ :requested_oauth_scopes_waiting_for_approval
9
11
  end
10
12
  end
11
13
  end
@@ -7,6 +7,20 @@ module Zaikio
7
7
  # Attributes
8
8
  attributes :name, :headquarter, :created_at, :updated_at, :address,
9
9
  :address_attributes
10
+
11
+ # Associations
12
+ has_one :address, uri: nil, class_name: "Zaikio::Hub::Address"
13
+
14
+ accepts_nested_attributes_for :address
15
+
16
+ # Callbacks
17
+ before_save :set_address_attributes
18
+
19
+ private
20
+
21
+ def set_address_attributes
22
+ attributes[:address_attributes] = address
23
+ end
10
24
  end
11
25
  end
12
26
  end
@@ -1,6 +1,8 @@
1
1
  module Zaikio
2
2
  module Hub
3
3
  class Subscription < Base
4
+ include Zaikio::Hub::Pagination::Scopes
5
+
4
6
  uri "subscriptions(/:id)"
5
7
 
6
8
  include_root_in_json :subscription
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module Hub
3
- VERSION = "0.3.0".freeze
3
+ VERSION = "0.6.1".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-hub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - crispymtn
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-02-12 00:00:00.000000000 Z
13
+ date: 2021-04-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: concurrent-ruby
@@ -84,16 +84,22 @@ dependencies:
84
84
  name: spyke
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: 5.3.4
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: 5.5.0
90
93
  type: :runtime
91
94
  prerelease: false
92
95
  version_requirements: !ruby/object:Gem::Requirement
93
96
  requirements:
94
- - - "~>"
97
+ - - ">="
95
98
  - !ruby/object:Gem::Version
96
99
  version: 5.3.4
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: 5.5.0
97
103
  description: Ruby API Client for Zaikio's Hub
98
104
  email:
99
105
  - op@crispymtn.com
@@ -109,6 +115,7 @@ files:
109
115
  - Rakefile
110
116
  - lib/zaikio/error.rb
111
117
  - lib/zaikio/hub.rb
118
+ - lib/zaikio/hub/address.rb
112
119
  - lib/zaikio/hub/asset.rb
113
120
  - lib/zaikio/hub/authorization_middleware.rb
114
121
  - lib/zaikio/hub/base.rb
@@ -124,6 +131,10 @@ files:
124
131
  - lib/zaikio/hub/membership.rb
125
132
  - lib/zaikio/hub/organization.rb
126
133
  - lib/zaikio/hub/organization_membership.rb
134
+ - lib/zaikio/hub/pagination.rb
135
+ - lib/zaikio/hub/pagination/header_parser.rb
136
+ - lib/zaikio/hub/pagination/relation.rb
137
+ - lib/zaikio/hub/pagination/scopes.rb
127
138
  - lib/zaikio/hub/person.rb
128
139
  - lib/zaikio/hub/revoked_access_token.rb
129
140
  - lib/zaikio/hub/role.rb