interakthq_rails 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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODgzYjA4MDZkMDgyNGY5ZmY5MTRmNGJmNzk5M2M2MDNhNWM3MDA1Mg==
5
+ data.tar.gz: !binary |-
6
+ MmVjYTczMzU2NTUyZjFkZTQyZjQ1NjlkZGFmNTJjNzRiMzQ5MmI5OA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ M2QxMDRhNDIyZmFmYTkwNDk2MTUyM2I4OTJlZjY1YTVjNDZiOTkxMTE4YTA1
10
+ N2M4MmUyZDU4MTE3NWUzMDdiNGI2ODk0MzI2NGZjZjhmZGE0YmRkMGEyYzlh
11
+ MDkxYTUwMGZlMDAxZWM4ZWVjYWY3MmJkYWE5NWY5YzllNTQ0OWQ=
12
+ data.tar.gz: !binary |-
13
+ MjA0MDIyYWFjYzA3NGIyYTk4Mjc3MjZiY2IyN2E5NjkxMmY0OGJkMTlhMThh
14
+ NzliMWM2ODE3ODQwNDBiMmRmYjBmZWQzZmI2Y2IyMDhhMDIwNzg5MDBmMTA2
15
+ MGFjYWZhMTIxNTBjMDUzZWMxODk0MGRkM2Y1M2FkMjRiY2I1Mjc=
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in interakthq_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Manoj
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,29 @@
1
+ # InterakthqRails
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'interakthq_rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install interakthq_rails
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'interakthq_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "interakthq_rails"
8
+ spec.version = InterakthqRails::VERSION
9
+ spec.authors = ["Manoj"]
10
+ spec.email = ["manoj.delhi24@gmail.com"]
11
+ spec.description = %q{Import your users data into interaktHQ}
12
+ spec.summary = %q{The Customer Interaction HeadQuarter. Easily interact with every single user of your app.}
13
+ spec.homepage = "http://interakthq.com/"
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.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency 'activesupport', '>3.0'
24
+ end
@@ -0,0 +1,74 @@
1
+ require "interakthq_rails/user_validator"
2
+
3
+ module InterakthqRails
4
+ class Import
5
+ include UserValidator
6
+
7
+ attr_reader :uri, :http, :max_batch_size
8
+ attr_accessor :members_sent, :response_verified
9
+
10
+ def self.start
11
+ new.start
12
+ end
13
+
14
+ def self.uri
15
+ host = (Rails.env.production? ? "http://interakthq.com" : "http://localhost:3000")
16
+ URI.parse(host + "/api/v1/members/import_data")
17
+ end
18
+
19
+ def initialize
20
+ @uri = Import.uri
21
+ @http = Net::HTTP.new(@uri.host, @uri.port)
22
+ @max_batch_size = 50
23
+ @members_sent = 0
24
+ end
25
+
26
+ def start
27
+ check_basic_validation
28
+ start_sending_in_batches
29
+ end
30
+
31
+ def start_sending_in_batches
32
+ model_name.find_in_batches batch_size: max_batch_size do |members|
33
+ members_json = json_collection_of_members( members)
34
+ send_members( members_json)
35
+
36
+ @members_sent += members_json.length
37
+ puts "#{members_sent} users sent."
38
+ end
39
+ end
40
+
41
+ def send_members( members)
42
+ request = Net::HTTP::Post.new(uri.request_uri)
43
+ request.basic_auth(InterakthqRails.config.app_id, InterakthqRails.config.api_key)
44
+ request["Content-Type"] = "application/json"
45
+ request.body = {members: members}.to_json
46
+
47
+ response = http.request(request)
48
+ verify_reponse( response) unless response_verified
49
+ end
50
+
51
+ def json_collection_of_members( members)
52
+ members.map do |member|
53
+ member_hash = {}
54
+ availabe_model_attributes.each do |attribute|
55
+ member_hash[attribute] = member.send( attribute)
56
+ end
57
+ member_hash.merge( custom_data_attributes_for_member(member))
58
+ end.compact
59
+ end
60
+
61
+
62
+ def custom_data_attributes_for_member(member)
63
+ if( (custom_data = InterakthqRails.config.user.custom_data).present?)
64
+ custom_data.inject({}) do |hash, (key, value)|
65
+ hash[key] = ( value.kind_of?(Proc) ? value.call(member) : member.send( value))
66
+ hash
67
+ end
68
+ else
69
+ {}
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,12 @@
1
+ namespace :interakthq do
2
+
3
+ desc "Import your users data into interaktHQ"
4
+ task :import => :environment do
5
+ begin
6
+ InterakthqRails::Import.start
7
+ rescue => e
8
+ puts e.message
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,7 @@
1
+ module InterakthqRails
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'interakthq_rails/interakthq.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_support'
2
+
3
+ module InterakthqRails
4
+ class Setting
5
+ cattr_accessor :app_id, :api_key
6
+ cattr_accessor :user do
7
+ Struct.new(:model, :custom_data).new( Proc.new { User } )
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ module InterakthqRails
2
+ module UserValidator
3
+ EXPECTED_ATTRIBUTES = ['email', 'name', 'first_name', 'last_name','firstname', 'lastname', 'created_at']
4
+
5
+ def check_basic_validation
6
+ raise 'We only supports ActiveRecord' unless (defined?(ActiveRecord::Base) && (model_name < ActiveRecord::Base))
7
+ raise "You have to add 'app_id' in your 'config/initializers/interakthq.rb' file first" unless InterakthqRails.config.app_id.present?
8
+ raise "You have to add 'api_key' in your 'config/initializers/interakthq.rb' file first" unless InterakthqRails.config.api_key.present?
9
+ raise "Your model must have 'email' field" unless model_name.new.respond_to? :email
10
+
11
+ end
12
+
13
+ def model_name
14
+ unless @model_name
15
+ begin
16
+ InterakthqRails.config.user.model.call
17
+ rescue Exception => e
18
+ raise "Model doesn't exists"
19
+ end
20
+ else
21
+ @model_name
22
+ end
23
+ end
24
+
25
+ def availabe_model_attributes
26
+ @availabe_model_attributes ||= EXPECTED_ATTRIBUTES.select{ |attribute| model_name.new.respond_to? attribute}
27
+ end
28
+
29
+ def verify_reponse( response)
30
+ raise "Your 'app_id' is not corrent. Please correct it in 'config/initializers/interakthq.rb'" if response.code.to_i == 401
31
+ raise "Something went wrong. Please report the issue." if response.code.to_i == 500
32
+ response_verified = true
33
+ end
34
+
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module InterakthqRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require "interakthq_rails/setting"
2
+ require "interakthq_rails/import"
3
+ require "interakthq_rails/version"
4
+ require "interakthq_rails/railtie"
5
+
6
+
7
+ module InterakthqRails
8
+
9
+ def self.config
10
+ block_given? ? yield(Setting) : Setting
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ module Interakthq
2
+ module Generators
3
+ class ConfigGenerator < ::Rails::Generators::Base
4
+
5
+ def self.source_root
6
+ File.dirname(__FILE__)
7
+ end
8
+
9
+ argument :app_id, :desc => "Your Interakthq app-id, which can be found here: https://www.interakthq.com/api_keys"
10
+ argument :api_key, :desc => "An Interakthq API key, for various rake tasks", :optional => true
11
+
12
+ def create_initializer_file
13
+ @app_id = app_id
14
+ @api_key = api_key
15
+
16
+ template("interakthq.rb.erb", "config/initializers/interakthq.rb")
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ InterakthqRails.config do |config|
2
+ # == Interakthq app_id
3
+ #
4
+ config.app_id = ENV["INTERAKTHQ_APP_ID"] || "<%= @app_id %>"
5
+
6
+ # == Interakthq API Key
7
+ # This is required for some Interakthq rake tasks like importing your users;
8
+ # you can generate one at https://www.interakthq.com/.
9
+ #
10
+ <%- if @api_key -%>
11
+ config.api_key = "<%= @api_key %>"
12
+ <%- else -%>
13
+ # config.api_key = "..."
14
+ <%- end -%>
15
+
16
+ # == User model class
17
+ # The class which defines your user model
18
+ #
19
+ # config.user.model = Proc.new { User }
20
+
21
+ # == User Custom Data
22
+ # A hash of additional data you wish to send about your users.
23
+ # You can provide either a method name which will be sent to the current
24
+ # user object, or a Proc which will be passed the current user.
25
+ #
26
+ # config.user.custom_data = {
27
+ # :plan => Proc.new { |current_user| current_user.plan.name },
28
+ # :favorite_color => :favorite_color
29
+ # }
30
+
31
+
32
+
33
+
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interakthq_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manoj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>'
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>'
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Import your users data into interaktHQ
56
+ email:
57
+ - manoj.delhi24@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .DS_Store
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - interakthq_rails.gemspec
69
+ - lib/interakthq_rails.rb
70
+ - lib/interakthq_rails/import.rb
71
+ - lib/interakthq_rails/interakthq.rake
72
+ - lib/interakthq_rails/railtie.rb
73
+ - lib/interakthq_rails/setting.rb
74
+ - lib/interakthq_rails/user_validator.rb
75
+ - lib/interakthq_rails/version.rb
76
+ - lib/rails/generators/interakthq/config/config_generator.rb
77
+ - lib/rails/generators/interakthq/config/interakthq.rb.erb
78
+ homepage: http://interakthq.com/
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: The Customer Interaction HeadQuarter. Easily interact with every single user
102
+ of your app.
103
+ test_files: []