stannp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: df463198452eace393468c01970f5a2d5f0aca063bfebd9e98a059b006525e66
4
+ data.tar.gz: c0e810b647919ca53513ccb0d35010aeb5afeed348a2c2325d2cdde4ed658fd3
5
+ SHA512:
6
+ metadata.gz: f84d3718a853fda778d709769141be754c250f99ee65de980307a98fbf1133b7b68f2d9e8b568007aaf9a4c88684189f38f9714002b6d50ea4c6ba58fc40c1ca
7
+ data.tar.gz: 7a5a4e153c5822dcac9888b98bf7560ea7c76895f88fe0df5022596ff4edbe1667a5d09413916370f138b1e80f84bbb53a4944c973248a0fea6ea6f54cb7299f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ Exclude:
4
+ - 'spec/**/*'
5
+ - '*.gemspec'
6
+ - 'vendor/**/*'
7
+ - 'vendor/**/.*'
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: single_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Layout/LineLength:
21
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-04-09
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in stannp.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 k-p-jones
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
+ # Stannp
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/stannp`. 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 'stannp'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install stannp
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 spec` 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 the created tag, 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/[USERNAME]/stannp.
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
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'stannp'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ module Stannp
7
+ class Client
8
+ attr_reader :api_key, :adapter
9
+
10
+ def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
11
+ @api_key = api_key
12
+ @adapter = adapter
13
+ @stubs = stubs
14
+ end
15
+
16
+ def account
17
+ AccountResource.new(client: self)
18
+ end
19
+
20
+ def user
21
+ UserResource.new(client: self)
22
+ end
23
+
24
+ def recipients
25
+ RecipientsResource.new(client: self)
26
+ end
27
+
28
+ def connection
29
+ @connection ||= Faraday.new do |conn|
30
+ conn.request :json
31
+ conn.response :json, content_type: 'application/json'
32
+ conn.adapter adapter, stubs
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :stubs
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class List
5
+ attr_reader :data
6
+
7
+ def self.from_request(data:, type:)
8
+ new(data: data.map { |attrs| type.new(attrs) })
9
+ end
10
+
11
+ def initialize(data:)
12
+ @data = data
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+
5
+ module Stannp
6
+ class Object
7
+ attr_reader :attributes
8
+
9
+ def initialize(attributes)
10
+ @attributes = OpenStruct.new(attributes)
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ value = attributes.send(method, *args, &block)
15
+ case value
16
+ when Hash
17
+ Object.new(value)
18
+ when Array
19
+ value.map { |item| item.is_a?(Hash) ? Object.new(item) : item }
20
+ else
21
+ value
22
+ end
23
+ end
24
+
25
+ def respond_to_missing?(method, include_private = false)
26
+ attributes.to_h.keys.include?(method) || super
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class Recipient < Object; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class User < Object; end
5
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class Resource
5
+ def initialize(client:)
6
+ @client = client
7
+ end
8
+
9
+ def get_request(url, params: {}, headers: {})
10
+ handle_response(client.connection.get(url, params, headers))
11
+ end
12
+
13
+ def post_request(url, body:, headers: {})
14
+ handle_response client.connection.post(url, body, headers)
15
+ end
16
+
17
+ def patch_request(url, body:, headers: {})
18
+ handle_response client.connection.patch(url, body, headers)
19
+ end
20
+
21
+ def put_request(url, body:, headers: {})
22
+ handle_response client.connection.put(url, body, headers)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :client
28
+
29
+ def handle_response(response)
30
+ raise Error, "#{response.status}: #{response.body["error"]}" unless response.success?
31
+
32
+ response
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class AccountResource < Resource
5
+ def balance
6
+ url = "https://dash.stannp.com/api/v1/accounts/balance?api_key=#{client.api_key}}"
7
+ get_request(url).body['data']['balance']
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class RecipientsResource < Resource
5
+ def list(group_id: nil)
6
+ id = group_id ? "/#{group_id}" : ''
7
+ url = "https://dash.stannp.com/api/v1/recipients/list#{id}?api_key=#{client.api_key}"
8
+ List.from_request(data: get_request(url).body['data'], type: Recipient)
9
+ end
10
+
11
+ def get(id:)
12
+ url = "https://dash.stannp.com/api/v1/recipients/get/#{id}?api_key=#{client.api_key}"
13
+ Recipient.new(get_request(url).body['data'])
14
+ end
15
+
16
+ def create(attributes:)
17
+ url = "https://dash.stannp.com/api/v1/recipients/new?api_key=#{client.api_key}"
18
+ Recipient.new(post_request(url, body: attributes).body['data'])
19
+ end
20
+
21
+ def delete(id:)
22
+ url = "https://dash.stannp.com/api/v1/recipients/delete?api_key=#{client.api_key}"
23
+ post_request(url, body: { id: id.to_i })
24
+ true
25
+ end
26
+
27
+ def delete_all
28
+ url = "https://dash.stannp.com/api/v1/recipients/deleteAll?api_key=#{client.api_key}"
29
+ post_request(url, body: { delete_all: true })
30
+ true
31
+ rescue Stannp::Error => e
32
+ # Despite the request actually deleteing all recipients, the API returns a 400. In those
33
+ # cases, and when delete all is called on an empty recipient list, just return true.
34
+ return true if e.message == '400: Nothing to delete'
35
+
36
+ raise e
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ class UserResource < Resource
5
+ def get
6
+ url = "https://dash.stannp.com/api/v1/users/me?api_key=#{client.api_key}"
7
+ User.new(get_request(url).body['data'])
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stannp
4
+ VERSION = '0.1.0'
5
+ end
data/lib/stannp.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'stannp/version'
4
+
5
+ module Stannp
6
+ autoload :Error, 'stannp/error'
7
+ autoload :Client, 'stannp/client'
8
+ autoload :Resource, 'stannp/resource'
9
+ autoload :Object, 'stannp/object'
10
+ autoload :List, 'stannp/list'
11
+ # resources
12
+ autoload :AccountResource, 'stannp/resources/account'
13
+ autoload :UserResource, 'stannp/resources/user'
14
+ autoload :RecipientsResource, 'stannp/resources/recipients'
15
+ # objects
16
+ autoload :User, 'stannp/objects/user'
17
+ autoload :Recipient, 'stannp/objects/recipient'
18
+ end
data/sig/stannp.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Stannp
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stannp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - k-p-jones
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".rspec"
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/stannp.rb
57
+ - lib/stannp/client.rb
58
+ - lib/stannp/error.rb
59
+ - lib/stannp/list.rb
60
+ - lib/stannp/object.rb
61
+ - lib/stannp/objects/recipient.rb
62
+ - lib/stannp/objects/user.rb
63
+ - lib/stannp/resource.rb
64
+ - lib/stannp/resources/account.rb
65
+ - lib/stannp/resources/recipients.rb
66
+ - lib/stannp/resources/user.rb
67
+ - lib/stannp/version.rb
68
+ - sig/stannp.rbs
69
+ homepage:
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.2.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A ruby wrapper for the Stannp direct mail API
92
+ test_files: []