contactually-api 0.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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +62 -0
  4. data/Rakefile +7 -0
  5. data/lib/contactually-api.rb +34 -0
  6. data/lib/contactually/accounts.rb +31 -0
  7. data/lib/contactually/api.rb +70 -0
  8. data/lib/contactually/contact_groupings.rb +16 -0
  9. data/lib/contactually/contacts.rb +57 -0
  10. data/lib/contactually/contents.rb +38 -0
  11. data/lib/contactually/errors.rb +18 -0
  12. data/lib/contactually/groupings.rb +49 -0
  13. data/lib/contactually/middleware/error_detector.rb +27 -0
  14. data/lib/contactually/notes.rb +39 -0
  15. data/lib/contactually/representer/account_representer.rb +13 -0
  16. data/lib/contactually/representer/contact_representer.rb +46 -0
  17. data/lib/contactually/representer/content_representer.rb +16 -0
  18. data/lib/contactually/representer/grouping_representer.rb +28 -0
  19. data/lib/contactually/representer/grouping_statistics_representer.rb +12 -0
  20. data/lib/contactually/representer/note_representer.rb +13 -0
  21. data/lib/contactually/representer/task_representer.rb +19 -0
  22. data/lib/contactually/tasks.rb +12 -0
  23. data/lib/contactually/version.rb +3 -0
  24. data/spec/accounts_spec.rb +57 -0
  25. data/spec/api_spec.rb +87 -0
  26. data/spec/contact_groupings_spec.rb +43 -0
  27. data/spec/contacts_spec.rb +117 -0
  28. data/spec/contents_spec.rb +81 -0
  29. data/spec/fixtures/account.json +10 -0
  30. data/spec/fixtures/accounts_index.json +16 -0
  31. data/spec/fixtures/contact.json +38 -0
  32. data/spec/fixtures/contacts_index.json +169 -0
  33. data/spec/fixtures/content.json +10 -0
  34. data/spec/fixtures/contents_index.json +26 -0
  35. data/spec/fixtures/grouping.json +19 -0
  36. data/spec/fixtures/groupings_index.json +24 -0
  37. data/spec/fixtures/groupings_statistics.json +46 -0
  38. data/spec/fixtures/note.json +7 -0
  39. data/spec/fixtures/note_destroy.json +4 -0
  40. data/spec/fixtures/notes_index.json +27 -0
  41. data/spec/fixtures/task.json +27 -0
  42. data/spec/groupings_spec.rb +100 -0
  43. data/spec/notes_spec.rb +89 -0
  44. data/spec/spec_helper.rb +14 -0
  45. data/spec/tasks_spec.rb +36 -0
  46. metadata +194 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 749467e599e692c5cc21a49444d12055f0ca31b7
4
+ data.tar.gz: 492eb3805a030a9921e75d4b8284388675f37817
5
+ SHA512:
6
+ metadata.gz: 6042bbafb8b6c2df1747d3c6685795d1d90fa1823287cbda53bf436dc193d03649d308235674c2d37bf493554927c5d17539b35bb02196624494608376206a47
7
+ data.tar.gz: 9f182641b3031f9f08f2b6fb1e2160aaf346e44b0f704a3c7b5405ae05ad9a53ef0e28b788b7ed1a3f5028ed622a01db091b55eed0f4fe465dbce18d03ea6d7a
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Johannes Heck
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,62 @@
1
+ # Contactually-API
2
+
3
+ [![Build
4
+ Status](https://travis-ci.org/railslove/contactually-api.svg?branch=master)](https://travis-ci.org/railslove/contactually-api)
5
+
6
+ TODO: Write a gem description
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'contactually-api'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install contactually-api
21
+
22
+ ## Usage
23
+
24
+ ### Configuration
25
+
26
+ As `contactually-api` is shipped with sensible defaults, that will allow you
27
+ to start right away, there are a couple of settings you may want to adjust,
28
+ depending on your applications requirements.
29
+
30
+ The available configuration options are:
31
+
32
+ * api_key (your contactually api_key)
33
+ * contactually_url (Default: "https://www.contactually.com/api/v1/")
34
+
35
+ Configuration goes as follows:
36
+
37
+ Contactually.configure do |c|
38
+ c.api_key = "YOURKEY"
39
+ c.contactually_url = "URL"
40
+ end
41
+
42
+ #### How to use the gem
43
+
44
+ contactually = Contactually::API.new
45
+ contacts = contactually.contacts.index
46
+ notes = contactually.notes.index
47
+ groupings = contactually.groupings.index
48
+
49
+ contact = { contact: { first_name: 'Jane', last_name: 'Doe', ... } }
50
+ contactually.contacts.create(contact)
51
+
52
+ ...
53
+
54
+ The API is documented here: [Contactually API Docs](http://developers.contactually.com/docs/)
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 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,34 @@
1
+ require 'active_support/configurable'
2
+ require 'faraday'
3
+ require 'json'
4
+ require 'roar/decorator'
5
+ require 'roar/representer/json'
6
+
7
+ require 'contactually/version'
8
+ require 'contactually/errors'
9
+
10
+ require 'contactually/middleware/error_detector'
11
+
12
+ require 'contactually/representer/grouping_representer'
13
+ require 'contactually/representer/account_representer'
14
+ require 'contactually/representer/contact_representer'
15
+ require 'contactually/representer/content_representer'
16
+ require 'contactually/representer/grouping_statistics_representer'
17
+ require 'contactually/representer/note_representer'
18
+ require 'contactually/representer/task_representer'
19
+
20
+ require 'contactually/api'
21
+ require 'contactually/groupings'
22
+ require 'contactually/contacts'
23
+ require 'contactually/notes'
24
+ require 'contactually/contact_groupings'
25
+ require 'contactually/accounts'
26
+ require 'contactually/contents'
27
+ require 'contactually/tasks'
28
+
29
+ module Contactually
30
+ include ActiveSupport::Configurable
31
+
32
+ config_accessor(:api_key)
33
+ config_accessor(:contactually_url) { "https://www.contactually.com/api/v1/" }
34
+ end
@@ -0,0 +1,31 @@
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
+ accounts_hash_to_objects(hash)
11
+ end
12
+
13
+ def show(id, params = {})
14
+ hash = @master.call("accounts/#{id}.json", :get, params)
15
+ AccountRepresenter.new(Account.new).from_hash(hash)
16
+ end
17
+
18
+ def destroy(id, params = {})
19
+ @master.call("accounts/#{id}.json", :delete, params)
20
+ end
21
+
22
+ private
23
+
24
+ def accounts_hash_to_objects(hash)
25
+ hash['accounts'].inject([]) do |arr, account|
26
+ arr << AccountRepresenter.new(Account.new).from_hash(account)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ module Contactually
2
+ class API
3
+ def initialize
4
+ raise ConfigMissingApiKeyError, 'You must provide a Contactually API key' unless Contactually.config.api_key
5
+ @api_key = Contactually.config.api_key
6
+ @base_url = Contactually.config.contactually_url
7
+ end
8
+
9
+ def call(url, method, params={})
10
+ response = send(method, url, params)
11
+ JSON.load(response.body)
12
+ end
13
+
14
+ def contacts
15
+ Contactually::Contacts.new self
16
+ end
17
+
18
+ def notes
19
+ Contactually::Notes.new self
20
+ end
21
+
22
+ def groupings
23
+ Contactually::Groupings.new self
24
+ end
25
+
26
+ def accounts
27
+ Contactually::Accounts.new self
28
+ end
29
+
30
+ def contact_groupings
31
+ Contactually::ContactGroupings.new self
32
+ end
33
+
34
+ def connection
35
+ @connection ||= Faraday.new do |faraday|
36
+ faraday.adapter Faraday.default_adapter
37
+ faraday.headers['Content-Type'] = 'application/json'
38
+ faraday.use Contactually::Middleware::ErrorDetector
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def call_params(params)
45
+ params.merge({ api_key: @api_key })
46
+ end
47
+
48
+ [ :post, :put ].each do |method_name|
49
+ define_method(method_name) do |url, params|
50
+ response = connection.send(method_name) do |req|
51
+ req.url base_url(url)
52
+ req.body = call_params(params).to_json
53
+ end
54
+ response
55
+ end
56
+ end
57
+
58
+ [ :get, :delete ].each do |method_name|
59
+ define_method(method_name) do |url, params|
60
+ response = connection.send(method_name, base_url(url), call_params(params))
61
+ response
62
+ end
63
+ end
64
+
65
+ def base_url(url)
66
+ "#{@base_url}#{url}"
67
+ end
68
+
69
+ end
70
+ 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
+ GroupingRepresenter.new(Grouping.new).from_hash(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,57 @@
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
+ ContactRepresenter.new(Contact.new).from_hash(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
+ ContactRepresenter.new(Contact.new).from_hash(hash)
23
+ end
24
+
25
+ def merge(params = {})
26
+ hash = @master.call('contacts/merge.json', :post, params)
27
+ ContactRepresenter.new(Contact.new).from_hash(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
+ @master.call("contacts/#{id}.json", :put, params)
37
+ end
38
+
39
+ def index(params = {})
40
+ hash = @master.call('contacts.json', :get, params)
41
+ contacts_hash_to_objects(hash)
42
+ end
43
+
44
+ def search(params = {})
45
+ hash = @master.call('contacts/search.json', :get, params)
46
+ contacts_hash_to_objects(hash)
47
+ end
48
+
49
+ private
50
+
51
+ def contacts_hash_to_objects(hash)
52
+ hash['contacts'].inject([]) do |arr, contact|
53
+ arr << ContactRepresenter.new(Contact.new).from_hash(contact)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,38 @@
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
+ ContentRepresenter.new(Content.new).from_hash(hash)
10
+ end
11
+
12
+ def index(params = {})
13
+ hash = @master.call('contents.json', :get, params)
14
+ 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
+ ContentRepresenter.new(Content.new).from_hash(hash)
24
+ end
25
+
26
+ def update(id, params = {})
27
+ @master.call("contents/#{id}.json", :put, params)
28
+ end
29
+
30
+ private
31
+
32
+ def contents_hash_to_objects(hash)
33
+ hash['contents'].inject([]) do |arr, content|
34
+ arr << ContentRepresenter.new(Content.new).from_hash(content)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
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 APIError < Error
16
+ end
17
+
18
+ end
@@ -0,0 +1,49 @@
1
+ module Contactually
2
+ class Groupings
3
+ def initialize(master)
4
+ @master = master
5
+ end
6
+
7
+ def create(params = {})
8
+ hash = @master.call('groupings.json', :post, params)
9
+ GroupingRepresenter.new(Grouping.new).from_hash(hash)
10
+ end
11
+
12
+ def index(params = {})
13
+ hash = @master.call('groupings.json', :get, params)
14
+ groupings_hash_to_objects(hash)
15
+ end
16
+
17
+ def minimal_index(params = {})
18
+ hash = @master.call('groupings/minimal_index.json', :get, params)
19
+ GroupingRepresenter.new(Grouping.new).from_hash(hash)
20
+ end
21
+
22
+ def destroy(id, params = {})
23
+ @master.call("groupings/#{id}.json", :delete, params)
24
+ end
25
+
26
+ def show(id, params = {})
27
+ hash = @master.call("groupings/#{id}.json", :get, params)
28
+ GroupingRepresenter.new(Grouping.new).from_hash(hash)
29
+ end
30
+
31
+ def update(id, params = {})
32
+ hash = @master.call("groupings/#{id}.json", :put, params)
33
+ GroupingRepresenter.new(Grouping.new).from_hash(hash)
34
+ end
35
+
36
+ def statistics(id, params = {})
37
+ hash = @master.call("groupings/#{id}/statistics.json", :get, params)
38
+ GroupingStatisticsRepresenter.new(GroupingStatistics.new).from_hash(hash)
39
+ end
40
+
41
+ private
42
+
43
+ def groupings_hash_to_objects(hash)
44
+ hash['groupings'].inject([]) do |arr, grouping|
45
+ arr << GroupingRepresenter.new(Grouping.new).from_hash(grouping)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+ module Contactually
2
+ module Middleware
3
+ class ErrorDetector < Faraday::Middleware
4
+
5
+ def call(env)
6
+ @app.call(env).on_complete do
7
+ unless (200..299).include? env[:status]
8
+ cast_error(env[:body])
9
+ end
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def cast_error(body)
16
+ case JSON.parse(body)['error']
17
+ when /^Invalid parameters/ then
18
+ raise InvalidParametersError, body
19
+ when /^We already have/ then
20
+ raise DuplicatedContactError, body
21
+ else
22
+ raise APIError, body
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end