everdeen 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: c997fb0070b01ea8496cd8ba1a0593d9c743575c
4
+ data.tar.gz: 3eda610abe2462e8ed36569ddd45c7527a589c93
5
+ SHA512:
6
+ metadata.gz: 7d93b51377e7f4566263aed88b1a7815bcd67ad60bdadd0d173dbfdb82cc10ccb7fbb6edad3b9f36444cc3ac5427194d4104c33b70c272147ac627b86c9d780d
7
+ data.tar.gz: cce415b1a74572276e7c0ddab2082a71e83beafe69237ba8d731e7267906a6ceb8e97c0ae5a187752ef2efba8b9abd5c3b4dbb3cb4629f760ca842556ac15aaf
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ binaries
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in test.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ everdeen (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (11.1.2)
11
+ rspec (3.4.0)
12
+ rspec-core (~> 3.4.0)
13
+ rspec-expectations (~> 3.4.0)
14
+ rspec-mocks (~> 3.4.0)
15
+ rspec-core (3.4.4)
16
+ rspec-support (~> 3.4.0)
17
+ rspec-expectations (3.4.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.4.0)
20
+ rspec-mocks (3.4.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.4.0)
23
+ rspec-support (3.4.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.8)
30
+ everdeen!
31
+ rake (~> 11)
32
+ rspec (~> 3.4)
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jon Normington
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,39 @@
1
+ # Everdeen
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/everdeen`. 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 'everdeen'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install everdeen
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 `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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/everdeen/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "everdeen"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/everdeen.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'everdeen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "everdeen"
8
+ spec.version = Everdeen::VERSION
9
+ spec.authors = ['Daniel Upton', 'Jon Normington']
10
+ spec.email = ['daniel.upton@geckoboard.com', 'jon@geckoboard.com']
11
+
12
+ spec.summary = %q{ Everdeen ruby client to setup expectation requests through the API }
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/geckoboard/everdeen"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + Dir['binaries/*']
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rspec", "~> 3.4"
24
+ spec.add_development_dependency "rake", "~> 11"
25
+ end
@@ -0,0 +1,44 @@
1
+ module Everdeen
2
+ class Client
3
+ attr_reader :control_addr
4
+
5
+ def initialize(control_addr)
6
+ @control_addr = control_addr
7
+ end
8
+
9
+ def ping
10
+ Net::HTTP.get build_uri('/ping')
11
+ end
12
+
13
+ def registered_expectations
14
+ response = Net::HTTP.get build_uri('/expectations')
15
+ JSON.parse(response)
16
+ end
17
+
18
+ def requests(expectation_uuid)
19
+ response = Net::HTTP.get build_uri("/expectations/#{expectation_uuid}/requests")
20
+ JSON.parse(response)
21
+ end
22
+
23
+ def create_expectations(expectations)
24
+ uri = build_uri('/expectations')
25
+
26
+ request = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' })
27
+ request.body = {
28
+ expectations: expectations.map(&:to_hash)
29
+ }.to_json
30
+
31
+ Net::HTTP.start(uri.host, uri.port) do |http|
32
+ JSON.parse(http.request(request).body)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def build_uri(path)
39
+ uri = control_addr.dup
40
+ uri.path = path
41
+ uri
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ module Everdeen
2
+ class Criterion
3
+ attr_reader :key, :type, :value
4
+
5
+ def initialize(args = {})
6
+ args.each do |key, value|
7
+ instance_variable_set("@#{key}", value)
8
+ end
9
+ end
10
+
11
+ def match_type
12
+ @match_type || 'exact'
13
+ end
14
+
15
+ def to_hash
16
+ base = { key: key, match_type: match_type, type: type }
17
+
18
+ if query_param_type? && value.is_a?(Array)
19
+ base.merge(values: value)
20
+ else
21
+ base.merge(value: value)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def query_param_type?
28
+ type == 'query_param'
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ module Everdeen
2
+ class Expectation
3
+ attr_reader :uuid, :max_matches, :response, :request_criteria
4
+
5
+ def initialize(args = {})
6
+ args.each do |key, value|
7
+ next if ['response', 'request_criteria'].include?(key.to_s)
8
+ instance_variable_set("@#{key}", value)
9
+ end
10
+
11
+ add_response(args[:response] || args['response'])
12
+ add_request(args[:request_criteria] || args['request_criteria'])
13
+ end
14
+
15
+ def pass_through
16
+ !!@pass_through
17
+ end
18
+
19
+ def store_matching_requests
20
+ !!@store_matching_requests
21
+ end
22
+
23
+ def to_hash
24
+ {
25
+ store_matching_requests: store_matching_requests,
26
+ max_matches: max_matches,
27
+ pass_through: pass_through,
28
+ request_criteria: request_criteria.to_hash,
29
+ respond_with: response.to_hash
30
+ }
31
+ end
32
+
33
+ private
34
+
35
+ def add_response(response_attr)
36
+ @response = Response.new(response_attr)
37
+ end
38
+
39
+ def add_request(request_attrs)
40
+ @request_criteria = RequestCriteria.new(request_attrs)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ require 'base64'
2
+
3
+ module Everdeen
4
+ class Request
5
+ attr_reader :body_base64, :headers, :method, :url
6
+
7
+ def initialize(args = {})
8
+ @body_base64 = args['body_base64']
9
+ @headers = args['headers']
10
+ @method = args['method']
11
+ @url = args['url']
12
+ end
13
+
14
+ def body
15
+ Base64.decode64(body_base64)
16
+ end
17
+
18
+ def ==(diff_req)
19
+ return false unless diff_req.is_a?(Everdeen::Request)
20
+ attributes == diff_req.attributes
21
+ end
22
+
23
+ def attributes
24
+ {
25
+ url: url,
26
+ method: method,
27
+ headers: headers,
28
+ body_base64: body_base64
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ module Everdeen
2
+ class RequestCriteria
3
+ def initialize(criterion)
4
+ Array(criterion).each do |criterion_attr|
5
+ add_criterion(criterion_attr)
6
+ end
7
+ end
8
+
9
+ def criteria
10
+ @criteria ||= []
11
+ end
12
+
13
+ def to_hash
14
+ criteria.map(&:to_hash)
15
+ end
16
+
17
+ private
18
+
19
+ def add_criterion(criterion_attrs)
20
+ criteria << Criterion.new(criterion_attrs)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module Everdeen
2
+ class Response
3
+ attr_reader :status, :headers, :body
4
+
5
+ def initialize(args = {})
6
+ Hash(args).each do |key, value|
7
+ instance_variable_set("@#{key}", value)
8
+ end
9
+ end
10
+
11
+ def body_encoding
12
+ @body_encoding.to_s
13
+ end
14
+
15
+ def to_hash
16
+ {
17
+ status: status,
18
+ headers: headers,
19
+ body: body,
20
+ body_encoding: body_encoding
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,87 @@
1
+ require 'tempfile'
2
+
3
+ module Everdeen
4
+ class Server
5
+ StartTimeout = Class.new(StandardError)
6
+
7
+ def self.start(opts = {})
8
+ server = new(opts)
9
+ server.start
10
+ server
11
+ end
12
+
13
+ attr_reader :proxy_port, :control_port, :stderr
14
+
15
+ def initialize(opts = {})
16
+ @proxy_port = opts.fetch(:proxy_port)
17
+ @control_port = opts.fetch(:control_port)
18
+ @stderr = opts.fetch(:stderr, Tempfile.new("everdeen"))
19
+ end
20
+
21
+ def start
22
+ pid = spawn_process.pid
23
+ wait_until_ready
24
+ pid
25
+ end
26
+
27
+ def create_expectations(expectations)
28
+ client.create_expectations(expectations).collect { |exp| Expectation.new(exp) }
29
+ end
30
+
31
+ def registered_expectations
32
+ client.registered_expectations
33
+ end
34
+
35
+ def requests(expectation_id)
36
+ client.requests(expectation_id)['requests'].collect { |req| Request.new(req) }
37
+ end
38
+
39
+ def stop
40
+ Process.kill(:INT, @pipe.pid)
41
+ end
42
+
43
+ def ping
44
+ client.ping
45
+ end
46
+
47
+ private
48
+
49
+ def spawn_process
50
+ @pipe = IO.popen([
51
+ Everdeen.bin_path,
52
+ "-proxy-addr=#{proxy_addr}",
53
+ "-control-addr=#{control_addr}"
54
+ ], err: stderr)
55
+ end
56
+
57
+ def wait_until_ready
58
+ 10.times do
59
+ return if ready?
60
+ sleep 0.1
61
+ end
62
+
63
+ stop
64
+ raise StartTimeout
65
+ end
66
+
67
+ def ready?
68
+ ping == 'PONG'
69
+ rescue Errno::ECONNREFUSED
70
+ false
71
+ end
72
+
73
+ def proxy_addr
74
+ "127.0.0.1:#{proxy_port}"
75
+ end
76
+
77
+ def control_addr
78
+ "127.0.0.1:#{control_port}"
79
+ end
80
+
81
+ def client
82
+ @_client ||= Client.new(
83
+ URI.parse("http://#{control_addr}")
84
+ )
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,4 @@
1
+ module Everdeen
2
+ VERSION = "0.1.0"
3
+ SERVER_VERSION = "0.1.0"
4
+ end
data/lib/everdeen.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ require 'everdeen/version'
6
+ require 'everdeen/criterion'
7
+ require 'everdeen/response'
8
+ require 'everdeen/request_criteria'
9
+ require 'everdeen/expectation'
10
+ require 'everdeen/server'
11
+ require 'everdeen/client'
12
+ require 'everdeen/request'
13
+
14
+ module Everdeen
15
+ SUPPORTED_PLATFORMS = %w[x86_64-linux-gnu]
16
+ BINARIES_DIR = File.expand_path('../../binaries', __FILE__)
17
+
18
+ class UnsupportedPlatformError < StandardError
19
+ def initialize
20
+ super "Everdeen does not support this architecture: #{RUBY_PLATFORM}"
21
+ end
22
+ end
23
+
24
+ def self.bin_path
25
+ raise UnsupportedPlatformError unless SUPPORTED_PLATFORMS.include? RUBY_PLATFORM
26
+ File.join(BINARIES_DIR, "everdeen_#{Everdeen::SERVER_VERSION}_#{RUBY_PLATFORM}")
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: everdeen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Upton
8
+ - Jon Normington
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-06-23 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.8'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.8'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.4'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.4'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '11'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '11'
56
+ description: Everdeen ruby client to setup expectation requests through the API
57
+ email:
58
+ - daniel.upton@geckoboard.com
59
+ - jon@geckoboard.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - binaries/everdeen_0.1.0_x86_64-linux-gnu
74
+ - everdeen.gemspec
75
+ - lib/everdeen.rb
76
+ - lib/everdeen/client.rb
77
+ - lib/everdeen/criterion.rb
78
+ - lib/everdeen/expectation.rb
79
+ - lib/everdeen/request.rb
80
+ - lib/everdeen/request_criteria.rb
81
+ - lib/everdeen/response.rb
82
+ - lib/everdeen/server.rb
83
+ - lib/everdeen/version.rb
84
+ homepage: https://github.com/geckoboard/everdeen
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.0
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Everdeen ruby client to setup expectation requests through the API
108
+ test_files: []