contactually-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +80 -0
  4. data/Rakefile +7 -0
  5. data/lib/contactually-ruby.rb +38 -0
  6. data/lib/contactually/accounts.rb +22 -0
  7. data/lib/contactually/api.rb +87 -0
  8. data/lib/contactually/buckets.rb +49 -0
  9. data/lib/contactually/contact_groupings.rb +16 -0
  10. data/lib/contactually/contacts.rb +54 -0
  11. data/lib/contactually/contents.rb +30 -0
  12. data/lib/contactually/errors.rb +21 -0
  13. data/lib/contactually/groupings.rb +36 -0
  14. data/lib/contactually/middleware/error_detector.rb +31 -0
  15. data/lib/contactually/notes.rb +31 -0
  16. data/lib/contactually/representer/account_representer.rb +15 -0
  17. data/lib/contactually/representer/bucket_representer.rb +16 -0
  18. data/lib/contactually/representer/contact_representer.rb +48 -0
  19. data/lib/contactually/representer/content_representer.rb +18 -0
  20. data/lib/contactually/representer/grouping_representer.rb +30 -0
  21. data/lib/contactually/representer/grouping_statistic_representer.rb +14 -0
  22. data/lib/contactually/representer/note_representer.rb +15 -0
  23. data/lib/contactually/representer/task_representer.rb +21 -0
  24. data/lib/contactually/tasks.rb +49 -0
  25. data/lib/contactually/utils.rb +44 -0
  26. data/lib/contactually/version.rb +3 -0
  27. data/spec/accounts_spec.rb +58 -0
  28. data/spec/api_spec.rb +125 -0
  29. data/spec/buckets_spec.rb +97 -0
  30. data/spec/contact_groupings_spec.rb +44 -0
  31. data/spec/contacts_spec.rb +133 -0
  32. data/spec/contents_spec.rb +80 -0
  33. data/spec/error_detector_spec.rb +24 -0
  34. data/spec/fixtures/account.json +10 -0
  35. data/spec/fixtures/accounts_index.json +19 -0
  36. data/spec/fixtures/bucket.json +8 -0
  37. data/spec/fixtures/buckets_index.json +41 -0
  38. data/spec/fixtures/contact.json +38 -0
  39. data/spec/fixtures/contacts_index.json +172 -0
  40. data/spec/fixtures/content.json +10 -0
  41. data/spec/fixtures/contents_index.json +29 -0
  42. data/spec/fixtures/grouping.json +19 -0
  43. data/spec/fixtures/groupings_index.json +28 -0
  44. data/spec/fixtures/note.json +7 -0
  45. data/spec/fixtures/note_destroy.json +4 -0
  46. data/spec/fixtures/notes_index.json +30 -0
  47. data/spec/fixtures/task.json +27 -0
  48. data/spec/fixtures/tasks_index.json +41 -0
  49. data/spec/groupings_spec.rb +85 -0
  50. data/spec/notes_spec.rb +85 -0
  51. data/spec/spec_helper.rb +14 -0
  52. data/spec/tasks_spec.rb +127 -0
  53. metadata +205 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ad5e5f35bd6313dabb8c0d31f19c7f04148b35c
4
+ data.tar.gz: ebb1775b23927a2ff1949f8fb4ae63b4a48ff01b
5
+ SHA512:
6
+ metadata.gz: 1fa545571fa44178ee7fbe365cf31b3f39f712d12a65e138cbb0042b141dd34e71a2aa48c177e7e1f1279c81bf0c1680bfce7613261656622ae7b80cf0832089
7
+ data.tar.gz: 838af6c311ab5ad8a4a7bf65f6771f2056ff1f1d86a4948848d4899b357e823dae688779657b5893b32eb581131b35762326934a7ca4cac2a982e0e2299765ce
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Revaluate
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.
@@ -0,0 +1,80 @@
1
+ # Contactually-Ruby
2
+
3
+ [![Code Climate](https://codeclimate.com/github/revaluate/contactually-ruby/badges/gpa.svg)](https://codeclimate.com/github/revaluate/contactually-ruby)
4
+ [![Build
5
+ Status](https://travis-ci.org/revaluate/contactually-ruby.svg?branch=master)](https://travis-ci.org/revaluate/contactually-ruby)
6
+
7
+ This is a simple API Wrapper for the Contactually API. It is not feature complete, as it does not support all available API endpoints yet, but should work for most use cases this will be fine. Feel free to contribute!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'contactually-ruby'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install contactually-ruby
22
+
23
+ ## Usage
24
+
25
+ ### Configuration
26
+
27
+ As `contactually-ruby` is shipped with sensible defaults, that will allow you
28
+ to start right away, there are a couple of settings you may want to adjust,
29
+ depending on your applications requirements.
30
+
31
+ The available configuration options are:
32
+
33
+ * access_token (your contactually access_token)
34
+ * contactually_url (Default: "https://api.contactually.com/v2/")
35
+
36
+ Configuration goes as follows:
37
+
38
+ ```ruby
39
+ Contactually.configure do |c|
40
+ c.api_token = "TOKEN FROM OAUTH"
41
+ c.contactually_url = "URL"
42
+ end
43
+ ```
44
+
45
+ #### How to use the gem
46
+
47
+ ```ruby
48
+ contactually = Contactually::API.new
49
+ contacts = contactually.contacts.index
50
+ # => [#<Contactually::Contact id=1234, ...>, #<Contactually::Contact id=1235, ...>, ...]
51
+ notes = contactually.notes.index
52
+ # => [#<Contactually::Note id=2345, ...>, #<Contactually::Note id=2346, ...>, ...]
53
+ groupings = contactually.groupings.index
54
+ # => [#<Contactually::Grouping id=3456, ...>, #<Contactually::Grouping id=3457, ...>, ...]
55
+
56
+ contact = { contact: { first_name: 'Jane', last_name: 'Doe', ... } }
57
+ contactually.contacts.create(contact)
58
+ # => #<Contactually::Contact id=9876, first_name='Jane', last_name='Doe', ...>
59
+ ```
60
+
61
+ Implemented endpoints are:
62
+
63
+ * Accounts
64
+ * Buckets
65
+ * Contact Groupings
66
+ * Contacts
67
+ * Contents
68
+ * Groupings
69
+ * Notes
70
+ * Tasks
71
+
72
+ The API is documented here: [Contactually API Docs](http://developers.contactually.com/docs/)
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ desc 'Run all examples'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
6
+ rescue LoadError
7
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_support/configurable'
2
+ require 'active_support/inflector'
3
+ require 'faraday'
4
+ require 'json'
5
+ require 'roar/decorator'
6
+ require 'roar/representer/json'
7
+
8
+ require 'contactually/version'
9
+ require 'contactually/errors'
10
+ require 'contactually/utils'
11
+
12
+ require 'contactually/middleware/error_detector'
13
+
14
+ require 'contactually/representer/grouping_representer'
15
+ require 'contactually/representer/account_representer'
16
+ require 'contactually/representer/contact_representer'
17
+ require 'contactually/representer/bucket_representer'
18
+ require 'contactually/representer/content_representer'
19
+ require 'contactually/representer/grouping_statistic_representer'
20
+ require 'contactually/representer/note_representer'
21
+ require 'contactually/representer/task_representer'
22
+
23
+ require 'contactually/api'
24
+ require 'contactually/groupings'
25
+ require 'contactually/contacts'
26
+ require 'contactually/buckets'
27
+ require 'contactually/notes'
28
+ require 'contactually/contact_groupings'
29
+ require 'contactually/accounts'
30
+ require 'contactually/contents'
31
+ require 'contactually/tasks'
32
+
33
+ module Contactually
34
+ include ActiveSupport::Configurable
35
+
36
+ config_accessor(:access_token)
37
+ config_accessor(:contactually_url) { "https://api.contactually.com/v2/" }
38
+ end
@@ -0,0 +1,22 @@
1
+ module Contactually
2
+ class Accounts
3
+
4
+ def initialize(master)
5
+ @master = master
6
+ end
7
+
8
+ def index(params = {})
9
+ hash = @master.call('accounts.json', :get, params)
10
+ Contactually::Utils.accounts_hash_to_objects(hash)
11
+ end
12
+
13
+ def show(id, params = {})
14
+ hash = @master.call("accounts/#{id}.json", :get, params)
15
+ Contactually::Utils.build_account(hash)
16
+ end
17
+
18
+ def destroy(id, params = {})
19
+ @master.call("accounts/#{id}.json", :delete, params)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,87 @@
1
+ module Contactually
2
+ class API
3
+ def initialize(access_token = nil)
4
+ raise ConfigMissingApiKeyError, 'You must provide a Contactually access_token' unless Contactually.config.access_token || access_token
5
+ @access_token = Contactually.config.access_token || access_token
6
+ @base_url = Contactually.config.contactually_url
7
+ end
8
+
9
+ def call(url, http_method, params={})
10
+ response = send(http_method, url, params)
11
+ JSON.load(response.body)
12
+ end
13
+
14
+ def contacts
15
+ @contacts ||= Contactually::Contacts.new self
16
+ end
17
+
18
+ def buckets
19
+ @buckets ||= Contactually::Buckets.new self
20
+ end
21
+
22
+ def notes
23
+ @notes ||= Contactually::Notes.new self
24
+ end
25
+
26
+ def tasks
27
+ @tasks ||= Contactually::Tasks.new self
28
+ end
29
+
30
+ def groupings
31
+ @groupings ||= Contactually::Groupings.new self
32
+ end
33
+
34
+ def accounts
35
+ @accounts ||= Contactually::Accounts.new self
36
+ end
37
+
38
+ def contact_groupings
39
+ @contact_groupings ||= Contactually::ContactGroupings.new self
40
+ end
41
+
42
+ def contents
43
+ @contents ||= Contactually::Contents.new self
44
+ end
45
+
46
+ def connection
47
+ @connection ||= Faraday.new do |faraday|
48
+ faraday.headers['Content-Type'] = 'application/json'
49
+ faraday.headers['Authorization'] = "Bearer #{@access_token}"
50
+ faraday.adapter Faraday.default_adapter
51
+ faraday.use Contactually::Middleware::ErrorDetector
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def call_params(params)
58
+ params.merge({ access_token: @access_token })
59
+ end
60
+
61
+ def post(url, params)
62
+ connection.post do |req|
63
+ req.url base_url(url)
64
+ req.body = call_params(params).to_json
65
+ end
66
+ end
67
+
68
+ def put(url, params)
69
+ connection.put do |req|
70
+ req.url base_url(url)
71
+ req.body = call_params(params).to_json
72
+ end
73
+ end
74
+
75
+ def get(url, params)
76
+ connection.get(base_url(url), call_params(params))
77
+ end
78
+
79
+ def delete(url, params)
80
+ connection.delete(base_url(url), call_params(params))
81
+ end
82
+
83
+ def base_url(url)
84
+ "#{@base_url}#{url}"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,49 @@
1
+ module Contactually
2
+ class Buckets
3
+ def initialize(master)
4
+ @master = master
5
+ end
6
+
7
+ def create(params = {})
8
+ hash = @master.call('buckets.json', :post, params)
9
+ Contactually::Utils.build_bucket(hash)
10
+ end
11
+
12
+ def destroy(id, params = {})
13
+ @master.call("buckets/#{id}.json", :delete, {})
14
+ end
15
+
16
+ def destroy_multiple(params = {})
17
+ @master.call('buckets.json', :delete, params)
18
+ end
19
+
20
+ def show(id, params = {})
21
+ hash = @master.call("buckets/#{id}.json", :get, params)
22
+ Contactually::Utils.build_bucket(hash)
23
+ end
24
+
25
+ def update(id, params = {})
26
+ hash = @master.call("buckets/#{id}.json", :put, params)
27
+ Contactually::Utils.build_bucket(hash);
28
+ end
29
+
30
+ def index(params = {})
31
+ hash = @master.call('buckets.json', :get, params)
32
+ Contactually::Utils.buckets_hash_to_objects(hash)
33
+ end
34
+
35
+ def contacts(id, params = {})
36
+ hash = @master.call("buckets/#{id}/contacts.json", :get, params)
37
+ Contactually::Utils.contacts_hash_to_objects(hash)
38
+ end
39
+
40
+ def search(params = {})
41
+ hash = @master.call('buckets/search.json', :get, params)
42
+ Contactually::Utils.buckets_hash_to_objects(hash)
43
+ end
44
+
45
+ def count(params = { limit: 1 })
46
+ @master.call('buckets.json', :get, params)["meta"]["total"]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module Contactually
2
+ class ContactGroupings
3
+ def initialize(master)
4
+ @master = master
5
+ end
6
+
7
+ def create(id, params = {})
8
+ hash = @master.call("contacts/#{id}/groupings.json", :post, params)
9
+ Contactually::Utils.build_grouping(hash)
10
+ end
11
+
12
+ def destroy(id, grouping_id, params = {})
13
+ @master.call("contacts/#{id}/groupings/#{grouping_id}.json", :delete, params)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ module Contactually
2
+ class Contacts
3
+ def initialize(master)
4
+ @master = master
5
+ end
6
+
7
+ def create(params = {})
8
+ hash = @master.call('contacts.json', :post, params)
9
+ Contactually::Utils.build_contact(hash)
10
+ end
11
+
12
+ def destroy(id, params = {})
13
+ @master.call("contacts/#{id}.json", :delete, {})
14
+ end
15
+
16
+ def destroy_multiple(params = {})
17
+ @master.call('contacts.json', :delete, params)
18
+ end
19
+
20
+ def show(id, params = {})
21
+ hash = @master.call("contacts/#{id}.json", :get, params)
22
+ Contactually::Utils.build_contact(hash)
23
+ end
24
+
25
+ def merge(params = {})
26
+ hash = @master.call('contacts/merge.json', :post, params)
27
+ Contactually::Utils.build_contact(hash)
28
+ end
29
+
30
+ def tags(id, params = {})
31
+ params[:tags] = params[:tags].join(', ') if params[:tags].class == Array
32
+ @master.call("contacts/#{id}/tags.json", :post, params)
33
+ end
34
+
35
+ def update(id, params = {})
36
+ hash = @master.call("contacts/#{id}.json", :put, params)
37
+ Contactually::Utils.build_contact(hash);
38
+ end
39
+
40
+ def index(params = {})
41
+ hash = @master.call('contacts.json', :get, params)
42
+ Contactually::Utils.contacts_hash_to_objects(hash)
43
+ end
44
+
45
+ def search(params = {})
46
+ hash = @master.call('contacts/search.json', :get, params)
47
+ Contactually::Utils.contacts_hash_to_objects(hash)
48
+ end
49
+
50
+ def count(params = { limit: 1 })
51
+ @master.call('contacts.json', :get, params)["total_count"]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ module Contactually
2
+ class Contents
3
+ def initialize(master)
4
+ @master = master
5
+ end
6
+
7
+ def create(params = {})
8
+ hash = @master.call('contents.json', :post, params)
9
+ Contactually::Utils.build_content(hash)
10
+ end
11
+
12
+ def index(params = {})
13
+ hash = @master.call('contents.json', :get, params)
14
+ Contactually::Utils.contents_hash_to_objects(hash)
15
+ end
16
+
17
+ def destroy(id, params = {})
18
+ @master.call("contents/#{id}.json", :delete, params)
19
+ end
20
+
21
+ def show(id, params = {})
22
+ hash = @master.call("contents/#{id}.json", :get, params)
23
+ Contactually::Utils.build_content(hash)
24
+ end
25
+
26
+ def update(id, params = {})
27
+ @master.call("contents/#{id}.json", :put, params)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module Contactually
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ class ConfigMissingApiKeyError < Error
7
+ end
8
+
9
+ class InvalidParametersError < Error
10
+ end
11
+
12
+ class DuplicatedContactError < Error
13
+ end
14
+
15
+ class AuthenticationError < Error
16
+ end
17
+
18
+ class APIError < Error
19
+ end
20
+
21
+ end