unique_response_ruby 0.1.0

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: 4f11e3916f4c04ba3fd47d4626f5e6931f16ce59
4
+ data.tar.gz: 68dd7ac19824d995a3912eabdf3516110dd82556
5
+ SHA512:
6
+ metadata.gz: c65eff1e9b39d4141938d9a41f128f50f9302ac72723929e6bb83509ba5925430204e7a29edc1efcc76b0f217fee811febdd441522656dc3d44bf210065079b9
7
+ data.tar.gz: 37a47c1dcfba4dad7efb7e1472a5e962fb764b5a5c3d4852fbe69168985677c912507f4f8dc9c595c3b0e026be0ddca359af26a3fb1c2d022f38427dd9201848
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # environment variables
15
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unique_response_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Alston
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,73 @@
1
+ # UniqueResponseRuby
2
+
3
+ Official Ruby SDK for Bovitz Unique Response.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'unique_response_ruby'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install unique_response_ruby
21
+
22
+
23
+ ## Setup
24
+
25
+ Set the following environment variables
26
+
27
+ UNIQUE_RESPONSE_ACCOUNT_ID
28
+ UNIQUE_RESPONSE_AUTH_TOKEN
29
+ UNIQUE_RESPONSE_ENDPOINT
30
+
31
+
32
+ ## Usage
33
+
34
+
35
+ ```ruby
36
+ # Initialize a new survey response with the needed attribute values as all strings. All values are required.
37
+ response = UniqueResponse::Response.new("panelist123", "respondent123", "63.116.49.146", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36", "survey123")
38
+
39
+ # Use the "save" method to post it to the API
40
+ response.save
41
+ # => true
42
+
43
+ # If the "save" method was successful, use "matches" to return an array of matching responses that share the same ip_address and survey_token. If no matches are found, an empty array is returned.
44
+ response.matches
45
+ # => [{"created_at"=>1491424044817, "survey_token"=>"survey123", "ip_address"=>"63.116.49.146", "respondent_id"=>"respondent234", "user_agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36", "panelist_id"=>"panelist234", "id"=>"47834810-1a3e-11e7-96bd-e56461835d2d"}, {"created_at"=>1491424066668, "survey_token"=>"survey123", "ip_address"=>"63.116.49.146", "respondent_id"=>"respondent345", "user_agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.37", "panelist_id"=>"panelist345", "id"=>"54897ac0-1a3e-11e7-96bd-e56461835d2d"}]
46
+
47
+
48
+ # Use these convenient methods to detect duplications
49
+ response.duplicate_ip_address?
50
+ # => true
51
+
52
+ response.duplicate_user_agent?
53
+ # => true
54
+
55
+ response.duplicate_ip_and_ua?
56
+ # => true
57
+ ```
58
+
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/johnalston/unique_response_ruby.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unique_response_ruby"
5
+ require "dotenv"
6
+ Dotenv.load!
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
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,7 @@
1
+ require "unique_response_ruby/version"
2
+ require "unique_response_ruby/client"
3
+ require "unique_response_ruby/response"
4
+
5
+ module UniqueResponse
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,54 @@
1
+ module UniqueResponse
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'json'
5
+
6
+ class Client
7
+ attr_reader :successful, :data
8
+
9
+ HTTP_HEADERS = {
10
+ 'Content-Type' => 'application/json',
11
+ 'Accept' => 'application/json',
12
+ 'Accept-Charset' => 'utf-8',
13
+ 'User-Agent' => 'unique_response_ruby'
14
+ }
15
+
16
+ def initialize
17
+ @account_id = ENV['UNIQUE_RESPONSE_ACCOUNT_ID']
18
+ @auth_token = ENV['UNIQUE_RESPONSE_AUTH_TOKEN']
19
+ if @account_id.nil? || @auth_token.nil?
20
+ raise ArgumentError, 'Account SID and auth token are required'
21
+ end
22
+ end
23
+
24
+ def save_response(response)
25
+ @uri = URI(ENV['UNIQUE_RESPONSE_ENDPOINT'])
26
+ @http_request = Net::HTTP::Post.new @uri.path, HTTP_HEADERS
27
+ @http_request.body = response.to_h.to_json
28
+ perform_request
29
+ end
30
+
31
+ private
32
+ def perform_request
33
+ http_response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
34
+ http.request @http_request
35
+ end
36
+
37
+ case http_response
38
+ when Net::HTTPSuccess, Net::HTTPRedirection
39
+ body = JSON.parse http_response.body
40
+ if @successful = body["successful"] || false
41
+ @data = body["data"]
42
+ else
43
+ @data = "Error saving response: #{body["data"].to_json}"
44
+ end
45
+ else
46
+ @data = "#{http_response.code}: #{http_response.body}"
47
+ @successful = false
48
+ end
49
+
50
+ return @successful
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ module UniqueResponse
2
+
3
+ Response = Struct.new(:panelist_id, :respondent_id, :ip_address, :user_agent, :survey_token) do
4
+ attr_reader :matches
5
+
6
+ def valid?
7
+ values.map { |value| value.is_a?(String) && !value.empty? }.reduce(:&)
8
+ end
9
+
10
+ def save
11
+ raise ArgumentError unless valid?
12
+
13
+ client = Client.new
14
+ if client.save_response(self)
15
+ @matches = client.data
16
+ true
17
+ else
18
+ false
19
+ end
20
+ end
21
+
22
+ def duplicate_ip_address?
23
+ @matches.any? { |match| match["ip_address"] == ip_address }
24
+ end
25
+
26
+ def duplicate_user_agent?
27
+ @matches.any? { |match| match["user_agent"] == user_agent }
28
+ end
29
+
30
+ def duplicate_ip_and_ua?
31
+ @matches.any? { |match| match["ip_address"] == ip_address && match["user_agent"] == user_agent }
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,3 @@
1
+ module UniqueResponseRuby
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unique_response_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unique_response_ruby"
8
+ spec.version = UniqueResponseRuby::VERSION
9
+ spec.authors = ["John Alston"]
10
+ spec.email = ["jalston@bovitzinc.com"]
11
+
12
+ spec.summary = %q{Ruby SDK for Unique Response}
13
+ spec.description = %q{Ruby SDK for Unique Response}
14
+ spec.homepage = "http://www.bovitzinc.com"
15
+ spec.license = "MIT"
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.14"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "webmock"
35
+ spec.add_development_dependency "dotenv"
36
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unique_response_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Alston
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-10 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby SDK for Unique Response
84
+ email:
85
+ - jalston@bovitzinc.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/unique_response_ruby.rb
101
+ - lib/unique_response_ruby/client.rb
102
+ - lib/unique_response_ruby/response.rb
103
+ - lib/unique_response_ruby/version.rb
104
+ - unique_response_ruby.gemspec
105
+ homepage: http://www.bovitzinc.com
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.11
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Ruby SDK for Unique Response
130
+ test_files: []