headsail 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: 05f03189ed820907c20e6ea06b9b68a2a785feb5
4
+ data.tar.gz: 02ac8b3a9f57c55983a30f680f3191d99443f598
5
+ SHA512:
6
+ metadata.gz: 4487c83e555555f13af185a2f2ecdba69036b9389351049c17d1e152825e460ecba355c77ea343ae609becf29d9ce32f78f3a5159f2df2e8241d785af487e77e
7
+ data.tar.gz: ba9e96a0156ab7100384b10bb3f4db2f5dc6c4301997c9a7e39f551795a1e660f296cbe45d19f389565d84b6580657c405c5bcfe763c7a70ed90389f0c7faceb
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/.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
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at nsweeting@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in headsail.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nicholas Sweeting
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,41 @@
1
+ # Headsail
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/headsail`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'headsail'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install headsail
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/headsail. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "headsail"
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/headsail ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'headsail'
4
+ require 'headsail/cli'
5
+
6
+ cli = Headsail::CLI.new(ARGV)
7
+ cli.run
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/headsail.gemspec ADDED
@@ -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 'headsail/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "headsail"
8
+ spec.version = Headsail::VERSION
9
+ spec.authors = ["Nicholas Sweeting"]
10
+ spec.email = ["nsweeting@gmail.com"]
11
+ spec.summary = "Automated API data retrieval and storage."
12
+ spec.description = "Automated API data retrieval and storage."
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "lib"
16
+ spec.executables = ['headsail']
17
+ spec.require_paths = ["lib"]
18
+ spec.add_development_dependency "bundler", "~> 1.11"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "minitest", "~> 5.0"
21
+ spec.add_development_dependency "redis"
22
+ spec.add_development_dependency "concurrent-ruby"
23
+ spec.add_development_dependency "oauth"
24
+ end
@@ -0,0 +1,22 @@
1
+ module Headsail
2
+ class API
3
+ attr_reader :name, :timer, :method, :uri, :oauth
4
+
5
+ def initialize(config)
6
+ @name = config['name']
7
+ @timer = config['timer']
8
+ @oauth = config['oauth']
9
+ @method = config['request']['method']
10
+ @uri = build_uri(config['request'])
11
+ end
12
+
13
+ private
14
+
15
+ def build_uri(request)
16
+ return false if request.nil?
17
+ uri_full = URI(request['uri'])
18
+ uri_full.query = URI.encode_www_form(request['params']) if request['params']
19
+ uri_full
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,64 @@
1
+ require 'optparse'
2
+ require 'headsail/configure'
3
+ require 'headsail/launcher'
4
+
5
+ module Headsail
6
+ # Handles the start of Headsail via command line
7
+ #
8
+ class CLI
9
+ attr_reader :launcher
10
+
11
+ # Creates a new CLI object, parsing the arguments that are passed to it,
12
+ # and readying a Launcher to be run
13
+ #
14
+ # @param argv [Array] an array of command-line arguments
15
+ def initialize(argv)
16
+ @argv = argv
17
+ @configure = nil
18
+ @parser = nil
19
+
20
+ setup_options
21
+ parse_options
22
+ @launcher = Launcher.new(@configure)
23
+ end
24
+
25
+ def run
26
+ @launcher.run
27
+ end
28
+
29
+ private
30
+
31
+ # Builds a Configure object based on options passed through the
32
+ # command line arguments.
33
+ #
34
+ def setup_options
35
+ @configure = Headsail::Configure.new.tap do |c|
36
+ @parser = OptionParser.new do |o|
37
+
38
+ o.banner = 'Usage: bundle exec bin/headsail [options]'
39
+
40
+ o.on('-a', '--apis PATH', 'Load PATH for api yaml file') do |arg|
41
+ c.load_apis(arg)
42
+ end
43
+
44
+ o.on('-s', '--start', 'Start the Headsail client') do
45
+ c.start_client = true
46
+ end
47
+
48
+ o.on('-h', '--help', 'Prints this help') do
49
+ puts o
50
+ exit
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+
57
+ def parse_options
58
+ @parser.parse!(@argv)
59
+ rescue
60
+ print 'Unsupported Options'
61
+ exit 1
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ require 'headsail/task'
2
+
3
+ module Headsail
4
+ # The main event loop for retrieving data from APIS
5
+ #
6
+ class Client
7
+ def initialize(configure)
8
+ @configure = configure
9
+ @threads = []
10
+ @tasks = []
11
+ setup_tasks
12
+ end
13
+
14
+ def run
15
+ @tasks.each { |task| @threads << task.run }
16
+ @threads.map(&:execute)
17
+ loop { sleep 300 }
18
+ end
19
+
20
+ private
21
+
22
+ def setup_tasks
23
+ @configure.apis.each { |api| @tasks << Task.new(api) }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ require 'yaml'
2
+
3
+ module Headsail
4
+ class Configure
5
+ attr_accessor :start_client
6
+ attr_reader :apis
7
+
8
+ def load_apis(path)
9
+ @apis = YAML.load_file(path)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ require 'logger'
2
+
3
+ module Headsail
4
+ class Event
5
+ def initialize(event_name)
6
+ @event_name = event_name
7
+ @logger = Logger.new(STDOUT)
8
+ end
9
+
10
+ def request(http, method)
11
+ message = "#{@event_name} / #{method} REQUEST / #{http.code} - #{http.code_status.to_s}"
12
+ @logger.info(message)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ require 'headsail/oauth'
2
+
3
+ module Headsail
4
+ class HTTP
5
+ attr_reader :response
6
+
7
+ def initialize(api)
8
+ @api = api
9
+ @response = nil
10
+ @oauth = determine_oauth
11
+ @request = determine_request
12
+ end
13
+
14
+ def run
15
+ send(@request)
16
+ end
17
+
18
+ def body
19
+ @response.body
20
+ end
21
+
22
+ def code
23
+ @response.code
24
+ end
25
+
26
+ def code_status
27
+ case @response.code.to_i
28
+ when 200 || 201
29
+ :SUCCESS
30
+ when (400..499)
31
+ :BAD_REQUEST
32
+ when (500..599)
33
+ :SERVER_PROBLEMS
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def get_request_no_oauth
40
+ @response = Net::HTTP.get_response(@api.uri)
41
+ code_status == :SUCCESS
42
+ end
43
+
44
+ def get_request_with_oauth
45
+ @response = @oauth.access_token.get(@api.uri.to_s)
46
+ code_status == :SUCCESS
47
+ end
48
+
49
+ def determine_request
50
+ case
51
+ when @api.method == 'GET' && @oauth
52
+ :get_request_with_oauth
53
+ when @api.method == 'GET' && @oauth == false
54
+ :get_request_no_oauth
55
+ end
56
+ end
57
+
58
+ def determine_oauth
59
+ @api.oauth ? OAuth.new(@api.oauth) : false
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ require 'headsail/client'
2
+
3
+ module Headsail
4
+ # Determines the action to be taken based on the Configure object
5
+ #
6
+ class Launcher
7
+ attr_reader :client
8
+
9
+ def initialize(configure)
10
+ @configure = configure
11
+ end
12
+
13
+ def run
14
+ case
15
+ when @configure.start_client
16
+ start_client
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ # Creates a new Client object - the main process used for data collection
23
+ #
24
+ def start_client
25
+ @client = Client.new(@configure)
26
+ @client.run
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'oauth'
2
+
3
+ module Headsail
4
+ class OAuth
5
+ attr_reader :access_token
6
+
7
+ def initialize(oauth)
8
+ return false if oauth.nil?
9
+ @oauth = oauth
10
+ @access_token = oauth_token
11
+ end
12
+
13
+ private
14
+
15
+ def oauth_token
16
+ ::OAuth::AccessToken.new(consumer, @oauth['access_token'], @oauth['access_token_secret'])
17
+ end
18
+
19
+ def consumer
20
+ ::OAuth::Consumer.new(@oauth['consumer_key'], @oauth['consumer_secret'], site: @oauth['site'])
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ require 'redis'
2
+
3
+ module Headsail
4
+ class Redis
5
+ attr_reader :connection
6
+
7
+ def initialize(key_name)
8
+ @key_name = key_name
9
+ setup_connection
10
+ setup_service
11
+ setup_next_id
12
+ end
13
+
14
+ def add(data)
15
+ @connection.incr(next_id_key)
16
+ @connection.set(new_key, data)
17
+ end
18
+
19
+ private
20
+
21
+ def new_key
22
+ "#{@key_name}:#{next_id}"
23
+ end
24
+
25
+ def next_id
26
+ @connection.get(next_id_key)
27
+ end
28
+
29
+ def next_id_key
30
+ @next_id_key ||= "#{@key_name}:next_id"
31
+ end
32
+
33
+ def setup_connection
34
+ @connection = ::Redis.new(url: ENV['REDIS_URL'])
35
+ end
36
+
37
+ def setup_service
38
+ return if @connection.exists(@key_name)
39
+ @connection.set(@key_name, true)
40
+ @connection.lpush('apis', @key_name)
41
+ end
42
+
43
+ def setup_next_id
44
+ @connection.set(next_id_key, 0) unless @connection.exists(next_id_key)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ require 'concurrent'
2
+ require 'headsail/api'
3
+ require 'headsail/event'
4
+ require 'headsail/http'
5
+ require 'headsail/redis'
6
+
7
+ module Headsail
8
+ class Task
9
+ def initialize(config)
10
+ @api = API.new(config)
11
+ @redis = Redis.new(@api.name)
12
+ @http = HTTP.new(@api)
13
+ @event = Event.new(@api.name)
14
+ end
15
+
16
+ def run
17
+ Concurrent::TimerTask.new(execution_interval: @api.timer) do
18
+ @redis.add(@http.body) if @http.run
19
+ @event.request(@http, @api.method)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Headsail
2
+ VERSION = "0.1.0"
3
+ end
data/lib/headsail.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'headsail/version'
2
+ require 'byebug'
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: headsail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Sweeting
8
+ autorequire:
9
+ bindir: lib
10
+ cert_chain: []
11
+ date: 2016-06-08 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
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: concurrent-ruby
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
+ - !ruby/object:Gem::Dependency
84
+ name: oauth
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Automated API data retrieval and storage.
98
+ email:
99
+ - nsweeting@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/headsail
113
+ - bin/setup
114
+ - headsail.gemspec
115
+ - lib/headsail.rb
116
+ - lib/headsail/api.rb
117
+ - lib/headsail/cli.rb
118
+ - lib/headsail/client.rb
119
+ - lib/headsail/configure.rb
120
+ - lib/headsail/event.rb
121
+ - lib/headsail/http.rb
122
+ - lib/headsail/launcher.rb
123
+ - lib/headsail/oauth.rb
124
+ - lib/headsail/redis.rb
125
+ - lib/headsail/task.rb
126
+ - lib/headsail/version.rb
127
+ homepage:
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.5.0
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Automated API data retrieval and storage.
151
+ test_files: []