seeuletter 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +82 -0
- data/Rakefile +7 -0
- data/examples/README.md +17 -0
- data/examples/letters.rb +49 -0
- data/lib/seeuletter/client.rb +21 -0
- data/lib/seeuletter/errors/invalid_request_error.rb +4 -0
- data/lib/seeuletter/errors/seeuletter_error.rb +20 -0
- data/lib/seeuletter/resources/letter.rb +14 -0
- data/lib/seeuletter/resources/resource_base.rb +109 -0
- data/lib/seeuletter/version.rb +3 -0
- data/lib/seeuletter.rb +7 -0
- data/seeuletter.gemspec +24 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1408fe288ce4a9b2f41d176873e32c47a2717dcf
|
4
|
+
data.tar.gz: a958dad52b535b938c0d60183969cf99cf6bf148
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 678d19d0e7976d2b154d15f5ff6487d57d8b2da53ec308e91ad8ac9ea904a91e388bb695936d4ce01621f130a0c37937f8ca185616e073427e07457a445b369c
|
7
|
+
data.tar.gz: a5dcf091b2165b1cac85fa2a0742c81e69ffde43bce15862826c4df69ad77a22e3f4ebf7e4437f6dfc5195368bf4af6d18187b0265eb58a6d22b7d54e59c827f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 Seeuletter.com
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# seeuletter-ruby
|
2
|
+
|
3
|
+
Ruby wrapper for the [Seeuletter.com](https://www.seeuletter.com) API. See the full Seeuletter.com [API Documentation](https://docs.seeuletter.com/?ruby#). For best results, be sure that you're using the latest version of the Seeuletter API and the latest version of the ruby wrapper. This gem gives you an ActiveRecord-style syntax to use the Seeuletter.com API.
|
4
|
+
|
5
|
+
Supports Ruby 2.0.0 and greater.
|
6
|
+
|
7
|
+
## Table of Contents
|
8
|
+
|
9
|
+
- [Getting Started](#getting-started)
|
10
|
+
- [Registration](#registration)
|
11
|
+
- [Installation](#installation)
|
12
|
+
- [Usage](#usage)
|
13
|
+
- [Examples](#examples)
|
14
|
+
|
15
|
+
## Getting Started
|
16
|
+
|
17
|
+
Here's a general overview of the Seeuletter services available, click through to read more.
|
18
|
+
|
19
|
+
Please read through the official [API Documentation](#api-documentation) to get a complete sense of what to expect from each endpoint.
|
20
|
+
|
21
|
+
### Registration
|
22
|
+
|
23
|
+
First, you will need to first create an account at [Seeuletter.com](https://www.seeuletter.com/signup) and obtain your Test and Live API Keys.
|
24
|
+
|
25
|
+
Once you have created an account, you can access your API Keys from the [Settings Panel](https://www.seeuletter.com/app/dashboard/keys).
|
26
|
+
|
27
|
+
### Installation
|
28
|
+
|
29
|
+
Add this line to your application's `Gemfile`:
|
30
|
+
|
31
|
+
gem 'seeuletter'
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or manually install it yourself:
|
38
|
+
|
39
|
+
$ gem install seeuletter
|
40
|
+
|
41
|
+
### Usage
|
42
|
+
|
43
|
+
The library uses an ActiveRecord-style interface. You'll feel right at home.
|
44
|
+
|
45
|
+
For optional parameters and other details, refer to the docs [here](https://docs.seeuletter.com/?ruby#).
|
46
|
+
|
47
|
+
#### Initialization and Configuration
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# To initialize a Seeuletter object
|
51
|
+
seeuletter = Seeuletter::Client.new(api_key: "your-api-key")
|
52
|
+
```
|
53
|
+
|
54
|
+
#### Caution: Pass zero-prefixed postal codes as strings
|
55
|
+
|
56
|
+
When using postal codes with zero-prefixes, always quote them. For example when specifying `02125`, pass it as a string `"02125"`, instead of an integer.
|
57
|
+
|
58
|
+
The Ruby interpreter assumes it's not of base-10 and tries to convert it to base-10 number. So that might result in an entirely different postal code than intended.
|
59
|
+
|
60
|
+
#### Accessing Response Headers
|
61
|
+
|
62
|
+
You can access response headers via a hidden `headers` method on the response hash.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
addresses = seeuletter.addresses.list
|
66
|
+
|
67
|
+
addresses._response.headers[:content_type]
|
68
|
+
# => "application/json; charset=utf-8"
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
## Examples
|
73
|
+
|
74
|
+
We've provided various examples for you to try out [here](https://github.com/seeuletter/seeuletter-ruby/tree/master/examples).
|
75
|
+
|
76
|
+
There are simple scripts to demonstrate how to create all letters with the Seeuletter API Ruby wrapper.
|
77
|
+
|
78
|
+
=======================
|
79
|
+
|
80
|
+
Copyright © 2017 Seeuletter.com
|
81
|
+
|
82
|
+
Released under the MIT License, which can be found in the repository in `LICENSE.txt`.
|
data/Rakefile
ADDED
data/examples/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Ruby Examples
|
2
|
+
|
3
|
+
Here you can find some examples to help you get started with the Ruby wrapper.
|
4
|
+
|
5
|
+
## Getting started
|
6
|
+
Before running these examples make sure you are in the `examples/` directory.
|
7
|
+
```
|
8
|
+
cd examples/
|
9
|
+
```
|
10
|
+
|
11
|
+
## Examples
|
12
|
+
|
13
|
+
|
14
|
+
### Create a letter
|
15
|
+
```
|
16
|
+
ruby letters.rb
|
17
|
+
```
|
data/examples/letters.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))
|
2
|
+
require 'seeuletter'
|
3
|
+
|
4
|
+
# initialize Seeuletter object
|
5
|
+
seeuletter = Seeuletter::Client.new(api_key: '<PUT_YOU_API_KEY_HERE>')
|
6
|
+
|
7
|
+
html = %{
|
8
|
+
<html>
|
9
|
+
<head>
|
10
|
+
<style>
|
11
|
+
*, *:before, *:after {
|
12
|
+
-webkit-box-sizing: border-box;
|
13
|
+
-moz-box-sizing: border-box;
|
14
|
+
box-sizing: border-box;
|
15
|
+
}
|
16
|
+
.text {
|
17
|
+
margin-left: 50px;
|
18
|
+
padding-top: 450px;
|
19
|
+
font-size: 50px;
|
20
|
+
font-weight: 700;
|
21
|
+
}
|
22
|
+
</style>
|
23
|
+
</head>
|
24
|
+
|
25
|
+
<body>
|
26
|
+
<p class="text">Hello {{name}}!</p>
|
27
|
+
</body>
|
28
|
+
|
29
|
+
</html>
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
# send the letter
|
34
|
+
puts seeuletter.letters.create(
|
35
|
+
description: "Test letter from the Ruby Wrapper",
|
36
|
+
to: {
|
37
|
+
name: 'Erlich',
|
38
|
+
address_line1: '30 rue de rivoli',
|
39
|
+
address_line2: '',
|
40
|
+
address_city: 'Paris',
|
41
|
+
address_country: 'France',
|
42
|
+
address_postalcode: '75004'
|
43
|
+
},
|
44
|
+
source_file: html,
|
45
|
+
source_file_type: 'html',
|
46
|
+
postage_type: 'prioritaire',
|
47
|
+
variables: { name: 'Erlich'},
|
48
|
+
color: 'color'
|
49
|
+
)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "seeuletter/resources/letter"
|
2
|
+
|
3
|
+
module Seeuletter
|
4
|
+
class Client
|
5
|
+
|
6
|
+
attr_reader :config
|
7
|
+
|
8
|
+
def initialize(config = nil)
|
9
|
+
if config.nil? || config[:api_key].nil?
|
10
|
+
raise ArgumentError.new(":api_key is a required argument to initialize Seeuletter")
|
11
|
+
end
|
12
|
+
|
13
|
+
@config = config
|
14
|
+
end
|
15
|
+
|
16
|
+
def letters
|
17
|
+
Seeuletter::Resources::Letter.new(config)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Seeuletter
|
2
|
+
|
3
|
+
class SeeuletterError < StandardError
|
4
|
+
attr_reader :http_status, :http_body, :json_body
|
5
|
+
|
6
|
+
def initialize(message, http_status=nil, http_body=nil, json_body=nil)
|
7
|
+
@http_status = http_status
|
8
|
+
@http_body = http_body
|
9
|
+
@json_body = json_body
|
10
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
11
|
+
super("#{status_string}#{message} \n #{@http_body}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def _response
|
15
|
+
@json_body
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require "json"
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
module Seeuletter
|
6
|
+
module Resources
|
7
|
+
class ResourceBase
|
8
|
+
|
9
|
+
attr_reader :config, :endpoint
|
10
|
+
|
11
|
+
def initialize(config)
|
12
|
+
@config = config
|
13
|
+
end
|
14
|
+
|
15
|
+
def list(options={})
|
16
|
+
submit :get, endpoint_url, options
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(resource_id)
|
20
|
+
submit :get, resource_url(resource_id)
|
21
|
+
end
|
22
|
+
|
23
|
+
def create(options={}, headers={})
|
24
|
+
submit :post, endpoint_url, options, headers
|
25
|
+
end
|
26
|
+
|
27
|
+
def destroy(resource_id)
|
28
|
+
submit :delete, resource_url(resource_id)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def submit(method, url, parameters={}, headers={})
|
34
|
+
clientVersion = Seeuletter::VERSION
|
35
|
+
|
36
|
+
begin
|
37
|
+
if method == :get || method == :delete
|
38
|
+
# Hack to URL encode nested objects like metadata.
|
39
|
+
url = "#{url}?#{build_nested_query(parameters)}"
|
40
|
+
response = RestClient.send(method, url, {
|
41
|
+
user_agent: 'Seeuletter/v1 RubyBindings/' + clientVersion,
|
42
|
+
"Seeuletter-Version" => config[:api_version]
|
43
|
+
})
|
44
|
+
else
|
45
|
+
headers = headers.merge({
|
46
|
+
user_agent: 'Seeuletter/v1 RubyBindings/' + clientVersion,
|
47
|
+
"Seeuletter-Version" => config[:api_version]
|
48
|
+
})
|
49
|
+
response = RestClient.send(method, url, parameters, headers)
|
50
|
+
end
|
51
|
+
|
52
|
+
body = JSON.parse(response)
|
53
|
+
|
54
|
+
body.define_singleton_method(:_response) do
|
55
|
+
response
|
56
|
+
end
|
57
|
+
|
58
|
+
return body
|
59
|
+
|
60
|
+
rescue RestClient::ExceptionWithResponse => e
|
61
|
+
handle_api_error(e)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def handle_api_error(error)
|
68
|
+
begin
|
69
|
+
response = JSON.parse(error.http_body.to_s)
|
70
|
+
message = response.fetch("error").fetch("message")
|
71
|
+
raise InvalidRequestError.new(message, error.http_code, error.http_body, error.response)
|
72
|
+
rescue JSON::ParserError, KeyError
|
73
|
+
# :nocov:
|
74
|
+
raise SeeuletterError.new("Invalid response object:", error.http_code, error.http_body)
|
75
|
+
# :nocov:
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def build_nested_query(value, prefix = nil)
|
80
|
+
case value
|
81
|
+
when Array
|
82
|
+
value.map { |v|
|
83
|
+
build_nested_query(v, "#{prefix}[]")
|
84
|
+
}.join("&")
|
85
|
+
when Hash
|
86
|
+
value.map { |k, v|
|
87
|
+
build_nested_query(v, prefix ? "#{prefix}[#{URI.encode_www_form_component(k)}]" : URI.encode_www_form_component(k))
|
88
|
+
}.reject(&:empty?).join('&')
|
89
|
+
else
|
90
|
+
raise ArgumentError, "value must be an Array or Hash" if prefix.nil?
|
91
|
+
"#{prefix}=#{URI.encode_www_form_component(value)}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def base_url
|
96
|
+
"https://#{config[:api_key]}:@api.seeuletter.com"
|
97
|
+
end
|
98
|
+
|
99
|
+
def endpoint_url
|
100
|
+
"#{base_url}/#{endpoint}"
|
101
|
+
end
|
102
|
+
|
103
|
+
def resource_url(resource_id)
|
104
|
+
"#{endpoint_url}/#{resource_id}"
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/lib/seeuletter.rb
ADDED
data/seeuletter.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
3
|
+
|
4
|
+
require "seeuletter/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "seeuletter"
|
8
|
+
spec.version = Seeuletter::VERSION
|
9
|
+
spec.authors = ["Seeuletter"]
|
10
|
+
spec.email = ["hello@seeuletter.com"]
|
11
|
+
spec.description = %q{Seeuletter API Ruby wrapper}
|
12
|
+
spec.summary = %q{Ruby wrapper for Seeuletter.com API - ActiveRecord-style syntax}
|
13
|
+
spec.homepage = "https://github.com/seeuletter/seeuletter-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rest-client", ">= 1.8", "< 3.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.4.2"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seeuletter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Seeuletter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.8'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.3'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.3'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 10.4.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 10.4.2
|
61
|
+
description: Seeuletter API Ruby wrapper
|
62
|
+
email:
|
63
|
+
- hello@seeuletter.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- examples/README.md
|
74
|
+
- examples/letters.rb
|
75
|
+
- lib/seeuletter.rb
|
76
|
+
- lib/seeuletter/client.rb
|
77
|
+
- lib/seeuletter/errors/invalid_request_error.rb
|
78
|
+
- lib/seeuletter/errors/seeuletter_error.rb
|
79
|
+
- lib/seeuletter/resources/letter.rb
|
80
|
+
- lib/seeuletter/resources/resource_base.rb
|
81
|
+
- lib/seeuletter/version.rb
|
82
|
+
- seeuletter.gemspec
|
83
|
+
homepage: https://github.com/seeuletter/seeuletter-ruby
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.6.11
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Ruby wrapper for Seeuletter.com API - ActiveRecord-style syntax
|
107
|
+
test_files: []
|