smokey_bear 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ require 'multi_json'
2
+ require 'timeout'
3
+ require 'rest_client'
4
+ require 'delegate'
5
+
6
+ module SmokeyBear
7
+ module Helpers
8
+
9
+ class ResponseDelegate < DelegateClass(RestClient::Response)
10
+
11
+ def has_status?(expected_code)
12
+ code == expected_code
13
+ end
14
+
15
+ def json?
16
+ headers[:content_type] =~ /\bjson\b/
17
+ end
18
+
19
+ end
20
+
21
+ def get(*args); perform_request(:get, *args); end
22
+ def post(*args); perform_request(:post, *args); end
23
+ def delete(*args); perform_request(:delete, *args); end
24
+ def put(*args); perform_request(:put, *args); end
25
+ def head(*args); perform_request(:head, *args); end
26
+
27
+ def response
28
+ @response ||= (raise "Please make sure you perform a request first")
29
+ end
30
+
31
+ def decoded_response
32
+ @decoded_response ||= MultiJson.load(response.to_str)
33
+ end
34
+
35
+ def headers
36
+ response.headers
37
+ end
38
+
39
+ private
40
+
41
+ def perform_request(*args)
42
+ Timeout.timeout SmokeyBear.timeout do
43
+ perform_raw_request(*args)
44
+ end
45
+ end
46
+
47
+ def perform_raw_request(method, path, options = {})
48
+ url = File.join(SmokeyBear.base_url)
49
+ endpoint = options.delete(:endpoint)
50
+ endpoint ||= SmokeyBear.endpoint unless endpoint == false
51
+ url = File.join(url, endpoint) unless endpoint == false
52
+ url = File.join(url, path)
53
+ if [:post, :put, :delete].include?(method)
54
+ response = RestClient.send method, url, MultiJson.dump(options), :content_type => :json, :accept => :json
55
+ else
56
+ url << "?#{options.to_param}" unless options.empty?
57
+ response = RestClient.send method, url, :accept => :json
58
+ end
59
+ @response = ResponseDelegate.new response
60
+ rescue RestClient::Exception => e
61
+ @response = e.response
62
+ end
63
+
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module SmokeyBear
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/smokey_bear.rb CHANGED
@@ -1,9 +1,31 @@
1
1
  require "smokey_bear/version"
2
+ require "active_support"
3
+ require "active_support/core_ext"
2
4
 
3
5
  module SmokeyBear
4
6
 
5
- class Configuration
6
- attr_accessor :domain, :endpoint
7
+ class << self
8
+
9
+ require 'smokey_bear/helpers'
10
+
11
+ attr_accessor :domain, :endpoint, :https, :timeout
12
+
13
+ def configure
14
+ yield self if block_given?
15
+ end
16
+
17
+ def build_base_url
18
+ "#{https ? "https" : "http"}://#{domain}/"
19
+ end
20
+
21
+ def base_url
22
+ @base_url ||= build_base_url
23
+ end
24
+
7
25
  end
8
26
 
27
+ self.endpoint = nil
28
+ self.https = false
29
+ self.timeout = 15
30
+
9
31
  end
data/smokey_bear.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Darcy Laycock"]
6
6
  gem.email = ["sutto@sutto.net"]
7
7
  gem.description = %q{Tools for testing JSON-based APIs}
8
- gem.summary = %q{Smokey Bear provides a set of tools orentied towards making it easy to test HTTP and JSON based APIS.}
8
+ gem.summary = %q{Smokey Bear provides a set of tools oriented towards making it easy to test HTTP and JSON based APIS.}
9
9
  gem.homepage = ""
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -17,4 +17,7 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_dependency 'rest-client'
19
19
  gem.add_dependency 'multi_json'
20
+ gem.add_dependency 'active_support', '~> 3.0'
21
+ gem.add_dependency 'i18n'
22
+ gem.add_dependency 'rest-client'
20
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smokey_bear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,6 +43,54 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: active_support
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: i18n
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rest-client
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
46
94
  description: Tools for testing JSON-based APIs
47
95
  email:
48
96
  - sutto@sutto.net
@@ -82,6 +130,6 @@ rubyforge_project:
82
130
  rubygems_version: 1.8.24
83
131
  signing_key:
84
132
  specification_version: 3
85
- summary: Smokey Bear provides a set of tools orentied towards making it easy to test
133
+ summary: Smokey Bear provides a set of tools oriented towards making it easy to test
86
134
  HTTP and JSON based APIS.
87
135
  test_files: []