closeio-rails 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
+ SHA1:
3
+ metadata.gz: e7203477a04ff5ef6a351a70598b7106857e205c
4
+ data.tar.gz: 076170b93c9f9a57b2a4fbf1064af82b036bf37e
5
+ SHA512:
6
+ metadata.gz: c19c337c975d042c456ee0bdf177ed051d27dc12053d801d745eb8935951fceadcc87934af3f9dc0cefd9c9ac67b879394cc0d1bea30a473ff73a3f36ff3ba7f
7
+ data.tar.gz: 461ea3a9bbb20485c6294a6cdfc0fbd45e8de8fc79ebab5247beadda0523df9f8e40e52a1ead202b5a473f643183242a3d7d85cf9a50414fabe9d9e1774a1543
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ closeio_rails
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ed@errorstudio.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in close-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Error Ltd
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,52 @@
1
+ # Closeio::Rails
2
+ A handy wrapper around the `closeio` gem to make Rails integration a little easier.
3
+
4
+ ## Models
5
+ There are 4 models defined in this gem. The aim is to have a DSL a little more like the familiar ActiveRecord one.
6
+
7
+ ### `Closeio::Rails::Base`
8
+ All the models inherit from `Closeio::Rails::Base`, which has a couple of mixins:
9
+
10
+ #### `Closeio::Rails::Attributes`
11
+ Make the model respond to `attributes()` - it returns a hash, like ActiveRecord
12
+
13
+ #### `Closeio::Rails::DateCoercion`
14
+ Parse the creation and update dates into datetime objects, and add `created_at()` and `updated_at()` methods which are in line with Rails norms.
15
+
16
+ ### `Closeio::Rails::Lead`
17
+ This is where most of the action happens. Leads have contacts, of type `Closeio::Rails::Contact`
18
+
19
+ ### `Closeio::Rails::Contact`
20
+ A contact entry in Close.io. Not accessible on its own; always created after a call to `Closeio::Lead
21
+
22
+ ### `Closeio::Rails::CustomField`
23
+ It's often useful to create and update custom fields in Close.io programatically - changing the values of select lists and similar.
24
+
25
+ ### `Closeio::Rails::Note`
26
+ This is a very lightweight wrapper around Close::Client#create_note.
27
+
28
+
29
+ ## Mountable webhooks controller
30
+ It's useful to receive webhooks from Close.io. You have to drop a note to their support team to set it up but it's very quick.
31
+
32
+ Mount your route like this:
33
+
34
+ ```
35
+ mount Closeio::Rails::Engine => "/closeio" #this can be any path you like
36
+ ```
37
+
38
+ That'll give you the following routes:
39
+
40
+ ```
41
+ debug_webhooks GET /webhooks/debug(.:format) closeio/rails/webhooks#debug {:format=>:json}
42
+ webhooks POST /webhooks(.:format) closeio/rails/webhooks#create {:format=>:json}
43
+ ```
44
+
45
+ The `debug_webhooks_path` only works in development. It just sanity-checks that you've got ot mounted properly :)
46
+
47
+
48
+
49
+
50
+
51
+
52
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ module Closeio
2
+ module Rails
3
+ class WebhooksController < ApplicationController
4
+ skip_before_action :verify_authenticity_token, only: [:create]
5
+
6
+ # def closeio
7
+ # if params[:model] == 'lead'
8
+ # SyncUsersJob.perform_later params[:data][:id]
9
+ # end
10
+ # head :ok
11
+ # end
12
+
13
+ def create
14
+ request.format = :json
15
+ event = params[:event]
16
+ ActiveSupport::Notifications.instrument("closeio.#{event}", params)
17
+
18
+ #must return an ok - check Rails version to determine whether to return nothing or head response.
19
+ if ::Rails.version =~ /^4/
20
+ render nothing: true
21
+ else
22
+ head :ok
23
+ end
24
+ end
25
+
26
+ def debug
27
+ render json: {response: "It works - this is the debug method in Closeio::Rails::WebhooksController"}
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ module Closeio
2
+ module Rails
3
+ class Base < OpenStruct
4
+ include DateCoercion
5
+ include Attributes
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ module Closeio
2
+ module Rails
3
+ class Contact < Base
4
+
5
+ def has_login?
6
+ emails.find {|e| e['type'] == 'other'}.present?
7
+ end
8
+
9
+ def email
10
+ raise NotImplementedError, "You need to implement Closeio::Rails::Contact#email in your own subclass or prepended mixin."
11
+ end
12
+
13
+ def phone_numbers
14
+ phones.inject({}) {|h, p| h[p['phone_formatted']] = p['type']; h}
15
+ end
16
+
17
+ def email_addresses
18
+ emails.inject({}) {|h,data| h[data[:email]] = data[:type]; h}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
1
+ module Closeio
2
+ module Rails
3
+ class CustomField < Base
4
+
5
+ def self.all
6
+ Closeio::Rails.configuration.client.list_custom_fields.collect do |field|
7
+ self.new(field)
8
+ end
9
+ end
10
+
11
+ def self.find(id)
12
+ self.new(Closeio::Rails.configuration.client.find_custom_field(id))
13
+ end
14
+
15
+ def self.create(params)
16
+ response = Closeio::Rails.configuration.client.create_custom_field(params)
17
+ if response.has_key?('field-errors')
18
+ raise Closeio::Error, "#{response['field-errors']['name']}"
19
+ elsif response.has_key?('error')
20
+ raise Closeio::Error, "#{response['error']}"
21
+ else
22
+ response
23
+ end
24
+ end
25
+
26
+ def add_choice(choice)
27
+ new_choices = choices.push(choice)
28
+ Closeio::Rails.configuration.client.update_custom_field(id,choices: new_choices)
29
+ end
30
+
31
+ def remove_choice(choice)
32
+ new_choices = choices.reject {|c| c == choice}
33
+ Closeio::Rails.configuration.client.update_custom_field(id,choices: new_choices)
34
+ end
35
+
36
+ def set_all_choices(choices)
37
+ Closeio::Rails.configuration.client.update_custom_field(id,choices: choices)
38
+ end
39
+
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module Closeio
2
+ module Rails
3
+ class Lead < Base
4
+ attr_reader :contacts
5
+
6
+ def self.all
7
+ Closeio::Rails.configuration.client.list_leads("*", per_page: 99999).data.collect do |lead|
8
+ self.new(lead.to_hash)
9
+ end
10
+ end
11
+
12
+ def self.with_status(status)
13
+ Closeio::Rails.configuration.client.list_leads("lead_status: #{status}", per_page: 99999).data.collect do |lead|
14
+ self.new(lead.to_hash)
15
+ end
16
+ end
17
+
18
+ def self.find(id)
19
+ self.new(Closeio::Rails.configuration.client.find_lead(id))
20
+ end
21
+
22
+ def self.create!(payload)
23
+ Closeio::Rails.configuration.client.create_lead(payload)
24
+ end
25
+
26
+ def contacts
27
+ @contacts ||= super.collect do |contact|
28
+ Contact.new(contact)
29
+ end
30
+ end
31
+
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module Closeio
2
+ module Rails
3
+ class Note < Base
4
+ def self.create!(payload)
5
+ Closeio::Rails.configuration.client.create_note(payload)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Closeio
2
+ module Rails
3
+ module Attributes
4
+ extend ActiveSupport::Concern
5
+
6
+ def attributes
7
+ to_h
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Closeio
2
+ module Rails
3
+ module DateCoercion
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ def initialize(hash)
7
+ hash['date_created'] = DateTime.parse(hash['date_created'])
8
+ hash['date_updated'] = DateTime.parse(hash['date_updated'])
9
+ hash['created_at'] = hash['date_created']
10
+ hash['updated_at'] = hash['date_updated']
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "close/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
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,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'closeio/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "closeio-rails"
8
+ spec.version = Closeio::Rails::VERSION
9
+ spec.authors = ["Ed Jones", "Paul Hendrick"]
10
+ spec.email = ["ed@error.agency", "paul@error.agency"]
11
+
12
+ spec.summary = %q{A wrapper around the closeio gem to provide some useful functionality in Rails}
13
+ spec.homepage = "https://github.com/errorstudio/close-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_dependency "require_all", "~> 1.3"
27
+ spec.add_dependency "closeio", "~> 2.6"
28
+ spec.add_dependency "rails", ">= 4.2", "< 6.0.0"
29
+
30
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ Closeio::Rails::Engine.routes.draw do
2
+ resources :webhooks, only: [:create], defaults: { format: :json } do
3
+ collection do
4
+ scope constraints: ->(request){ Rails.env.development? } do
5
+ get "/debug", to: "webhooks#debug"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Closeio
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+
5
+ isolate_namespace Closeio::Rails
6
+
7
+ config.before_initialize do
8
+
9
+ end
10
+
11
+
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Closeio
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'require_all'
2
+ require_rel '.'
3
+
4
+ module Closeio
5
+ module Rails
6
+
7
+ class << self
8
+ attr_accessor :configuration
9
+ def configure
10
+ self.configuration ||= Configuration.new
11
+ yield(configuration)
12
+ self.configuration.configure_connection
13
+ end
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :api_key, :verbose
18
+ attr_reader :client
19
+
20
+ def configure_connection
21
+ @verbose ||= false
22
+ @client = Closeio::Client.new(@api_key, @verbose, nil, true)
23
+ end
24
+ end
25
+
26
+ class Error < StandardError
27
+
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: closeio-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Jones
8
+ - Paul Hendrick
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.13'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.13'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: require_all
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.3'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.3'
56
+ - !ruby/object:Gem::Dependency
57
+ name: closeio
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.6'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.6'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rails
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '4.2'
77
+ - - "<"
78
+ - !ruby/object:Gem::Version
79
+ version: 6.0.0
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '4.2'
87
+ - - "<"
88
+ - !ruby/object:Gem::Version
89
+ version: 6.0.0
90
+ description:
91
+ email:
92
+ - ed@error.agency
93
+ - paul@error.agency
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - ".ruby-gemset"
100
+ - ".ruby-version"
101
+ - CODE_OF_CONDUCT.md
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - app/controllers/closeio/rails/webhooks_controller.rb
107
+ - app/models/closeio/rails/base.rb
108
+ - app/models/closeio/rails/contact.rb
109
+ - app/models/closeio/rails/custom_field.rb
110
+ - app/models/closeio/rails/lead.rb
111
+ - app/models/closeio/rails/note.rb
112
+ - app/models/concerns/closeio/rails/attributes.rb
113
+ - app/models/concerns/closeio/rails/date_coercion.rb
114
+ - bin/console
115
+ - bin/setup
116
+ - close-rails.gemspec
117
+ - config/routes.rb
118
+ - lib/closeio/rails.rb
119
+ - lib/closeio/rails/engine.rb
120
+ - lib/closeio/rails/version.rb
121
+ homepage: https://github.com/errorstudio/close-rails
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.6.12
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A wrapper around the closeio gem to provide some useful functionality in
145
+ Rails
146
+ test_files: []