statraptor 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- statraptor (0.1.0)
4
+ statraptor (0.2.0)
5
5
  activeresource (>= 2.3.5)
6
6
  json (>= 1.4.6)
7
+ rest-client (>= 1.6.7)
7
8
  typhoeus (>= 0.3.3)
8
9
 
9
10
  GEM
@@ -31,10 +32,14 @@ GEM
31
32
  guard (>= 0.10.0)
32
33
  i18n (0.6.0)
33
34
  json (1.6.5)
35
+ macaddr (1.5.0)
36
+ systemu (>= 2.4.0)
34
37
  mime-types (1.17.2)
35
- multi_json (1.0.4)
38
+ multi_json (1.1.0)
36
39
  rake (0.9.2.2)
37
40
  rb-fsevent (0.9.0)
41
+ rest-client (1.6.7)
42
+ mime-types (>= 1.16)
38
43
  rspec (2.8.0)
39
44
  rspec-core (~> 2.8.0)
40
45
  rspec-expectations (~> 2.8.0)
@@ -43,9 +48,12 @@ GEM
43
48
  rspec-expectations (2.8.0)
44
49
  diff-lcs (~> 1.1.2)
45
50
  rspec-mocks (2.8.0)
51
+ systemu (2.4.2)
46
52
  thor (0.14.6)
47
53
  typhoeus (0.3.3)
48
54
  mime-types
55
+ uuid (2.3.5)
56
+ macaddr (~> 1.0)
49
57
  vcr (2.0.0.rc1)
50
58
  webmock (1.7.10)
51
59
  addressable (~> 2.2, > 2.2.5)
@@ -61,5 +69,6 @@ DEPENDENCIES
61
69
  rb-fsevent (~> 0.9.0)
62
70
  rspec (~> 2.8.0)
63
71
  statraptor!
72
+ uuid (~> 2.3.5)
64
73
  vcr (= 2.0.0.rc1)
65
74
  webmock (= 1.7.10)
@@ -0,0 +1,24 @@
1
+ module StatRaptor
2
+ class Client
3
+ module Adapters
4
+ module RestClient
5
+ def self.request_api_response(method, path, params = {})
6
+ params.merge!(:platform_credentials => StatRaptor.platform_credentials)
7
+ verify_ssl = StatRaptor.disable_ssl_peer_verification ? false : true
8
+ url = StatRaptor.full_uri(path)
9
+
10
+ options = {
11
+ :url => url,
12
+ :method => method,
13
+ :payload => params,
14
+ :verify_ssl => StatRaptor.disable_ssl_peer_verification,
15
+ :timeout => StatRaptor.timeout_in_seconds,
16
+ :headers => Config::HTTP_HEADERS
17
+ }
18
+
19
+ ::RestClient::Request.execute(options){|response, request, result| response }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module StatRaptor
2
+ class Client
3
+ module Adapters
4
+ module Typhoeus
5
+ def self.request_api_response(method, path, params = {})
6
+ params.merge!(:platform_credentials => StatRaptor.platform_credentials)
7
+
8
+ ::Typhoeus::Request.run(
9
+ "#{StatRaptor.endpoint}/api/v1#{path}",
10
+ :method => method,
11
+ :params => params,
12
+ :disable_ssl_peer_verification => StatRaptor.disable_ssl_peer_verification,
13
+ :timeout => StatRaptor.timeout
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -20,7 +20,7 @@ module StatRaptor
20
20
  private
21
21
 
22
22
  def graph_data(type, params = {})
23
- get("/graphs/#{type}.json", params)
23
+ get("/api/v1/graphs/#{type}.json", params)
24
24
  end
25
25
  end
26
26
  end
@@ -2,11 +2,11 @@ module StatRaptor
2
2
  class Client
3
3
  module Projects
4
4
  def create_project(params = {})
5
- post("/projects", params)
5
+ post("/api/v1/projects", params)
6
6
  end
7
7
 
8
8
  def delete_project(params = {})
9
- delete("/projects/#{params[:subdomain]}", :user_credentials => params[:user_credentials])
9
+ delete("/api/v1/projects/#{params[:subdomain]}", :user_credentials => params[:user_credentials])
10
10
  end
11
11
  end
12
12
  end
@@ -2,15 +2,15 @@ module StatRaptor
2
2
  class Client
3
3
  module Users
4
4
  def create_user(params = {})
5
- post("/users.json", :user => params)
5
+ post("/api/v1/users.json", :user => params)
6
6
  end
7
7
 
8
8
  def delete_user(user_credentials)
9
- delete("/users/#{user_credentials}.json")
9
+ delete("/api/v1/users/#{user_credentials}.json")
10
10
  end
11
11
 
12
12
  def get_users
13
- get("/users.json")
13
+ get("/api/v1/users.json")
14
14
  end
15
15
  end
16
16
  end
@@ -15,6 +15,9 @@ module StatRaptor
15
15
  require 'statraptor/client/graphs'
16
16
  include StatRaptor::Client::Graphs
17
17
 
18
+ require 'statraptor/client/adapters/rest_client'
19
+ require 'statraptor/client/adapters/typhoeus'
20
+
18
21
  attr_accessor *Config::VALID_OPTIONS_KEYS
19
22
 
20
23
  # Initializes a new API object
@@ -30,40 +33,28 @@ module StatRaptor
30
33
 
31
34
  private
32
35
 
33
- def get(url, params = {})
34
- request_and_parse :get, url, params
36
+ def get(path, params = {})
37
+ request_and_parse :get, path, params
35
38
  end
36
39
 
37
- def post(url, params = {})
38
- request_and_parse :post, url, params
40
+ def post(path, params = {})
41
+ request_and_parse :post, path, params
39
42
  end
40
43
 
41
- def put(url, params = {})
42
- request_and_parse :put, url, params
44
+ def put(path, params = {})
45
+ request_and_parse :put, path, params
43
46
  end
44
47
 
45
- def delete(url, params = {})
46
- request_and_parse :delete, url, params
48
+ def delete(path, params = {})
49
+ request_and_parse :delete, path, params
47
50
  end
48
51
 
49
- def request_and_parse(method, url, params = {})
50
- response = request_api_response(method, url, params)
52
+ def request_and_parse(method, path, params = {})
53
+ response = Client::Adapters::RestClient.request_api_response(method, path, params)
51
54
  handle_response(response)
52
55
  parse_response(response)
53
56
  end
54
57
 
55
- def request_api_response(method, url, params = {})
56
- params.merge!(:platform_credentials => StatRaptor.platform_credentials)
57
-
58
- Typhoeus::Request.run(
59
- "#{StatRaptor.endpoint}/api/v1#{url}",
60
- :method => method,
61
- :params => params,
62
- :disable_ssl_peer_verification => StatRaptor.disable_ssl_peer_verification,
63
- :timeout => StatRaptor.timeout
64
- )
65
- end
66
-
67
58
  def parse_response(response)
68
59
  JSON.parse(response.body)
69
60
  rescue JSON::ParserError
@@ -21,6 +21,11 @@ module StatRaptor
21
21
  # then you’ll have to disable peer verification to make SSL work
22
22
  DEFAULT_DISABLE_SSL_PEER_VERIFICATION = false
23
23
 
24
+ HTTP_HEADERS = {
25
+ 'Accept' => 'application/json',
26
+ 'User-Agent' => "StatRaptor RubyGem #{StatRaptor::VERSION} - http://github.com/chargify/statraptor",
27
+ }
28
+
24
29
  # An array of valid keys in the options hash when configuring a {StatRaptor::Client}
25
30
  VALID_OPTIONS_KEYS = [
26
31
  :endpoint,
@@ -59,5 +64,13 @@ module StatRaptor
59
64
  self.disable_ssl_peer_verification = DEFAULT_DISABLE_SSL_PEER_VERIFICATION
60
65
  self
61
66
  end
67
+
68
+ def timeout_in_seconds
69
+ self.timeout / 1000.0
70
+ end
71
+
72
+ def full_uri(path)
73
+ "#{StatRaptor.endpoint}#{path}"
74
+ end
62
75
  end
63
76
  end
@@ -1,3 +1,3 @@
1
1
  module StatRaptor
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/statraptor.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'typhoeus'
3
3
  require 'json'
4
+ require 'rest_client'
4
5
  require 'statraptor/version'
5
6
  require 'statraptor/config'
6
7
  require 'statraptor/client'
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'rspec'
5
5
  require 'vcr'
6
6
  require 'webmock/rspec'
7
7
  require 'yaml'
8
+ require 'uuid'
8
9
 
9
10
  unless File.exists?(File.join(File.dirname(__FILE__), 'remote.yml'))
10
11
  STDERR.puts "\nERROR: Make sure a remote.yml file exists at ./spec/remote.yml\n\n"
@@ -34,7 +35,7 @@ VCR.configure do |config|
34
35
  config.hook_into :webmock
35
36
  config.cassette_library_dir = 'spec/cassettes'
36
37
  config.configure_rspec_metadata!
37
- config.default_cassette_options = {:record => :once, :match_requests_on => [:uri, :body, :headers]}
38
+ config.default_cassette_options = {:record => :new_episodes, :match_requests_on => [:uri, :body, :headers]}
38
39
  end
39
40
 
40
41
  def platform_credentials
@@ -62,3 +63,7 @@ private
62
63
  def remote_configuration
63
64
  @remote_configuration ||= YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__), 'remote.yml')))
64
65
  end
66
+
67
+ def random_email
68
+ "statraptor-gem-test-email-#{UUID.generate}@example.com"
69
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe StatRaptor::Client::Adapters::RestClient do
4
+ describe ".request_api_response", :vcr do
5
+ it "returns a RestClient response" do
6
+ response = StatRaptor::Client::Adapters::RestClient.request_api_response(:get, "/users.json")
7
+ [:code, :headers, :raw_headers].each do |attr|
8
+ response.respond_to?(attr).should be_true
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe StatRaptor::Client::Adapters::Typhoeus do
4
+ describe ".request_api_response", :vcr do
5
+ it "returns a typhoeus response" do
6
+ response = StatRaptor::Client::Adapters::Typhoeus.request_api_response(:get, "/users.json")
7
+ response.should be_a(Typhoeus::Response)
8
+ end
9
+ end
10
+ end
@@ -5,7 +5,7 @@ describe StatRaptor::Client::Projects do
5
5
 
6
6
  context "#create_project", :vcr do
7
7
  before do
8
- @user = client.create_user(:email => "tommy@example.com", :chargify_api_key => "ABC123")
8
+ @user = client.create_user(:email => random_email, :chargify_api_key => "ABC123")
9
9
  @user["user_credentials"].should_not be_nil
10
10
  end
11
11
 
@@ -22,7 +22,7 @@ describe StatRaptor::Client::Projects do
22
22
 
23
23
  context "#delete_project", :vcr do
24
24
  it "returns the project hash on success" do
25
- user = client.create_user(:email => "sammy@example.com", :chargify_api_key => "ABC123")
25
+ user = client.create_user(:email => random_email, :chargify_api_key => "ABC123")
26
26
  project = client.create_project(:user_credentials => user["user_credentials"], :project => {
27
27
  :name => "Modern Marvels", :subdomain => "modern-marvels", :component => "advanced"
28
28
  })
@@ -5,20 +5,22 @@ describe StatRaptor::Client::Users do
5
5
 
6
6
  context "#create", :vcr do
7
7
  it "returns a user hash on success" do
8
- user = client.create_user(:email => "austin@example.com", :chargify_api_key => "ABC123")
8
+ email = random_email
9
+ user = client.create_user(:email => email, :chargify_api_key => "ABC123")
9
10
  user["user_credentials"].should_not be_nil
10
11
  user["chargify_api_key"].should == "ABC123"
11
- user["email"].should == "austin@example.com"
12
+ user["email"].should == email
12
13
  end
13
14
  end
14
15
 
15
16
  context "#delete_user", :vcr do
16
17
  it "returns the user hash on success" do
17
- user = client.create_user(:email => "leroy@example.com", :chargify_api_key => "XYZ123")
18
+ email = random_email
19
+ user = client.create_user(:email => email, :chargify_api_key => "XYZ123")
18
20
  deleted_user = client.delete_user(user["user_credentials"])
19
21
  deleted_user["user_credentials"].should == user["user_credentials"]
20
22
  deleted_user["chargify_api_key"].should == "XYZ123"
21
- deleted_user["email"].should == "leroy@example.com"
23
+ deleted_user["email"].should == email
22
24
  end
23
25
  end
24
26
 
@@ -88,4 +88,33 @@ describe StatRaptor do
88
88
  end
89
89
  end
90
90
 
91
+ describe ".timeout_in_seconds" do
92
+ it "returns the read timeout in seconds" do
93
+ StatRaptor.timeout = 1000
94
+ StatRaptor.timeout_in_seconds.should == 1.0
95
+ end
96
+ end
97
+
98
+ context "HTTP_HEADERS" do
99
+ it "should set the Accept header" do
100
+ StatRaptor::Config::HTTP_HEADERS['Accept'].should == "application/json"
101
+ end
102
+
103
+ it "should set the User-Agent header" do
104
+ StatRaptor::Config::HTTP_HEADERS['User-Agent'].should == "StatRaptor RubyGem #{StatRaptor::VERSION} - http://github.com/chargify/statraptor"
105
+ end
106
+ end
107
+
108
+ describe ".full_uri" do
109
+ it "accepts a path and returns the full URI to the StatRaptor API" do
110
+ StatRaptor.endpoint = "https://statraptor.com"
111
+ StatRaptor.full_uri("/api/v1/users").should == "https://statraptor.com/api/v1/users"
112
+ end
113
+
114
+ it "utilizes the current StatRaptor endpoint config option" do
115
+ StatRaptor.endpoint = "http://example.com"
116
+ StatRaptor.full_uri("/api/v2/path").should == "http://example.com/api/v2/path"
117
+ end
118
+ end
119
+
91
120
  end
data/statraptor.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.name = "statraptor"
9
9
  s.version = StatRaptor::VERSION
10
10
  s.platform = Gem::Platform::RUBY
11
- s.date = "2012-02-28"
11
+ s.date = "2012-02-29"
12
12
  s.authors = ["Shay Frendt"]
13
13
  s.email = "shay.frendt@gmail.com"
14
14
  s.homepage = "http://github.com/chargify/statraptor"
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_runtime_dependency('activeresource', '>= 2.3.5')
26
26
  s.add_runtime_dependency('typhoeus', '>= 0.3.3')
27
27
  s.add_runtime_dependency('json', '>= 1.4.6')
28
+ s.add_runtime_dependency('rest-client', '>= 1.6.7')
28
29
 
29
30
  # Development Dependencies
30
31
  s.add_development_dependency('rake', '~> 0.9.2')
@@ -34,4 +35,5 @@ Gem::Specification.new do |s|
34
35
  s.add_development_dependency('guard-rspec', '~> 0.6.0')
35
36
  s.add_development_dependency('growl', '~> 1.0.3')
36
37
  s.add_development_dependency('rb-fsevent', '~> 0.9.0')
38
+ s.add_development_dependency('uuid', '~> 2.3.5')
37
39
  end
data/todo.taskpaper CHANGED
@@ -1,3 +1,3 @@
1
1
  StatRaptor Todo:
2
2
  - Raise an error on a timeout
3
- - Set the user_agent for all HTTP calls
3
+ - Allow specifying an http adapter in the config
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statraptor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shay Frendt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-28 00:00:00 -05:00
18
+ date: 2012-02-29 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -67,9 +67,25 @@ dependencies:
67
67
  type: :runtime
68
68
  version_requirements: *id003
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: rest-client
71
71
  prerelease: false
72
72
  requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 1
78
+ segments:
79
+ - 1
80
+ - 6
81
+ - 7
82
+ version: 1.6.7
83
+ type: :runtime
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: rake
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
73
89
  none: false
74
90
  requirements:
75
91
  - - ~>
@@ -81,11 +97,11 @@ dependencies:
81
97
  - 2
82
98
  version: 0.9.2
83
99
  type: :development
84
- version_requirements: *id004
100
+ version_requirements: *id005
85
101
  - !ruby/object:Gem::Dependency
86
102
  name: rspec
87
103
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
104
+ requirement: &id006 !ruby/object:Gem::Requirement
89
105
  none: false
90
106
  requirements:
91
107
  - - ~>
@@ -97,16 +113,16 @@ dependencies:
97
113
  - 0
98
114
  version: 2.8.0
99
115
  type: :development
100
- version_requirements: *id005
116
+ version_requirements: *id006
101
117
  - !ruby/object:Gem::Dependency
102
118
  name: vcr
103
119
  prerelease: false
104
- requirement: &id006 !ruby/object:Gem::Requirement
120
+ requirement: &id007 !ruby/object:Gem::Requirement
105
121
  none: false
106
122
  requirements:
107
123
  - - "="
108
124
  - !ruby/object:Gem::Version
109
- hash: -2355515322
125
+ hash: -1931884648
110
126
  segments:
111
127
  - 2
112
128
  - 0
@@ -115,11 +131,11 @@ dependencies:
115
131
  - 1
116
132
  version: 2.0.0.rc1
117
133
  type: :development
118
- version_requirements: *id006
134
+ version_requirements: *id007
119
135
  - !ruby/object:Gem::Dependency
120
136
  name: webmock
121
137
  prerelease: false
122
- requirement: &id007 !ruby/object:Gem::Requirement
138
+ requirement: &id008 !ruby/object:Gem::Requirement
123
139
  none: false
124
140
  requirements:
125
141
  - - "="
@@ -131,11 +147,11 @@ dependencies:
131
147
  - 10
132
148
  version: 1.7.10
133
149
  type: :development
134
- version_requirements: *id007
150
+ version_requirements: *id008
135
151
  - !ruby/object:Gem::Dependency
136
152
  name: guard-rspec
137
153
  prerelease: false
138
- requirement: &id008 !ruby/object:Gem::Requirement
154
+ requirement: &id009 !ruby/object:Gem::Requirement
139
155
  none: false
140
156
  requirements:
141
157
  - - ~>
@@ -147,11 +163,11 @@ dependencies:
147
163
  - 0
148
164
  version: 0.6.0
149
165
  type: :development
150
- version_requirements: *id008
166
+ version_requirements: *id009
151
167
  - !ruby/object:Gem::Dependency
152
168
  name: growl
153
169
  prerelease: false
154
- requirement: &id009 !ruby/object:Gem::Requirement
170
+ requirement: &id010 !ruby/object:Gem::Requirement
155
171
  none: false
156
172
  requirements:
157
173
  - - ~>
@@ -163,11 +179,11 @@ dependencies:
163
179
  - 3
164
180
  version: 1.0.3
165
181
  type: :development
166
- version_requirements: *id009
182
+ version_requirements: *id010
167
183
  - !ruby/object:Gem::Dependency
168
184
  name: rb-fsevent
169
185
  prerelease: false
170
- requirement: &id010 !ruby/object:Gem::Requirement
186
+ requirement: &id011 !ruby/object:Gem::Requirement
171
187
  none: false
172
188
  requirements:
173
189
  - - ~>
@@ -179,7 +195,23 @@ dependencies:
179
195
  - 0
180
196
  version: 0.9.0
181
197
  type: :development
182
- version_requirements: *id010
198
+ version_requirements: *id011
199
+ - !ruby/object:Gem::Dependency
200
+ name: uuid
201
+ prerelease: false
202
+ requirement: &id012 !ruby/object:Gem::Requirement
203
+ none: false
204
+ requirements:
205
+ - - ~>
206
+ - !ruby/object:Gem::Version
207
+ hash: 9
208
+ segments:
209
+ - 2
210
+ - 3
211
+ - 5
212
+ version: 2.3.5
213
+ type: :development
214
+ version_requirements: *id012
183
215
  description: StatRaptor gathers all your metrics into one simple-to-use dashboard.
184
216
  email: shay.frendt@gmail.com
185
217
  executables: []
@@ -200,6 +232,8 @@ files:
200
232
  - Rakefile
201
233
  - lib/statraptor.rb
202
234
  - lib/statraptor/client.rb
235
+ - lib/statraptor/client/adapters/rest_client.rb
236
+ - lib/statraptor/client/adapters/typhoeus.rb
203
237
  - lib/statraptor/client/graphs.rb
204
238
  - lib/statraptor/client/projects.rb
205
239
  - lib/statraptor/client/users.rb
@@ -210,6 +244,8 @@ files:
210
244
  - lib/statraptor/version.rb
211
245
  - spec/spec.opts
212
246
  - spec/spec_helper.rb
247
+ - spec/statrapator/client/adapters/rest_client_spec.rb
248
+ - spec/statrapator/client/adapters/typhoeus_spec.rb
213
249
  - spec/statrapator/client/graphs_spec.rb
214
250
  - spec/statrapator/client/projects_spec.rb
215
251
  - spec/statrapator/client/user_spec.rb
@@ -257,6 +293,8 @@ summary: A StatRaptor API wrapper for Ruby
257
293
  test_files:
258
294
  - spec/spec.opts
259
295
  - spec/spec_helper.rb
296
+ - spec/statrapator/client/adapters/rest_client_spec.rb
297
+ - spec/statrapator/client/adapters/typhoeus_spec.rb
260
298
  - spec/statrapator/client/graphs_spec.rb
261
299
  - spec/statrapator/client/projects_spec.rb
262
300
  - spec/statrapator/client/user_spec.rb