handiv 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: 721a25d6f64fcd6dad901e0d4394f534ad244970
4
+ data.tar.gz: ad9f20bd05589af1494c705cde5af8f8c19232ef
5
+ SHA512:
6
+ metadata.gz: bbf9b37f6ee8966532ce25051ea770a6c6ca73c0ff5f9e13ca62bdb50f9623a46faa88f1968abfc54cd0b7135c3d0cccb032e34a56a06ae15628f2400bf07f11
7
+ data.tar.gz: ba6991592d29bc4c0128e10c2c35a75773aaec5cb6589e6cdb5a590238d75db2328cdf003fd1d8827d6f21f6e4e27e320f6f8ae44a9005b7cdce3676b85f6436
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ Rails:
2
+ Enabled: false
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/AlignParameters:
8
+ Enabled: true
9
+ EnforcedStyle: with_fixed_indentation
10
+
11
+ Style/MultilineOperationIndentation:
12
+ Enabled: true
13
+ EnforcedStyle: indented
14
+
15
+ Style/MultilineMethodCallIndentation:
16
+ Enabled: true
17
+ EnforcedStyle: indented
18
+
19
+ Style/CaseIndentation:
20
+ Enabled: true
21
+ IndentWhenRelativeTo: end
22
+
23
+ Style/SignalException:
24
+ Enabled: false
25
+
26
+ Style/FrozenStringLiteralComment:
27
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,42 @@
1
+ # How to contribute
2
+
3
+ I like to encourage you to contribute to the repository.
4
+ This should be as easy as possible for you but there are a few things to consider when contributing.
5
+ The following guidelines for contribution should be followed if you want to submit a pull request.
6
+
7
+ ## How to prepare
8
+
9
+ * You need a [GitHub account](https://github.com/signup/free)
10
+ * Submit an [issue ticket](https://github.com/handiv/handiv-ruby/issues) for your issue if the is no one yet.
11
+ * Describe the issue and include steps to reproduce if it's a bug.
12
+ * Ensure to mention the earliest version that you know is affected.
13
+ * If you are able and want to fix this, fork the repository on GitHub
14
+
15
+ ## Make Changes
16
+
17
+ * In your forked repository, create a topic branch for your upcoming patch. (e.g. `feature--autoplay` or `bugfix--ios-crash`)
18
+ * Usually this is based on the master branch.
19
+ * Create a branch based on master; `git branch
20
+ fix/master/my_contribution master` then checkout the new branch with `git
21
+ checkout fix/master/my_contribution`. Please avoid working directly on the `master` branch.
22
+ * Make sure you stick to the coding style that is used already.
23
+ * Make use of the `.editorconfig`-file if provided with the repository.
24
+ * Make commits of logical units and describe them properly.
25
+ * Check for unnecessary whitespace with `git diff --check` before committing.
26
+
27
+ * If possible, submit tests to your patch / new feature so it can be tested easily.
28
+ * Assure nothing is broken by running all the tests.
29
+
30
+ ## Submit Changes
31
+
32
+ * Push your changes to a topic branch in your fork of the repository.
33
+ * Open a pull request to the original repository and choose the right original branch you want to patch.
34
+ _Advanced users may use [`hub`](https://github.com/defunkt/hub#git-pull-request) gem for that._
35
+ * If not done in commit messages (which you really should do) please reference and update your issue with the code changes. But _please do not close the issue yourself_.
36
+ _Notice: You can [turn your previously filed issues into a pull-request here](http://issue2pr.herokuapp.com/)._
37
+ * Even if you have write access to the repository, do not directly push or merge pull-requests. Let another team member review your pull request and approve.
38
+
39
+ # Additional Resources
40
+
41
+ * [General GitHub documentation](http://help.github.com/)
42
+ * [GitHub pull request documentation](http://help.github.com/send-pull-requests/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Justas Palumickas
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,37 @@
1
+ # Handiv Ruby
2
+
3
+ A Ruby client which communicate with Handiv server.
4
+
5
+ ## Getting Started
6
+
7
+ ### Install
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'handiv'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ Handiv.configure do |config|
19
+ config.organization_id = '7b613512d9ca6545d37701efa1f2a0cc58c3b9ba'
20
+ end
21
+ ```
22
+
23
+ Report about performed task
24
+
25
+ ```ruby
26
+ Handiv.report_task('task_id')
27
+ ```
28
+
29
+ If you want to send a message about task specify an `message` argument
30
+
31
+ ```ruby
32
+ Handiv.report_task('task_id', message: 'Successfully fetched 2 records')
33
+ ```
34
+
35
+ ## License
36
+
37
+ 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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'handiv'
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
data/handiv.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'handiv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'handiv'
8
+ spec.version = Handiv::VERSION
9
+ spec.authors = ['Justas Palumickas']
10
+ spec.email = ['support@handiv.com']
11
+
12
+ spec.summary = 'A gem that provides a client interface for Handiv'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/handiv/handiv-ruby'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
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_dependency 'faraday', '>= 0.7.6'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.11'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ end
@@ -0,0 +1,61 @@
1
+ module Handiv
2
+ class Configuration
3
+ class Error < StandardError; end
4
+
5
+ DEFAULT_HOST = 'handiv.com'.freeze
6
+
7
+ # Host that will be used for connections
8
+ attr_accessor :host
9
+
10
+ attr_accessor :ssl
11
+
12
+ # General authentication data
13
+ attr_accessor :organization_id
14
+ attr_accessor :app_id
15
+ attr_accessor :secret_token
16
+
17
+ # Timeout when waiting for the server to return data in seconds
18
+ attr_accessor :timeout
19
+
20
+ # Timeout waiting for the connection to open in seconds
21
+ attr_accessor :open_timeout
22
+
23
+ # Proxy information to pass to the HTTP adapter
24
+ attr_accessor :proxy
25
+
26
+ # Logger to use internally
27
+ attr_accessor :logger
28
+
29
+ def initialize
30
+ set_default_configs
31
+ set_faraday_configs
32
+ end
33
+
34
+ def ssl?
35
+ ssl
36
+ end
37
+
38
+ def uri
39
+ scheme = ssl? ? 'https' : 'http'
40
+ "#{scheme}://#{host}"
41
+ end
42
+
43
+ def verify!
44
+ raise Error, 'No organization id specified.' unless organization_id
45
+ raise Error, 'No app id specified.' unless app_id
46
+ raise Error, 'No secret token specified.' unless secret_token
47
+ end
48
+
49
+ private
50
+
51
+ def set_default_configs
52
+ @ssl = false
53
+ @host = DEFAULT_HOST
54
+ end
55
+
56
+ def set_faraday_configs
57
+ @timeout = 2
58
+ @open_timeout = 2
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,58 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Handiv
5
+ class Connection
6
+ USER_AGENT = "handiv-ruby/#{Handiv::VERSION}".freeze
7
+
8
+ attr_reader :connection
9
+
10
+ def initialize
11
+ @connection = set_connection
12
+ end
13
+
14
+ def send_data(path, data)
15
+ config.verify!
16
+
17
+ response = post_data(path, data)
18
+ response.status
19
+ rescue => e
20
+ Handiv.logger.error "Failed to send a message (#{e.class} - #{e.message})"
21
+ 500 # Return status
22
+ end
23
+
24
+ private
25
+
26
+ def post_data(path, data)
27
+ connection.post do |req|
28
+ req.url(path)
29
+ req.headers['Content-Type'] = 'application/json'
30
+ req.headers['Authorization'] = "Token token=#{config.secret_token}"
31
+ req.body = data.to_json
32
+ end
33
+ end
34
+
35
+ def ping_url
36
+ "#{config.uri}/ping/#{config.organization_id}/#{config.app_id}"
37
+ end
38
+
39
+ def set_connection
40
+ conn = Faraday.new(url: ping_url)
41
+
42
+ config_general_options(conn)
43
+
44
+ conn
45
+ end
46
+
47
+ def config_general_options(conn)
48
+ conn.headers[:user_agent] = USER_AGENT
49
+ conn.options[:proxy] = config.proxy if config.proxy
50
+ conn.options[:timeout] = config.timeout if config.timeout
51
+ conn.options[:open_timeout] = config.open_timeout if config.open_timeout
52
+ end
53
+
54
+ def config
55
+ Handiv.configuration
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ require 'logger'
2
+
3
+ module Handiv
4
+ class Logger
5
+ LOG_PREFIX = '** [Handiv] '.freeze
6
+ LOG_LEVELS = [:fatal, :error, :warn, :info, :debug].freeze
7
+
8
+ LOG_LEVELS.each do |level|
9
+ define_method(level)do |*args, &block|
10
+ msg = args[0] # Block-level default args is a 1.9 feature
11
+ msg ||= block.call if block
12
+
13
+ logger.send(level, "#{LOG_PREFIX}#{msg}")
14
+ end
15
+ end
16
+
17
+ def logger
18
+ custom_logger || default_logger
19
+ end
20
+
21
+ private
22
+
23
+ def default_logger
24
+ @default_logger ||= ::Logger.new(STDOUT)
25
+ end
26
+
27
+ def custom_logger
28
+ Handiv.configuration.logger
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ module Handiv
2
+ module Tasks
3
+ class Reporter
4
+ attr_reader :task_id, :options
5
+
6
+ def initialize(task_id, options = {})
7
+ @task_id = task_id
8
+ @options = options
9
+ end
10
+
11
+ def report
12
+ Handiv.logger.debug "Sending event about task #{task_id}"
13
+
14
+ status = connection.send_data("tasks/#{task_id}", options)
15
+ log_status(status)
16
+
17
+ status == 200 # Return success
18
+ end
19
+
20
+ private
21
+
22
+ def log_status(status)
23
+ case status
24
+ when 200
25
+ Handiv.logger.debug "Successfully sent event about task #{task_id}"
26
+ when 404
27
+ Handiv.logger.error(
28
+ "Failed to send event for #{task_id} task.")
29
+ else
30
+ Handiv.logger.error "Failed to send event about task #{task_id}"
31
+ end
32
+ end
33
+
34
+ def connection
35
+ @connection ||= Connection.new
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Handiv
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/handiv.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'handiv/configuration'
2
+ require 'handiv/logger'
3
+ require 'handiv/connection'
4
+ require 'handiv/version'
5
+
6
+ require 'handiv/tasks/reporter'
7
+
8
+ module Handiv
9
+ class << self
10
+ def logger
11
+ @logger ||= Logger.new
12
+ end
13
+
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(configuration) if block_given?
20
+ end
21
+
22
+ def report_task(task_id, options = {})
23
+ Tasks::Reporter.new(task_id, options).report
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handiv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justas Palumickas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A gem that provides a client interface for Handiv
70
+ email:
71
+ - support@handiv.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CONTRIBUTING.md
81
+ - Gemfile
82
+ - LICENSE.md
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - handiv.gemspec
88
+ - lib/handiv.rb
89
+ - lib/handiv/configuration.rb
90
+ - lib/handiv/connection.rb
91
+ - lib/handiv/logger.rb
92
+ - lib/handiv/tasks/reporter.rb
93
+ - lib/handiv/version.rb
94
+ homepage: https://github.com/handiv/handiv-ruby
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A gem that provides a client interface for Handiv
118
+ test_files: []