ontraport 0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c912fbafd548695f15360b86740e87ac0eea4ed8
4
+ data.tar.gz: d59e37833ca83b3453c90450f47b82b705cb847b
5
+ SHA512:
6
+ metadata.gz: 5b7e0732bed00535e41aaccf029b59d7ff51cd6783398c8a8a49fa0e878fdbe19a360004ad8e4ea635c0fe924e1ebff6657fc7be9026c2e8be8273812eabd2fe
7
+ data.tar.gz: 3495263a634880b137b52b87dfc678bd12b43662cf18aa5e1cffed3f81925690dc7c851105dfcbab8a72916324ccfd41ce1e6fd91ce7040cb3b4649160e7e3bd
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # 0.1 - February 6, 2016
2
+
3
+ - Initial release
4
+ - Implements all "objects" API methods from ONTRAPORT
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Hamza Tayeb
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Ruby API client for ONTRAPORT
2
+
3
+ [![GitHub](https://img.shields.io/badge/github-ontraport--ruby-blue.svg)](http://github.com/hamzatayeb/ontraport-ruby)
4
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://rubydoc.info/github/hamzatayeb/ontraport-ruby/)
5
+ [![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg)](#license)
6
+ <!--
7
+ [![Build Status](https://travis-ci.org/hamzatayeb/ontraport-ruby.svg?branch=master)](https://travis-ci.org/hamzatayeb/ontraport-ruby)
8
+ -->
9
+
10
+ Disclaimer - This Gem is a work in progress.
11
+
12
+ <!-- Installation
13
+ ------------
14
+
15
+ ### Bundler
16
+
17
+ Add the Ontraport gem to your Gemfile:
18
+
19
+ ```ruby
20
+ gem 'ontraport', '~> 1.0.0'
21
+ ```
22
+
23
+ ### Manual
24
+
25
+ Install the Gem from your terminal -
26
+
27
+ ```bash
28
+ gem install ontraport
29
+ ```
30
+ -->
31
+
32
+ Configuration
33
+ -------------
34
+
35
+ In Rails, use an initializer to configure -
36
+
37
+ ```ruby
38
+ # config/initializers/ontraport.rb
39
+ require 'ontraport'
40
+
41
+ Ontraport.configure do |config|
42
+ config.api_id = 'foo'
43
+ config.api_key = 'bar'
44
+ end
45
+ ```
46
+
47
+ Contact
48
+ -------
49
+
50
+ If you have a question or a bug report, feel free to -
51
+
52
+ * [file an issue][issues]
53
+ * [send me an email](mailto:hamza.tayeb@gmail.com)
54
+
55
+ License
56
+ -------
57
+
58
+ The project uses the MIT License. See LICENSE.md for details.
59
+
60
+ [issues]: https://github.com/hamzatayeb/ontraport-ruby/issues
61
+ [semver]: http://semver.org
62
+ [rubydoc]: http://www.rubydoc.info/github/hamzatayeb/ontraport-ruby/
data/lib/ontraport.rb ADDED
@@ -0,0 +1,171 @@
1
+ Dir[File.expand_path('../ontraport/*.rb', __FILE__)].each do |file|
2
+ require file
3
+ end
4
+
5
+ # @author Hamza Tayeb
6
+ # @see https://api.ontraport.com/doc ONTRAPORT API Documentation
7
+ module Ontraport
8
+ BASE_URL = 'https://api.ontraport.com/'
9
+ API_VERSION = '1'
10
+
11
+ # Describe a given object type, including the numeric object Id and the available fields.
12
+ #
13
+ # @example
14
+ # Ontraport.describe :contact
15
+ #
16
+ # @param object [Symbol] the type of object you want to describe
17
+ # @raise [ArgumentError] if the argument is not a +Symbol+
18
+ # @raise [ObjectNotFoundError] if the argument does not match any object defined in the schema
19
+ # @return [Hash{String=>String,Hash}] object metadata. Keys are:
20
+ # - +'name'+ - name of the object
21
+ # - +'fields'+ - Hash containing the object's fields
22
+ # - +'schema_object_id'+ - numeric Id of the object, as a String
23
+ def self.describe object
24
+ unless object.class.eql? Symbol
25
+ raise ArgumentError.new "Must provide a symbol for the object name."
26
+ end
27
+
28
+ unless metadata = objects_meta.data.find{ |k, v| v['name'].downcase.to_sym.eql? object }
29
+ raise ObjectNotFoundError.new "No object matching #{object.inspect} could be found."
30
+ end
31
+
32
+ metadata.second.update 'schema_object_id' => metadata.first
33
+ end
34
+
35
+ # Clear the cache of object metadata generated by the describe call. Use this
36
+ # if you make changes to custom fields or objects in your ONTRAPORT instance and
37
+ # don't want to reload the Ontraport module.
38
+ #
39
+ # @example
40
+ # Ontraport.clear_describe_cache!
41
+ #
42
+ # @return [nil]
43
+ def self.clear_describe_cache!
44
+ @objects_meta_cache = nil
45
+ end
46
+
47
+ # @!group Accessor methods
48
+
49
+ # Retrieve a single object of the specified type.
50
+ #
51
+ # @example
52
+ # Ontraport.get_object :contact, 12345
53
+ # #=> #<Ontraport::Response @data=...>
54
+ #
55
+ # @param object_type [Symbol] the type of object, for instance +:contact+
56
+ # @param id [Integer] Id of the record you want
57
+ # @return [Response]
58
+ def self.get_object object_type, id
59
+ objects_call :get, object_type, endpoint: '/object', data: { id: id }
60
+ end
61
+
62
+ # Retrieve a collection of objects of the specified type, matching the query
63
+ # specified by the parameters.
64
+ #
65
+ # @example
66
+ # Ontraport.get_objects :contact, { condition: "email like '%@foo.com'", sort: 'lastname' }
67
+ #
68
+ # @see https://api.ontraport.com/doc/#!/objects/getObjects List of accepted params
69
+ #
70
+ # @param object_type [Symbol] the type of object
71
+ # @param params [Hash] hash containing request parameters
72
+ # @return [Response]
73
+ def self.get_objects object_type, params={}
74
+ objects_call :get, object_type, endpoint: '/objects', data: params
75
+ end
76
+
77
+ # @!endgroup
78
+ # @!group Modifier methods
79
+
80
+ # Create an object with the given data, or merge if the unique field matches another row.
81
+ #
82
+ # @example
83
+ # Ontraport.save_or_update :contact, { email: 'foo@bar.com', firstname: 'Foo' }
84
+ # #=> #<Ontraport::Response @data=...>
85
+ #
86
+ # @see https://api.ontraport.com/doc/#!/objects/createormergeObject API docs
87
+ #
88
+ # @param object_type [Symbol] the type of object
89
+ # @param params [Hash] input data
90
+ # @return [Response]
91
+ def self.save_or_update object_type, params
92
+ objects_call :post, object_type, endpoint: '/objects/saveorupdate', data: params
93
+ end
94
+
95
+ # Given an object type, Id, and a +Hash+ of changes, update an single row.
96
+ #
97
+ # @example
98
+ # Ontraport.update_object :contact, 12345, { firstname: 'ChangeMe' }
99
+ # #=> #<Ontraport::Response @data=...>
100
+ #
101
+ # @param object_type [Symbol] the type of object
102
+ # @param params [Hash] update data. Use +.describe+ to get a list of available fields.
103
+ # @return [Response]
104
+ def self.update_object object_type, id, params
105
+ objects_call :put, object_type, endpoint: '/objects', data: params.update(id: id)
106
+ end
107
+
108
+ # Add tags to objects matching the supplied conditions. In addition to various conditionals, you
109
+ # may supply a list of Ids for objects you wish to tag (+ids+). The +add_list+ parameter (which contains the
110
+ # Ids of the tags you want to add) is required.
111
+ #
112
+ # @note The +add_list+ and +ids+ parameters should be strings comprised of comma-delimeted Integers.
113
+ #
114
+ # @example
115
+ # Ontraport.tag_objects :contact, { add_list: '11111,22222', ids: '33333,44444' }
116
+ # #=> #<Ontraport::Response @data=...>
117
+ #
118
+ # @see https://api.ontraport.com/doc/#!/objects/addTag API docs
119
+ #
120
+ # @param object_type [Symbol] the type of object
121
+ # @param params [Hash] parameters describing the conditions of the tag operation.
122
+ # @return [Response]
123
+ def self.tag_objects object_type, params
124
+
125
+ objects_call :put, object_type, endpoint: '/objects/tag', data: params
126
+ end
127
+
128
+ # Remove tags from objects matching the supplied conditions. Interface is identical to +#tag_objects+
129
+ #
130
+ # @see tag_objects
131
+ # @see https://api.ontraport.com/doc/#!/objects/removeTag API docs
132
+ #
133
+ # @param object_type [Symbol] the type of object
134
+ # @param params [Hash] parameters describing the conditions of the tag operation.
135
+ # @return [Response]
136
+ def self.untag_objects object_type, object:, tag:
137
+ objects_call :delete, object_type, endpoint: '/objects/tag', data: params
138
+ end
139
+
140
+ # @!endgroup
141
+
142
+ private
143
+ def self.request_with_authentication method, endpoint:, data: nil
144
+ data_param = method.eql?(:get) ? :query : :body
145
+
146
+ args = [method, "#{BASE_URL}#{API_VERSION}#{endpoint}"]
147
+ kwargs = {
148
+ :headers => { 'Api-Appid' => @configuration.api_id, 'Api-Key' => @configuration.api_key },
149
+ data_param => data
150
+ }
151
+
152
+ response = HTTParty.send *args, **kwargs
153
+
154
+ unless response.code.eql? 200
155
+ raise APIError.new response.body
156
+ end
157
+
158
+ Response.new **response.parsed_response.symbolize_keys
159
+ end
160
+
161
+ def self.objects_call method, object_type, endpoint:, data: {}
162
+ metadata = describe object_type
163
+ data.update 'objectID' => metadata['schema_object_id']
164
+
165
+ request_with_authentication method, endpoint: endpoint, data: data
166
+ end
167
+
168
+ def self.objects_meta
169
+ @objects_meta_cache ||= request_with_authentication :get, endpoint: '/objects/meta'
170
+ end
171
+ end
@@ -0,0 +1,28 @@
1
+ module Ontraport
2
+ # @!group Configuration methods
3
+
4
+ # Getter method for the module config.
5
+ # @return [Configuration]
6
+ def self.configuration
7
+ @configuration ||= Configuration.new
8
+ end
9
+
10
+ # Used to enable a configuration block in the application the Gem being used by.
11
+ #
12
+ # @example
13
+ # Ontraport.configure do |config|
14
+ # config.api_id = 'foo'
15
+ # config.api_key = 'bar'
16
+ # end
17
+ #
18
+ # @return [nil]
19
+ def self.configure
20
+ yield configuration
21
+ end
22
+ # @!endgroup
23
+
24
+ # The Gem's configuration
25
+ class Configuration
26
+ attr_accessor :api_id, :api_key
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ module Ontraport
2
+ # Used when the API itself returns with an HTTP status code other than +200+
3
+ class APIError < StandardError; end
4
+
5
+ # Used by the describe call when attempting to look up an object type
6
+ # that cannot be found in the object metadata
7
+ class ObjectNotFoundError < StandardError; end
8
+ end
@@ -0,0 +1,69 @@
1
+ module Ontraport
2
+ class Response
3
+ # @!attribute [r] code
4
+ # Response code
5
+ #
6
+ # @return [String]
7
+ #
8
+ # @!attribute [r] data
9
+ # A collection containing actual object data returned from the API.
10
+ #
11
+ # @return [Array<Hash{String=>String,NilClass}>, Hash{String=>String,NilClass}] either a
12
+ # Hash containing keys for each field belonging to the object type, or an array of those
13
+ #
14
+ # @!attribute [r] updates
15
+ # @return [Array]
16
+ #
17
+ # @!attribute [r] notifications
18
+ # @return [Array]
19
+ #
20
+ # @!attribute [r] account_id
21
+ # Account Id
22
+ #
23
+ # @return [String] the Id of your ONTRAPORT account
24
+ #
25
+ # @!attribute [r] misc
26
+ #
27
+ # @!attribute [r] name
28
+ # Name of an object in the schema.
29
+ #
30
+ # @return [String] name of the object type, used by the +/objects/meta+ call
31
+ #
32
+ # @!attribute [r] fields
33
+ # Describes the field metadata for an object.
34
+ #
35
+ # @return [Hash{String=>Hash}] a collection where keys correspond to field names, and
36
+ # values are a nested +Hash+ containing the following:
37
+ # - +'alias'+ - Human-readable name of the field
38
+ # - +'type'+ - type of field. May be one of the following:
39
+ # - +check+
40
+ # - +country+
41
+ # - +fulldate+
42
+ # - +list+
43
+ # - +longtext+
44
+ # - +numeric+
45
+ # - +price+
46
+ # - +phone+
47
+ # - +state+
48
+ # - +drop+
49
+ # - +text+
50
+ # - +timestamp+
51
+ # - +parent+
52
+ # - +subscription+
53
+ # - +email+
54
+ # - +sms+
55
+ # - +'options'+ - either an Array or a Hash of options. This is +nil+ unless +type+ is +list+ or +drop+
56
+ #
57
+ # @!attribute [r] schema_object_id
58
+ # Numeric object identifier for a particular object in the schema.
59
+ #
60
+ # @return [String]
61
+ attr_reader :code, :data, :updates, :notifications, :account_id, :misc, :name, :fields, :schema_object_id
62
+
63
+ def initialize args
64
+ args.each do |key,val|
65
+ instance_variable_set("@#{key}", val) unless val.nil?
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Ontraport
2
+ VERSION = '0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ontraport
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Hamza Tayeb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ description: |2
28
+ This Gem implements the ONTRAPORT JSON API. Basic data retrieval and
29
+ manipulation operations are supported.
30
+
31
+ Full details on the API can be found at https://api.ontraport.com/doc/
32
+ email: hamza.tayeb@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - README.md
37
+ - LICENSE.md
38
+ - CHANGELOG.md
39
+ files:
40
+ - CHANGELOG.md
41
+ - LICENSE.md
42
+ - README.md
43
+ - lib/ontraport.rb
44
+ - lib/ontraport/configuration.rb
45
+ - lib/ontraport/exceptions.rb
46
+ - lib/ontraport/response.rb
47
+ - lib/ontraport/version.rb
48
+ homepage: https://github.com/hamzatayeb/ontraport-ruby
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 2.3.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: API client for ONTRAPORT - http://ontraport.com
72
+ test_files: []
73
+ has_rdoc: