pebblebed 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -6,3 +6,5 @@ pkg/*
6
6
  tmp/
7
7
  coverage
8
8
  .powder
9
+ /*.sublime-*
10
+ .idea
@@ -6,35 +6,16 @@ module Pebblebed
6
6
  @identity = get("/identities/me")[:identity]
7
7
  end
8
8
 
9
- def cache_key_for_identity_id(id)
10
- "identity:#{id}"
11
- end
12
-
13
9
  # Given a list of identity IDs it returns each identity or an empty hash for identities that doesnt exists.
14
10
  # If pebbles are configured with memcached, results will be cached.
15
11
  # Params: ids a list of identities
16
12
  def find_identities(ids)
17
13
 
18
14
  result = {}
19
- uncached = ids
20
-
21
- if Pebblebed.memcached
22
- cache_keys = ids.collect {|id| cache_key_for_identity_id(id) }
23
- result = Hash[Pebblebed.memcached.get_multi(*cache_keys).map { |key, identity|
24
- if key =~ /identity:(\d+)/
25
- [$1.to_i, identity]
26
- end
27
- }]
28
- uncached = ids-result.keys
29
- end
30
-
31
- if uncached.size > 0
32
- request = get("/identities/#{uncached.join(',')},")
33
- uncached.each_with_index do |id, i|
34
- identity = request.identities[i].identity.unwrap
35
- result[id] = identity
36
- Pebblebed.memcached.set(cache_key_for_identity_id(id), identity, ttl=60*15) if Pebblebed.memcached
37
- end
15
+ request = get("/identities/#{ids.join(',')},")
16
+ ids.each_with_index do |id, i|
17
+ identity = request.identities[i].identity.unwrap
18
+ result[id] = identity
38
19
  end
39
20
  return DeepStruct.wrap(ids.collect {|id| result[id]})
40
21
  end
@@ -43,4 +24,4 @@ module Pebblebed
43
24
  me.god if me
44
25
  end
45
26
  end
46
- end
27
+ end
@@ -11,8 +11,8 @@ module Pebblebed
11
11
  def perform(method, url = '', params = {}, &block)
12
12
  begin
13
13
  result = Pebblebed::Http.send(method, service_url(url), service_params(params), &block)
14
- return DeepStruct.wrap(Yajl::Parser.parse(result.body))
15
- rescue Yajl::ParseError
14
+ return DeepStruct.wrap(JSON.parse(result.body))
15
+ rescue JSON::ParserError
16
16
  return result.body
17
17
  end
18
18
  end
@@ -33,8 +33,14 @@ module Pebblebed
33
33
  end
34
34
 
35
35
  def service_params(params)
36
- params ||= {}
37
- params['session'] ||= @session_key if @session_key
36
+ if (key = @session_key) and (params.nil? or not params[:session])
37
+ if params
38
+ params = params.dup # Make sure we don't modify it
39
+ else
40
+ params = {}
41
+ end
42
+ params['session'] = key
43
+ end
38
44
  params
39
45
  end
40
46
 
@@ -2,6 +2,7 @@ require 'active_support/inflector'
2
2
 
3
3
  module Pebblebed
4
4
  class Connector
5
+ attr_accessor :key
5
6
  def initialize(key = nil, url_opts = {})
6
7
  @key = key
7
8
  @clients = {}
@@ -23,7 +24,7 @@ module Pebblebed
23
24
 
24
25
 
25
26
  def parts
26
- @parts ||= Pebblebed::Parts.new(self)
27
+ @@parts ||= Pebblebed::Parts.new(self)
27
28
  end
28
29
 
29
30
  def self.client_class_for(service)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'uri'
4
4
  require 'curl'
5
- require 'yajl'
5
+ require 'yajl/json_gem'
6
6
  require 'queryparams'
7
7
  require 'nokogiri'
8
8
  require 'pathbuilder'
@@ -55,19 +55,19 @@ module Pebblebed
55
55
 
56
56
  def self.post(url, params, &block)
57
57
  url, params = url_and_params_from_args(url, params, &block)
58
- body = params.is_a?(String) ? params : params.to_json
58
+ content_type, body = serialize_params(params)
59
59
  handle_curl_response(Curl::Easy.http_post(url.to_s, body) do |curl|
60
60
  curl.headers['Accept'] = 'application/json'
61
- curl.headers['Content-Type'] = params.is_a?(String) ? 'text/plain' : 'application/json'
61
+ curl.headers['Content-Type'] = content_type
62
62
  end)
63
63
  end
64
64
 
65
65
  def self.put(url, params, &block)
66
66
  url, params = url_and_params_from_args(url, params, &block)
67
- body = params.is_a?(String) ? params : params.to_json
67
+ content_type, body = serialize_params(params)
68
68
  handle_curl_response(Curl::Easy.http_put(url.to_s, body) do |curl|
69
69
  curl.headers['Accept'] = 'application/json'
70
- curl.headers['Content-Type'] = params.is_a?(String) ? 'text/plain' : 'application/json'
70
+ curl.headers['Content-Type'] = content_type
71
71
  end)
72
72
  end
73
73
 
@@ -78,6 +78,18 @@ module Pebblebed
78
78
 
79
79
  private
80
80
 
81
+ def self.serialize_params(params)
82
+ if String === params
83
+ content_type, body = 'text/plain', params
84
+ else
85
+ content_type, body = 'application/json', JSON.dump(params)
86
+ end
87
+ if body.respond_to?(:encoding) and body.encoding != Encoding::UTF_8
88
+ content_type << "; charset=#{body.encoding}"
89
+ end
90
+ return content_type, body
91
+ end
92
+
81
93
  def self.handle_http_errors(result)
82
94
  if result.status >= 400
83
95
  errmsg = "Service request to '#{result.url}' failed (#{result.status}):"
@@ -121,6 +121,6 @@ Pebblebed::Parts.register_composition_strategy :direct do |url, params|
121
121
  begin
122
122
  Pebblebed::Connector.new.get(url, params)
123
123
  rescue HttpError => e
124
- "<span class='pebbles_error'>'#{url}' with parameters #{params.to_json} failed (#{e.status}): #{e.message}</span>"
124
+ "<span class='pebbles_error'>'#{url}' with parameters #{JSON.dump(params)} failed (#{e.status}): #{e.message}</span>"
125
125
  end
126
126
  end
@@ -1,3 +1,3 @@
1
1
  module Pebblebed
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
data/pebblebed.gemspec CHANGED
@@ -23,8 +23,8 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency "rake"
24
24
  s.add_development_dependency "simplecov"
25
25
 
26
- s.add_runtime_dependency "deepstruct"
27
- s.add_runtime_dependency "curb", "~> 0.8.0"
26
+ s.add_runtime_dependency "deepstruct", ">= 0.0.2"
27
+ s.add_runtime_dependency "curb", ">= 0.7.14"
28
28
  s.add_runtime_dependency "yajl-ruby"
29
29
  s.add_runtime_dependency "queryparams"
30
30
  s.add_runtime_dependency "futurevalue"
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'json'
3
2
 
4
3
  describe Pebblebed::CheckpointClient do
5
4
 
@@ -31,13 +30,6 @@ describe Pebblebed::CheckpointClient do
31
30
  end
32
31
  end
33
32
 
34
- describe "cache_key_for_identity_id" do
35
- it "creates a nice looking cache key for memcache" do
36
- checkpoint_client = Pebblebed::Connector.new('session_key')[:checkpoint]
37
- checkpoint_client.cache_key_for_identity_id(2).should eq "identity:2"
38
- end
39
- end
40
-
41
33
  describe "find_identities" do
42
34
  let(:canned_response) {
43
35
  DeepStruct.wrap({:body=>
@@ -47,70 +39,17 @@ describe Pebblebed::CheckpointClient do
47
39
  })
48
40
  }
49
41
 
50
- describe "without memcache configured" do
51
- before(:each) do
52
- Pebblebed.config do
53
- host "checkpoint.dev"
54
- service :checkpoint
55
- end
56
- end
57
-
58
- it "issues an http request every time" do
59
- Pebblebed::Http.should_receive(:get).twice.and_return canned_response
60
- checkpoint_client.find_identities([1, 2])
61
- checkpoint_client.find_identities([1, 2])
42
+ before(:each) do
43
+ Pebblebed.config do
44
+ host "checkpoint.dev"
45
+ service :checkpoint
62
46
  end
63
47
  end
64
48
 
65
- describe "with memcached configured" do
66
- before(:each) do
67
- Pebblebed.config do
68
- host "checkpoint.dev"
69
- memcached $memcached
70
- service :checkpoint
71
- end
72
- end
73
-
74
- it "issues an http request and caches it" do
75
- Pebblebed::Http.should_receive(:get).once.and_return(canned_response)
76
- checkpoint_client.find_identities([1, 2])
77
- checkpoint_client.find_identities([1, 2])
78
-
79
- $memcached.get(checkpoint_client.cache_key_for_identity_id(1)).should eq({"id"=>1})
80
- $memcached.get(checkpoint_client.cache_key_for_identity_id(2)).should eq({})
81
- end
82
-
83
- it "returns exactly the same data no matter if it is cached or originating from a request" do
84
- Pebblebed::Http.should_receive(:get).once.and_return(canned_response)
85
-
86
- http_requested_result = checkpoint_client.find_identities([1, 2, 3])
87
- cached_result = checkpoint_client.find_identities([1, 2, 3])
88
-
89
- http_requested_result.unwrap.should eq cached_result.unwrap
90
- end
91
-
92
- it "issues a request only for not previously cached identities" do
93
- Pebblebed::Http.should_receive(:get) { |url|
94
- url.path.should match("/identities/1,2,4")
95
- canned_response
96
- }.once
97
- checkpoint_client.find_identities([1, 2, 4])
98
-
99
- Pebblebed::Http.should_receive(:get) { |url|
100
- url.path.should match("/identities/3,") # Note the extra comma. Will ensure that a list is returned
101
- canned_response
102
- }.once
103
- checkpoint_client.find_identities([1, 2, 3, 4])
104
- end
105
-
106
- it "will always return identities in the order they are requested" do
107
- Pebblebed::Http.should_receive(:get).once.and_return(canned_response)
108
-
109
- checkpoint_client.find_identities([1, 2, 3, 4])
110
- identities = checkpoint_client.find_identities([4, 3, 2, 1])
111
- identities[1].id.should eq 3
112
- identities[3].id.should eq 1
113
- end
49
+ it "issues an http request every time" do
50
+ Pebblebed::Http.should_receive(:get).twice.and_return canned_response
51
+ checkpoint_client.find_identities([1, 2])
52
+ checkpoint_client.find_identities([1, 2])
114
53
  end
115
54
  end
116
- end
55
+ end
@@ -20,4 +20,11 @@ describe "Pebblebed::Connector" do
20
20
  connector['foobar'].class.name.should eq "Pebblebed::GenericClient"
21
21
  end
22
22
 
23
+ it "has key getter and setter" do
24
+ connector = Pebblebed::Connector.new("session_key")
25
+ connector.key.should == "session_key"
26
+ connector.key = "another_key"
27
+ connector.key.should == "another_key"
28
+ end
29
+
23
30
  end
data/spec/http_spec.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
4
+ require 'yajl/json_gem'
2
5
 
3
6
  describe Pebblebed::Http do
4
7
 
@@ -47,7 +50,7 @@ describe Pebblebed::Http do
47
50
  ['post', 'put'].each do |method|
48
51
  response = Pebblebed::Http.send(method.to_sym, pebble_url, {hello:'world'})
49
52
  result = JSON.parse(response.body)
50
- result["CONTENT_TYPE"].should eq "application/json"
53
+ result["CONTENT_TYPE"].should =~ %r{^application/json\b}i
51
54
  JSON.parse(result["BODY"])['hello'].should eq 'world'
52
55
  end
53
56
  end
@@ -56,7 +59,7 @@ describe Pebblebed::Http do
56
59
  ['post', 'put'].each do |method|
57
60
  response = Pebblebed::Http.send(method.to_sym, pebble_url, "Hello world")
58
61
  result = JSON.parse(response.body)
59
- result["CONTENT_TYPE"].should eq "text/plain"
62
+ result["CONTENT_TYPE"].should =~ %r{^text/plain\b}i
60
63
  result["BODY"].should eq "Hello world"
61
64
  end
62
65
  end
data/spec/mock_pebble.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # http://localhost:8666/api/mock/v1
4
4
 
5
5
  require 'webrick'
6
- require 'json'
6
+ require 'yajl/json_gem'
7
7
 
8
8
  class MockPebble
9
9
  class Servlet < WEBrick::HTTPServlet::AbstractServlet
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebblebed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-15 00:00:00.000000000 Z
13
+ date: 2012-06-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &70306887832940 !ruby/object:Gem::Requirement
17
+ requirement: &70098584984600 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70306887832940
25
+ version_requirements: *70098584984600
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &70306887832320 !ruby/object:Gem::Requirement
28
+ requirement: &70098584984180 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70306887832320
36
+ version_requirements: *70098584984180
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: simplecov
39
- requirement: &70306887831520 !ruby/object:Gem::Requirement
39
+ requirement: &70098584983740 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,32 +44,32 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70306887831520
47
+ version_requirements: *70098584983740
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: deepstruct
50
- requirement: &70306887830640 !ruby/object:Gem::Requirement
50
+ requirement: &70098584983120 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: 0.0.2
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70306887830640
58
+ version_requirements: *70098584983120
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: curb
61
- requirement: &70306887829680 !ruby/object:Gem::Requirement
61
+ requirement: &70098584982480 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
- - - ~>
64
+ - - ! '>='
65
65
  - !ruby/object:Gem::Version
66
- version: 0.8.0
66
+ version: 0.7.14
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *70306887829680
69
+ version_requirements: *70098584982480
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: yajl-ruby
72
- requirement: &70306887829040 !ruby/object:Gem::Requirement
72
+ requirement: &70098584998200 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :runtime
79
79
  prerelease: false
80
- version_requirements: *70306887829040
80
+ version_requirements: *70098584998200
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: queryparams
83
- requirement: &70306887828360 !ruby/object:Gem::Requirement
83
+ requirement: &70098584997700 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :runtime
90
90
  prerelease: false
91
- version_requirements: *70306887828360
91
+ version_requirements: *70098584997700
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: futurevalue
94
- requirement: &70306887827660 !ruby/object:Gem::Requirement
94
+ requirement: &70098584997260 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :runtime
101
101
  prerelease: false
102
- version_requirements: *70306887827660
102
+ version_requirements: *70098584997260
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: pathbuilder
105
- requirement: &70306887827160 !ruby/object:Gem::Requirement
105
+ requirement: &70098584996820 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :runtime
112
112
  prerelease: false
113
- version_requirements: *70306887827160
113
+ version_requirements: *70098584996820
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: nokogiri
116
- requirement: &70306887826500 !ruby/object:Gem::Requirement
116
+ requirement: &70098584996160 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: '0'
122
122
  type: :runtime
123
123
  prerelease: false
124
- version_requirements: *70306887826500
124
+ version_requirements: *70098584996160
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: i18n
127
- requirement: &70306887819280 !ruby/object:Gem::Requirement
127
+ requirement: &70098584995420 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ! '>='
@@ -132,10 +132,10 @@ dependencies:
132
132
  version: '0'
133
133
  type: :runtime
134
134
  prerelease: false
135
- version_requirements: *70306887819280
135
+ version_requirements: *70098584995420
136
136
  - !ruby/object:Gem::Dependency
137
137
  name: activesupport
138
- requirement: &70306887818520 !ruby/object:Gem::Requirement
138
+ requirement: &70098584992940 !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
141
141
  - - ! '>='
@@ -143,7 +143,7 @@ dependencies:
143
143
  version: '0'
144
144
  type: :runtime
145
145
  prerelease: false
146
- version_requirements: *70306887818520
146
+ version_requirements: *70098584992940
147
147
  description: Development tools for working with Pebblebed
148
148
  email:
149
149
  - katrina@bengler.no
@@ -201,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  requirements: []
203
203
  rubyforge_project: pebblebed
204
- rubygems_version: 1.8.10
204
+ rubygems_version: 1.8.15
205
205
  signing_key:
206
206
  specification_version: 3
207
207
  summary: Development tools for working with Pebblebed