zaikio-hub 0.6.0 → 0.8.0

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: 71418bf55d46197710099e666b3db5b04fb9e08264b0c21f874f7ea9680dd9b0
4
- data.tar.gz: cccda672c5c0149bc602605278b61e28ac2bef9b930e98bf0b17664562a62d2c
3
+ metadata.gz: 334a558132d7077ca054fbdf5c085025e324d7e37061d5608bcd8e4ab11b8266
4
+ data.tar.gz: cc234b4c4dbb4dfad4f0fc68cdd58091206057701b7307fa26a2cc18e6ae4705
5
5
  SHA512:
6
- metadata.gz: e0acec4f0cd530d9f4cb52b49eb53a86bf9aff30a576b02ab0ddd6573c3ef031b97a14ef3d13b451e61f9d7233e0232c4b50ad8bfd9afbeceaaf5d341df09256
7
- data.tar.gz: '09c81fcb75e68d73e27ce8b69f2d0bc329b42e0fd7286b8fc0e825df50b40fbfabd1c4f1fd13a344126c79a99a1a52c4e77f70c27f6c26c9f13e40f0cbfcfa90'
6
+ metadata.gz: c84626e8323410a6d4d4f28302ec04fc18d61ee52347ed281635cbdcc389291f9df274cf4f53e5cbf0b4086da2b478ea28b4c9bd7aa89398f495783432589c7a
7
+ data.tar.gz: 75cc5d306c754be2f701e629818e65746cf0fef20dd3129af3587be68b8251b7a8b917e008c19a905842da1ed3962ba69eba22c871de431eba2c5f4f386c52c6
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.8.0] - 2021-08-25
11
+
12
+ * Added `CurrentOrganization#create_subscription`
13
+
14
+ ## [0.7.0] - 2021-06-18
15
+
16
+ * **BREAKING** `Relation#all` now fetches all following paginated resources by default
17
+ (endpoints which don't support pagination are unaffected).
18
+ * **BREAKING** Removed `Relation#each_page` method. You should just use the `all` method now
19
+ instead:
20
+
21
+ ```diff
22
+ -Model.all.each_page.flat_map(&:to_a)
23
+ +Model.all.to_a
24
+ ```
25
+
26
+ ## [0.6.2] - 2021-04-27
27
+
28
+ * Include `id` property in `CurrentPerson#attributes` and `CurrentOrganization#attributes`
29
+
30
+ ## [0.6.1] - 2020-04-12
31
+
32
+ * Fixed that `each_page` works correctly with client instance
33
+
10
34
  ## [0.6.0] - 2020-04-12
11
35
 
12
36
  * Added pagination features for `Subscription` and `Connection`
@@ -53,7 +77,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
53
77
  ### Added
54
78
  - Added subscriptions (migration required)
55
79
 
56
- [Unreleased]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.0..HEAD
80
+ [Unreleased]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.7.0..HEAD
81
+ [0.7.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.2..v0.7.0
82
+ [0.6.2]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.1..v0.6.2
83
+ [0.6.1]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.6.0..v0.6.1
57
84
  [0.6.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.5.0..v0.6.0
58
85
  [0.5.0]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.4.1..v0.5.0
59
86
  [0.4.1]: https://github.com/zaikio/zaikio-hub-ruby/compare/v0.4.0...v0.4.1
data/README.md CHANGED
@@ -46,8 +46,10 @@ token = "..." # Your valid JWT for an organization
46
46
  Zaikio::Hub.with_token(token) do
47
47
  organization = Zaikio::Hub::CurrentOrganization.new
48
48
 
49
- # Fetch Data
49
+ # Fetch data
50
50
  organization.memberships.find('abc')
51
+
52
+ # Note that .all understands pagination headers and will fetch all pages
51
53
  organization.members.all
52
54
  organization.machines.all
53
55
  organization.software.all
@@ -122,16 +124,15 @@ client.test_accounts.create(
122
124
 
123
125
  ```rb
124
126
  Zaikio::Hub.with_basic_auth(client_id, client_secret) do
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)
127
+ # Get total number of records
128
+ Zaikio::Hub::Connection.per_page(10).total_count
129
+ #=> 20
130
+
131
+ # Fetch just the second page
132
+ Zaikio::Hub::Connection.per_page(10).page(2).find_some
133
+
133
134
  # All
134
- all_connections = Zaikio::Hub::Connection.all.each_page.flat_map(&:to_a)
135
+ all_connections = Zaikio::Hub::Connection.all.to_a
135
136
 
136
137
  subscription = Zaikio::Hub::Subscription.find("Organization-b1475f65-236c-58b8-96e1-e1778b43beb7")
137
138
  subscription.plan # => "advanced"
@@ -1,6 +1,8 @@
1
+ require "zaikio/client/model"
2
+
1
3
  module Zaikio
2
4
  module Hub
3
- class Base < Spyke::Base
5
+ class Base < Zaikio::Client::Model
4
6
  self.callback_methods = { create: :post, update: :patch }.freeze
5
7
  end
6
8
  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,8 +1,6 @@
1
1
  module Zaikio
2
2
  module Hub
3
3
  class Connection < Base
4
- include Zaikio::Hub::Pagination::Scopes
5
-
6
4
  uri "connections(/:id)"
7
5
 
8
6
  include_root_in_json :connection
@@ -16,7 +16,7 @@ module Zaikio
16
16
  include_root_in_json :organization
17
17
 
18
18
  # Attributes
19
- attributes :name, :slug, :logo_url, :connected, :subscription,
19
+ attributes :id, :name, :slug, :logo_url, :connected, :subscription,
20
20
  :created_at, :updated_at, :country_code, :kinds,
21
21
  :sections, :currency, :brand_color, :test_account_owner_id,
22
22
  :granted_oauth_scopes, :requested_oauth_scopes,
@@ -47,6 +47,14 @@ module Zaikio
47
47
  def members
48
48
  memberships.with_fallback.map(&:person)
49
49
  end
50
+
51
+ def create_subscription(status: "active", plan_name: nil)
52
+ if Zaikio::Hub.current_token_data.subject_type == "Organization"
53
+ result = self.class.request(:post, "organization/subscription",
54
+ subscription: { status: status, plan_name: plan_name })
55
+ Zaikio::Hub::Subscription.new(result.data)
56
+ end
57
+ end
50
58
  end
51
59
  end
52
60
  end
@@ -6,7 +6,7 @@ module Zaikio
6
6
  uri "person"
7
7
 
8
8
  # Attributes
9
- attributes :updated_at, :created_at, :first_name, :name, :full_name, :email,
9
+ attributes :id, :updated_at, :created_at, :first_name, :name, :full_name, :email,
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,
@@ -1,8 +1,6 @@
1
1
  module Zaikio
2
2
  module Hub
3
3
  class Subscription < Base
4
- include Zaikio::Hub::Pagination::Scopes
5
-
6
4
  uri "subscriptions(/:id)"
7
5
 
8
6
  include_root_in_json :subscription
@@ -10,7 +8,7 @@ module Zaikio
10
8
  # Attributes
11
9
  attributes :updated_at, :created_at, :subscriber_type, :subscriber_id,
12
10
  :status, :app_name, :activated_at, :last_billed_at,
13
- :last_paid_at, :trial_ended_at, :plan, :preceding_plan,
11
+ :last_paid_at, :trial_ended_at, :plan, :plan_name, :preceding_plan,
14
12
  :changed_plan_at, :usages_in_current_billing_period
15
13
 
16
14
  def initialize(attributes = {})
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module Hub
3
- VERSION = "0.6.0".freeze
3
+ VERSION = "0.8.0".freeze
4
4
  end
5
5
  end
data/lib/zaikio/hub.rb CHANGED
@@ -1,15 +1,12 @@
1
1
  require "faraday"
2
2
  require "spyke"
3
+ require "zaikio-client-helpers"
4
+
3
5
  require "zaikio/hub/configuration"
4
- require "zaikio/hub/json_parser"
5
6
  require "zaikio/hub/authorization_middleware"
6
7
  require "zaikio/hub/basic_auth_middleware"
7
8
 
8
- # Pagination
9
- require "zaikio/hub/pagination"
10
-
11
9
  # Models
12
- require "zaikio/error"
13
10
  require "zaikio/hub/client"
14
11
  require "zaikio/hub/base"
15
12
  require "zaikio/hub/asset"
@@ -73,8 +70,8 @@ module Zaikio
73
70
  ssl: { verify: configuration.environment != :test }) do |c|
74
71
  c.request :json
75
72
  c.response :logger, configuration&.logger, headers: false
76
- c.use Zaikio::Hub::Pagination::HeaderParser
77
- c.use JSONParser
73
+ c.use Zaikio::Client::Helpers::Pagination::FaradayMiddleware
74
+ c.use Zaikio::Client::Helpers::JSONParser
78
75
  c.use AuthorizationMiddleware
79
76
  c.use BasicAuthMiddleware
80
77
  c.adapter Faraday.default_adapter
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.6.0
4
+ version: 0.8.0
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-04-12 00:00:00.000000000 Z
13
+ date: 2021-08-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: concurrent-ruby
@@ -69,7 +69,7 @@ dependencies:
69
69
  version: 3.10.5
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
- version: 3.12.0
72
+ version: 3.14.0
73
73
  type: :runtime
74
74
  prerelease: false
75
75
  version_requirements: !ruby/object:Gem::Requirement
@@ -79,7 +79,7 @@ dependencies:
79
79
  version: 3.10.5
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
- version: 3.12.0
82
+ version: 3.14.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: spyke
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -89,7 +89,7 @@ dependencies:
89
89
  version: 5.3.4
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
- version: 5.5.0
92
+ version: 6.1.0
93
93
  type: :runtime
94
94
  prerelease: false
95
95
  version_requirements: !ruby/object:Gem::Requirement
@@ -99,7 +99,21 @@ dependencies:
99
99
  version: 5.3.4
100
100
  - - "<"
101
101
  - !ruby/object:Gem::Version
102
- version: 5.5.0
102
+ version: 6.1.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: zaikio-client-helpers
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.2'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.2'
103
117
  description: Ruby API Client for Zaikio's Hub
104
118
  email:
105
119
  - op@crispymtn.com
@@ -113,7 +127,6 @@ files:
113
127
  - MIT-LICENSE
114
128
  - README.md
115
129
  - Rakefile
116
- - lib/zaikio/error.rb
117
130
  - lib/zaikio/hub.rb
118
131
  - lib/zaikio/hub/address.rb
119
132
  - lib/zaikio/hub/asset.rb
@@ -126,15 +139,10 @@ files:
126
139
  - lib/zaikio/hub/connection.rb
127
140
  - lib/zaikio/hub/current_organization.rb
128
141
  - lib/zaikio/hub/current_person.rb
129
- - lib/zaikio/hub/json_parser.rb
130
142
  - lib/zaikio/hub/machine.rb
131
143
  - lib/zaikio/hub/membership.rb
132
144
  - lib/zaikio/hub/organization.rb
133
145
  - 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
138
146
  - lib/zaikio/hub/person.rb
139
147
  - lib/zaikio/hub/revoked_access_token.rb
140
148
  - lib/zaikio/hub/role.rb
data/lib/zaikio/error.rb DELETED
@@ -1,18 +0,0 @@
1
- module Zaikio
2
- class Error < StandardError; end
3
-
4
- class ConnectionError < Zaikio::Error; end
5
-
6
- class ResourceNotFound < Zaikio::Error; end
7
- end
8
-
9
- module Spyke
10
- instance_eval do
11
- # avoid warning: already initialized constant
12
- remove_const("ConnectionError")
13
- remove_const("ResourceNotFound")
14
- end
15
-
16
- ConnectionError = Class.new Zaikio::ConnectionError
17
- ResourceNotFound = Class.new Zaikio::ResourceNotFound
18
- end
@@ -1,36 +0,0 @@
1
- require "multi_json"
2
-
3
- module Zaikio
4
- module Hub
5
- class JSONParser < Faraday::Response::Middleware
6
- def on_complete(env)
7
- connection_error(env) unless /^(2\d\d)|422|404$/.match?(env.status.to_s)
8
-
9
- raise Spyke::ResourceNotFound if env.status.to_s == "404"
10
-
11
- env.body = parse_body(env.body)
12
- end
13
-
14
- def connection_error(env)
15
- Zaikio::Hub.configuration.logger
16
- .error("Zaikio::Hub Status Code #{env.status}, #{env.body}")
17
- raise Spyke::ConnectionError, "Status Code #{env.status}, #{env.body}"
18
- end
19
-
20
- def parse_body(body)
21
- json = MultiJson.load(body, symbolize_keys: true)
22
- {
23
- data: json,
24
- metadata: {},
25
- errors: json.is_a?(Hash) ? json[:errors] : {}
26
- }
27
- rescue MultiJson::ParseError
28
- {
29
- data: {},
30
- metadata: {},
31
- errors: {}
32
- }
33
- end
34
- end
35
- end
36
- end
@@ -1,25 +0,0 @@
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
@@ -1,41 +0,0 @@
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
@@ -1,23 +0,0 @@
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
@@ -1,19 +0,0 @@
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"