zaius 0.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 +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zaius/api_operations/list.rb +14 -0
- data/lib/zaius/api_operations/request.rb +41 -0
- data/lib/zaius/api_resource.rb +63 -0
- data/lib/zaius/customer.rb +9 -0
- data/lib/zaius/errors.rb +44 -0
- data/lib/zaius/list.rb +7 -0
- data/lib/zaius/list_object.rb +56 -0
- data/lib/zaius/subscription.rb +59 -0
- data/lib/zaius/util.rb +304 -0
- data/lib/zaius/version.rb +3 -0
- data/lib/zaius/zaius_client.rb +273 -0
- data/lib/zaius/zaius_object.rb +472 -0
- data/lib/zaius/zaius_response.rb +45 -0
- data/lib/zaius.rb +76 -0
- data/zaius.gemspec +29 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 57b238d3a6ff59579af7036c014514c77170ab67e15060d602fd5e46c3df2178
|
4
|
+
data.tar.gz: 49536b0b47debd6fd4a458eab4eaeb36932264e832d4def12cc8f0bb00b7ade7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 16108b1f95352dc7c8a072586e685fa6d6f00f035790789d9f7415722c08f174e97f6fb87d04244f64fc61b1fd9527577ce6d9b284e991533787ee579908bd0c
|
7
|
+
data.tar.gz: e2f9e007d7627f79f454fc71e093ef69690c9b1d2b994abef402e4e2635cea7240ad36c7e2c7d23115b8a5e8a156216e6acc338c0395b896c8b6410ba2185e7c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zaius (0.1.0)
|
5
|
+
faraday (~> 0.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
byebug (10.0.2)
|
11
|
+
faraday (0.15.3)
|
12
|
+
multipart-post (>= 1.2, < 3)
|
13
|
+
minitest (5.11.3)
|
14
|
+
multipart-post (2.0.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
bundler (~> 1.16)
|
22
|
+
byebug
|
23
|
+
minitest (~> 5.0)
|
24
|
+
rake (~> 10.0)
|
25
|
+
zaius!
|
26
|
+
|
27
|
+
BUNDLED WITH
|
28
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Chris Anderson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Zaius
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zaius`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'zaius'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install zaius
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/chrismanderson/zaius.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zaius"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zaius
|
4
|
+
module APIOperations
|
5
|
+
module List
|
6
|
+
def list(filters = {}, opts = {})
|
7
|
+
resp, opts = request(:get, resource_url, filters, opts)
|
8
|
+
obj = ListObject.construct_from(resp.data, opts)
|
9
|
+
|
10
|
+
obj
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zaius
|
4
|
+
module APIOperations
|
5
|
+
module Request
|
6
|
+
module ClassMethods
|
7
|
+
def request(method, url, params = {}, opts = {})
|
8
|
+
opts[:client] ||= ZaiusClient.active_client
|
9
|
+
|
10
|
+
headers = opts.clone
|
11
|
+
api_key = headers.delete(:api_key)
|
12
|
+
api_base = headers.delete(:api_base)
|
13
|
+
client = headers.delete(:client)
|
14
|
+
# Assume all remaining opts must be headers
|
15
|
+
|
16
|
+
resp, opts[:api_key] = client.execute_request(
|
17
|
+
method, url,
|
18
|
+
api_base: api_base, api_key: api_key,
|
19
|
+
headers: headers, params: params
|
20
|
+
)
|
21
|
+
|
22
|
+
# Hash#select returns an array before 1.9
|
23
|
+
opts_to_persist = {}
|
24
|
+
|
25
|
+
[resp, opts_to_persist]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.included(base)
|
30
|
+
base.extend(ClassMethods)
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def request(method, url, params = {}, opts = {})
|
36
|
+
opts = @opts.merge(Util.normalize_opts(opts))
|
37
|
+
self.class.request(method, url, params, opts)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zaius
|
4
|
+
class APIResource < ZaiusObject
|
5
|
+
include Zaius::APIOperations::Request
|
6
|
+
|
7
|
+
def self.class_name
|
8
|
+
name.split("::")[-1]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.resource_url
|
12
|
+
if self == APIResource
|
13
|
+
raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)"
|
14
|
+
end
|
15
|
+
# Namespaces are separated in object names with periods (.) and in URLs
|
16
|
+
# with forward slashes (/), so replace the former with the latter.
|
17
|
+
"/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
|
18
|
+
end
|
19
|
+
|
20
|
+
# A metaprogramming call that specifies that a field of a resource can be
|
21
|
+
# its own type of API resource (say a nested card under an account for
|
22
|
+
# example), and if that resource is set, it should be transmitted to the
|
23
|
+
# API on a create or update. Doing so is not the default behavior because
|
24
|
+
# API resources should normally be persisted on their own RESTful
|
25
|
+
# endpoints.
|
26
|
+
def self.save_nested_resource(name)
|
27
|
+
define_method(:"#{name}=") do |value|
|
28
|
+
super(value)
|
29
|
+
|
30
|
+
# The parent setter will perform certain useful operations like
|
31
|
+
# converting to an APIResource if appropriate. Refresh our argument
|
32
|
+
# value to whatever it mutated to.
|
33
|
+
value = send(name)
|
34
|
+
|
35
|
+
# Note that the value may be subresource, but could also be a scalar
|
36
|
+
# (like a tokenized card's ID for example), so we check the type before
|
37
|
+
# setting #save_with_parent here.
|
38
|
+
value.save_with_parent = true if value.is_a?(APIResource)
|
39
|
+
|
40
|
+
value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def resource_url
|
45
|
+
unless (id = self["id"])
|
46
|
+
raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", "id")
|
47
|
+
end
|
48
|
+
"#{self.class.resource_url}/#{CGI.escape(id)}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def refresh
|
52
|
+
resp, opts = request(:get, resource_url, @retrieve_params)
|
53
|
+
initialize_from(resp.data, opts)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.retrieve(id, opts = {})
|
57
|
+
opts = Util.normalize_opts(opts)
|
58
|
+
instance = new(id, opts)
|
59
|
+
instance.refresh
|
60
|
+
instance
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/zaius/errors.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Zaius
|
2
|
+
class ZaiusError < StandardError
|
3
|
+
|
4
|
+
attr_reader :code
|
5
|
+
attr_reader :http_body
|
6
|
+
attr_reader :http_headers
|
7
|
+
attr_reader :http_status
|
8
|
+
attr_reader :json_body # equivalent to #data
|
9
|
+
|
10
|
+
attr_accessor :response
|
11
|
+
|
12
|
+
def initialize(message = nil, http_status: nil, http_body: nil, json_body: nil,
|
13
|
+
http_headers: nil, code: nil)
|
14
|
+
@message = message
|
15
|
+
@http_status = http_status
|
16
|
+
@http_body = http_body
|
17
|
+
@http_headers = http_headers || {}
|
18
|
+
@json_body = json_body
|
19
|
+
@code = code
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
24
|
+
"#{status_string}#{@message}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class APIError < ZaiusError
|
29
|
+
def initialize(title:, http_status: nil, detail: {})
|
30
|
+
@message = title
|
31
|
+
@http_status = http_status
|
32
|
+
|
33
|
+
@json_body = detail
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
37
|
+
"#{status_string}#{@message}: #{json_body}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class AuthenticationError < StandardError
|
43
|
+
end
|
44
|
+
end
|
data/lib/zaius/list.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Zaius
|
2
|
+
class ListObject < ZaiusObject
|
3
|
+
include Enumerable
|
4
|
+
include Zaius::APIOperations::List
|
5
|
+
include Zaius::APIOperations::Request
|
6
|
+
# include Zaius::APIOperations::Create
|
7
|
+
|
8
|
+
OBJECT_NAME = "list".freeze
|
9
|
+
|
10
|
+
# This accessor allows a `ListObject` to inherit various filters that were
|
11
|
+
# given to a predecessor. This allows for things like consistent limits,
|
12
|
+
# expansions, and predicates as a user pages through resources.
|
13
|
+
attr_accessor :filters
|
14
|
+
|
15
|
+
# An empty list object. This is returned from +next+ when we know that
|
16
|
+
# there isn't a next page in order to replicate the behavior of the API
|
17
|
+
# when it attempts to return a page beyond the last.
|
18
|
+
def self.empty_list(opts = {})
|
19
|
+
ListObject.construct_from({ data: [] }, opts)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(*args)
|
23
|
+
super
|
24
|
+
self.filters = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def [](k)
|
28
|
+
case k
|
29
|
+
when String, Symbol
|
30
|
+
super
|
31
|
+
else
|
32
|
+
raise ArgumentError, "You tried to access the #{k.inspect} index, but ListObject types only support String keys. (HINT: List calls return an object with a 'data' (which is the data array). You likely want to call #data[#{k.inspect}])"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Iterates through each resource in the page represented by the current
|
37
|
+
# `ListObject`.
|
38
|
+
#
|
39
|
+
# Note that this method makes no effort to fetch a new page when it gets to
|
40
|
+
# the end of the current page's resources. See also +auto_paging_each+.
|
41
|
+
def each(&blk)
|
42
|
+
data.each(&blk)
|
43
|
+
end
|
44
|
+
|
45
|
+
def retrieve(id, opts = {})
|
46
|
+
id, retrieve_params = Util.normalize_id(id)
|
47
|
+
resp, opts = request(:get, "#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts)
|
48
|
+
Util.convert_to_zaius_object(resp.data, opts)
|
49
|
+
end
|
50
|
+
|
51
|
+
def resource_url
|
52
|
+
url ||
|
53
|
+
raise(ArgumentError, "List object does not contain a 'url' field.")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Zaius
|
2
|
+
class Subscription < APIResource
|
3
|
+
OBJECT_NAME = "subscription".freeze
|
4
|
+
|
5
|
+
def self.resource_url
|
6
|
+
"/lists/subscriptions"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.list(params = {}, opts = {})
|
10
|
+
resp, opts = request(:get, resource_url, params, opts)
|
11
|
+
|
12
|
+
ZaiusObject.construct_from(resp.data, opts)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.update_opt_in(params = {}, opts = {})
|
16
|
+
resp, opts = request(:post, resource_url, params, opts)
|
17
|
+
|
18
|
+
ZaiusObject.construct_from(resp.data, opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.update(email: nil, list_id: nil, subscribed: false)
|
22
|
+
params = { email: email, list_id: list_id, subscribed: subscribed }
|
23
|
+
resp, opts = request(:post, resource_url, params)
|
24
|
+
|
25
|
+
ZaiusObject.construct_from(resp.data, opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.update_list(opted_in: true, email:, lists: [])
|
29
|
+
body = lists.map do |list|
|
30
|
+
[
|
31
|
+
{ opted_in: opted_in, email: email },
|
32
|
+
{ list_id: list[:id], email: email, subscribed: list[:subscribed] }
|
33
|
+
]
|
34
|
+
end.flatten
|
35
|
+
resp, opts = request(:post, resource_url, body)
|
36
|
+
|
37
|
+
ZaiusObject.construct_from(resp.data, opts)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.opt_out(params = {}, opts = {})
|
41
|
+
params[:opted_in] = false
|
42
|
+
resp, opts = request(:post, resource_url, params, opts)
|
43
|
+
|
44
|
+
ZaiusObject.construct_from(resp.data, opts)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.opt_in(params = {}, opts = {})
|
48
|
+
params[:opted_in] = true
|
49
|
+
|
50
|
+
resp, opts = request(:post, resource_url, params, opts)
|
51
|
+
|
52
|
+
ZaiusObject.construct_from(resp.data, opts)
|
53
|
+
end
|
54
|
+
|
55
|
+
def resource_url
|
56
|
+
self.class.resource_url
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|