runscope-rb 0.0.1

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
+ data.tar.gz: 205a1ecd9e1143f1625c8f566f3442ef3f8fe925
4
+ metadata.gz: 751eb3188abc34ad5b2e37126699aca22b185eb2
5
+ SHA512:
6
+ data.tar.gz: 35a8b832d41be93d5c19d514140563afa4d815d07f1fa3be9f670499785d38d65ee544b1928e157f3712df51f21cc5918e2d3dbb292a8d006c81796e982be72d
7
+ metadata.gz: c52460de1cbd0082ca1298c6e0baf2583b9bd9d9f9835e257a0a9b4de81f7e4b3801078853005a805cbce613e5fb05f42403e30aa5510d7ce3ad858fc46d50b9
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,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christopher Warren
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,56 @@
1
+ # runscope-rb
2
+
3
+ A gem to quickly get up and running with Runscope in a few lines of code.
4
+
5
+ runscope-rb currently supports Net::HTTP requests. Requests made using other HTTP libraries, such as libcurl or sockets, won't be modified. If you want to add support for another HTTP library that would be fantastic. See the section on Contributing at the end of the readme.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'runscope', '~> 0.0.1'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install runscope
20
+
21
+ ## Usage
22
+
23
+ Obviously you'll need a [Runscope](http://runscope.com) account to use this. Then just run the configure block below within your code. If you're making a Rails app you probably want to put it in config/initializers/runscope.rb.
24
+
25
+ Runscope.configure do |config|
26
+ config.bucket = "abcd1234"
27
+ config.domains = ["api.stackexchange.com", "api.github.com", /\S+\.desk\.com/]
28
+ end
29
+
30
+ ## Options
31
+
32
+ There are a few options to set:
33
+
34
+ `bucket` *Required*
35
+
36
+ A string for the bucket key from Runscope. Default is `nil`.
37
+
38
+ `domains` *Required*
39
+
40
+ An array of strings and regular expressions to match outbound traffic against. Any requests through Net::HTTP that don't match one of these will pass through unaltered. Default is `[]`.
41
+
42
+ `enabled`
43
+
44
+ A boolean to control if Runscope is used at all. Set to `false` to stop using Runscope. Default is `true`.
45
+
46
+ ## Troubleshooting
47
+
48
+ If your requests aren't being sent through Runscope after you're set up, check how the request is being made. If it's not using Net::HTTP it won't work at this time. Check the section below on Contributing if you want to add support for another HTTP library.
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,17 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+
4
+ module Net
5
+ class HTTP
6
+
7
+ def request_with_runscope(request, body=nil, &block)
8
+ if ::Runscope.monitor?(self.address)
9
+ @address = ::Runscope.proxy_domain(self.address)
10
+ end
11
+ request_without_runscope(request, body, &block)
12
+ end
13
+ alias_method :request_without_runscope, :request
14
+ alias_method :request, :request_with_runscope
15
+
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ module Runscope
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :bucket,
5
+ :domains,
6
+ :enabled
7
+ ]
8
+
9
+ # By default, use Runscope
10
+ DEFAULT_ENABLED = true
11
+
12
+ # By default, send no traffic to Runscope
13
+ DEFAULT_DOMAINS = []
14
+
15
+ # By default, don't set a bucket
16
+ DEFAULT_BUCKET = nil
17
+
18
+ attr_accessor *VALID_OPTIONS_KEYS
19
+
20
+ # When this module is extended, set all configuration options to their default values
21
+ def self.extended(base)
22
+ base.reset
23
+ end
24
+
25
+ # Convenience method to allow configuration options to be set in a block
26
+ def configure
27
+ yield self
28
+ end
29
+
30
+ # Create a hash of options and their values
31
+ def options
32
+ Hash[VALID_OPTIONS_KEYS.map {|key| [key, send(key)] }]
33
+ end
34
+
35
+ def reset
36
+ self.bucket = DEFAULT_BUCKET
37
+ self.domains = DEFAULT_DOMAINS
38
+ self.enabled = DEFAULT_ENABLED
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Runscope
2
+ VERSION = "0.0.1"
3
+ end
data/lib/runscope.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "runscope/version"
2
+ require "runscope/configuration"
3
+ require "ext/net_http"
4
+
5
+ module Runscope
6
+ extend Configuration
7
+
8
+ def self.version_string
9
+ "Runscope version #{Runscope::VERSION}"
10
+ end
11
+
12
+ def self.monitor?(address)
13
+ enabled? && monitor_domain?(address)
14
+ end
15
+
16
+ def self.monitor_domain?(address)
17
+ raise NoDomainsSetError unless domains
18
+ domains.any? do |domain|
19
+ if domain.is_a?(String)
20
+ address == domain
21
+ elsif domain.is_a?(Regexp)
22
+ address =~ domain
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.proxy_domain(address)
28
+ raise BucketNotSetError unless bucket
29
+ subdomain = address.gsub(".","-")
30
+ "#{subdomain}-#{bucket}.runscope.net"
31
+ end
32
+
33
+ def self.enabled?
34
+ Runscope.enabled
35
+ end
36
+ end
37
+
38
+ class RunscopeError < StandardError; end
39
+ class BucketNotSetError < RunscopeError; end
40
+ class NoDomainsSetError < RunscopeError; end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'runscope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "runscope-rb"
8
+ spec.version = Runscope::VERSION
9
+ spec.authors = ["Chris Warren"]
10
+ spec.email = ["chris@expectless.com"]
11
+ spec.description = %q{Start using Runscope to monitor your API traffic in minutes}
12
+ spec.summary = %q{Start using Runscope to monitor your API traffic in minutes}
13
+ spec.homepage = "http://github.com/chriswarren/runscope-rb"
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_development_dependency "rspec", "~>2.13.0"
24
+ spec.add_development_dependency "fakeweb", "~> 1.3.0"
25
+ spec.add_development_dependency "simplecov"
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Net::HTTP do
4
+ context "outgoing requests" do
5
+ before(:each) do
6
+ @domains = ["api.stackexchange.com", /\S+\.desk\.com/]
7
+ @bucket = "1234abcd"
8
+
9
+ Runscope.configure do |config|
10
+ config.domains = @domains
11
+ config.bucket = @bucket
12
+ end
13
+ end
14
+
15
+ after(:each) do
16
+ Runscope.reset
17
+ end
18
+
19
+ context "to a url monitored by runscope" do
20
+ it "should modify a to a url that matches a string" do
21
+ FakeWeb.register_uri(:get, "http://api-stackexchange-com-#{@bucket}.runscope.net", :body => "test")
22
+ Net::HTTP.get_response(URI.parse("http://api.stackexchange.com"))
23
+ end
24
+
25
+ it "should modify a request to a url that matches a regexp" do
26
+ FakeWeb.register_uri(:get, "http://test-desk-com-#{@bucket}.runscope.net", :body => "test")
27
+ Net::HTTP.get_response(URI.parse("http://test.desk.com"))
28
+ end
29
+ end
30
+
31
+ context "to a url not monitored by runscope" do
32
+ it "should not modify a request to a url that doesn't match" do
33
+ FakeWeb.register_uri(:get, "http://api.twitter.com", :body => "test")
34
+ Net::HTTP.get_response(URI.parse("http://api.twitter.com"))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Runscope::Configuration do
4
+ after(:each) do
5
+ Runscope.reset
6
+ end
7
+
8
+ it 'should set options when passed a block' do
9
+ domains = ["api.stackexchange.com", /\S+\.desk\.com/]
10
+ bucket = "1234abcd"
11
+
12
+ Runscope.configure do |config|
13
+ config.domains = domains
14
+ config.bucket = bucket
15
+ end
16
+
17
+ Runscope.enabled.should eq(true)
18
+ Runscope.domains.should eq(domains)
19
+ Runscope.bucket.should eq(bucket)
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Runscope do
4
+ context "basic methods" do
5
+ it 'should return correct version string' do
6
+ Runscope.version_string.should eq("Runscope version #{Runscope::VERSION}")
7
+ end
8
+ end
9
+
10
+ context 'monitoring' do
11
+ before(:each) do
12
+ Runscope.domains = ["api.stackexchange.com", /\S+\.desk\.com/]
13
+ end
14
+
15
+ after(:each) do
16
+ Runscope.reset
17
+ end
18
+
19
+ context "when enabled" do
20
+ before(:each) do
21
+ Runscope.enabled = true
22
+ end
23
+
24
+ it "should be true if the domain is monitored" do
25
+ Runscope.monitor?("api.stackexchange.com").should eq(true)
26
+ end
27
+
28
+ it "should be false if the domain is not monitored" do
29
+ Runscope.monitor?("api.twitter.com").should eq(false)
30
+ end
31
+ end
32
+
33
+ context "when disabled" do
34
+ before(:each) do
35
+ Runscope.enabled = false
36
+ end
37
+
38
+ it "should be false if the domain is monitored" do
39
+ Runscope.monitor?("api.stackexchange.com").should eq(false)
40
+ end
41
+
42
+ it "should be false if the domain is not monitored" do
43
+ Runscope.monitor?("api.twitter.com").should eq(false)
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'matching domains' do
49
+ before(:each) do
50
+ Runscope.domains = ["api.stackexchange.com", /\S+\.desk\.com/]
51
+ end
52
+
53
+ it "should match when stored as a string" do
54
+ Runscope.monitor_domain?("api.stackexchange.com").should eq(true)
55
+ end
56
+
57
+ it "should match when stored as a Regexp" do
58
+ Runscope.monitor_domain?("test.desk.com").should eq(true)
59
+ end
60
+
61
+ it "should not match domains that aren't set" do
62
+ Runscope.monitor_domain?("api.twitter.com").should eq(false)
63
+ end
64
+
65
+ it "should raise an error if no domains are set" do
66
+ Runscope.domains = nil
67
+ expect {
68
+ Runscope.monitor_domain?("api.stackexchange.com")
69
+ }.to raise_error(NoDomainsSetError)
70
+ end
71
+ end
72
+
73
+ context 'generating a runscope proxy domain' do
74
+ before(:each) do
75
+ @bucket = "1234abcd"
76
+ Runscope.bucket = @bucket
77
+ end
78
+
79
+ it "should return a domain to runscope" do
80
+ Runscope.proxy_domain("api.stackexchange.com").should
81
+ eq("api-stackexchange-com-#{@bucket}.runscope.net")
82
+ end
83
+
84
+ it "should raise an error if no bucket is set" do
85
+ Runscope.bucket = nil
86
+ expect {
87
+ Runscope.proxy_domain("api.stackexchange.com")
88
+ }.to raise_error(BucketNotSetError)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rspec'
5
+ require 'fakeweb'
6
+ require 'runscope'
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.formatter = 'documentation'
11
+ config.order = "random"
12
+ end
13
+
14
+ FakeWeb.allow_net_connect = false
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runscope-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Warren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-06-03 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: "1.3"
22
+ type: :development
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: rake
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - &id005
30
+ - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id002
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id003 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: 2.13.0
43
+ type: :development
44
+ version_requirements: *id003
45
+ - !ruby/object:Gem::Dependency
46
+ name: fakeweb
47
+ prerelease: false
48
+ requirement: &id004 !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: 1.3.0
53
+ type: :development
54
+ version_requirements: *id004
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ prerelease: false
58
+ requirement: &id006 !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - *id005
61
+ type: :development
62
+ version_requirements: *id006
63
+ description: Start using Runscope to monitor your API traffic in minutes
64
+ email:
65
+ - chris@expectless.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - lib/ext/net_http.rb
79
+ - lib/runscope.rb
80
+ - lib/runscope/configuration.rb
81
+ - lib/runscope/version.rb
82
+ - runscope-rb.gemspec
83
+ - spec/ext/net_http_spec.rb
84
+ - spec/runscope/configuration_spec.rb
85
+ - spec/runscope_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: http://github.com/chriswarren/runscope-rb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - *id005
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - *id005
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Start using Runscope to monitor your API traffic in minutes
110
+ test_files:
111
+ - spec/ext/net_http_spec.rb
112
+ - spec/runscope/configuration_spec.rb
113
+ - spec/runscope_spec.rb
114
+ - spec/spec_helper.rb