jeff 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -37,6 +37,7 @@ client = Client.new.tap do |config|
37
37
  config.secret = 'secret'
38
38
  end
39
39
  ```
40
+
40
41
  You should now be able to access the endpoint.
41
42
 
42
43
  ```ruby
@@ -44,7 +45,9 @@ client.post query: {},
44
45
  body: 'data'
45
46
  ```
46
47
 
47
- Make a chunked request.
48
+ ### Chunked Requests
49
+
50
+ You can upload large files performantly by passing a proc that delivers chunks.
48
51
 
49
52
  ```ruby
50
53
  file = File.open 'data'
@@ -54,7 +57,10 @@ client.post query: {},
54
57
  request_block: chunker
55
58
  ```
56
59
 
57
- Stream a response.
60
+ ### Streaming Responses
61
+
62
+ Similarly, you can download and parse large files performantly by passing a
63
+ block that will receive chunks.
58
64
 
59
65
  ```ruby
60
66
  streamer = ->(chunk, remaining, total) { puts chunk }
@@ -63,7 +69,33 @@ client.get query: {},
63
69
  response_block: streamer
64
70
  ```
65
71
 
66
- HTTP connections are persistent.
72
+ ### Instrumentation
73
+
74
+ Requests can be instrumented.
75
+
76
+ ```ruby
77
+ class Logger
78
+ def self.instrument(name, params = {})
79
+ if name =~ /request/
80
+ $stderr.puts [
81
+ params[:scheme],
82
+ '://',
83
+ params[:host]
84
+ '/',
85
+ params[:path],
86
+ '?',
87
+ params[:query]
88
+ ].join
89
+ yield if block_given?
90
+ end
91
+ end
92
+ end
93
+
94
+ client.get query: {},
95
+ instrumentor: Logger
96
+ ```
97
+
98
+ For more detailed configuration options, check out the [excon README][excon].
67
99
 
68
100
  [aws]: http://aws.amazon.com/
69
101
  [excon]: https://github.com/geemus/excon
data/lib/jeff.rb CHANGED
@@ -70,14 +70,14 @@ module Jeff
70
70
  eval <<-DEF
71
71
  def #{method}(opts = {}, &block)
72
72
  opts.update method: :#{method}
73
- request opts, &block
73
+ connection.request sign opts, &block
74
74
  end
75
75
  DEF
76
76
  end
77
77
 
78
78
  # Internal: Builds a sorted query.
79
79
  #
80
- # hsh - A hash of parameters specific to request.
80
+ # hsh - A hash of query parameters specific to the request.
81
81
  #
82
82
  # Returns a query String.
83
83
  def build_query(hsh)
@@ -88,30 +88,25 @@ module Jeff
88
88
  .join '&'
89
89
  end
90
90
 
91
- # Internal: Signs a message.
92
- #
93
- # message - A String to sign.
94
- #
95
- # Returns a String signature.
96
- def sign(message)
97
- digest = OpenSSL::HMAC.digest SHA256, secret, message
98
- Base64.encode64(digest).chomp
99
- end
100
-
101
91
  private
102
92
 
103
- def request(opts, &block)
93
+ def sign(opts)
104
94
  query = build_query opts[:query] || {}
95
+
105
96
  string_to_sign = [
106
- opts[:method],
107
- host,
108
- path,
97
+ opts[:method].upcase,
98
+ opts[:host] || connection.connection[:host],
99
+ opts[:path] || connection.connection[:path],
109
100
  query
110
101
  ].join "\n"
111
- signature = sign string_to_sign
112
- opts[:query] = [query, "Signature=#{escape signature}"].join '&'
113
102
 
114
- connection.request opts, &block
103
+ digest = OpenSSL::HMAC.digest SHA256, secret, string_to_sign
104
+ signature = Base64.encode64(digest).chomp
105
+
106
+ opts.update query: [
107
+ query,
108
+ "Signature=#{escape signature}"
109
+ ].join('&')
115
110
  end
116
111
 
117
112
  def escape(val)
@@ -120,18 +115,6 @@ module Jeff
120
115
  end
121
116
  end
122
117
 
123
- def host
124
- @host ||= url.host
125
- end
126
-
127
- def path
128
- @path ||= url.path
129
- end
130
-
131
- def url
132
- @url ||= URI endpoint
133
- end
134
-
135
118
  module ClassMethods
136
119
  # Amazon recommends that libraries identify themselves via a User Agent.
137
120
  USER_AGENT = "Jeff/#{VERSION} (Language=Ruby; #{`hostname`.chomp})"
data/lib/jeff/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jeff
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  end
data/spec/jeff_spec.rb CHANGED
@@ -90,19 +90,6 @@ describe Jeff do
90
90
  end
91
91
  end
92
92
 
93
- context 'given a key and a secret' do
94
- before do
95
- client.key = 'key'
96
- client.secret = 'secret'
97
- end
98
-
99
- describe '#sign' do
100
- subject { client.sign 'foo' }
101
-
102
- it { should be_a String }
103
- end
104
- end
105
-
106
93
  context 'given an endpoint' do
107
94
  before do
108
95
  client.endpoint = 'http://slowapi.com/delay/0'
@@ -133,32 +120,20 @@ describe Jeff do
133
120
 
134
121
  Excon::HTTP_VERBS.each do |method|
135
122
  describe "##{method}" do
136
- subject { client.send method, mock: true }
123
+ subject { client.send(method, mock: true).body }
137
124
 
138
- it "should make a #{method.upcase} request" do
139
- Excon.stub({ method: method.to_sym }, { body: method })
140
- subject.body.should eql method
141
- end
142
-
143
- it 'should include default headers' do
125
+ before do
144
126
  Excon.stub({ method: method.to_sym }) do |params|
145
- { body: params[:headers] }
127
+ { body: params }
146
128
  end
147
- subject.body.should have_key 'User-Agent'
148
129
  end
149
130
 
150
- it 'should include parameters' do
151
- Excon.stub({ method: method.to_sym }) do |params|
152
- { body: params[:query] }
153
- end
154
- subject.body.should match client.build_query({})
131
+ it "should make a #{method.upcase} request" do
132
+ subject[:method].should eql method.to_sym
155
133
  end
156
134
 
157
135
  it 'should append a signature' do
158
- Excon.stub({ method: method.to_sym }) do |params|
159
- { body: params[:query] }
160
- end
161
- subject.body.should match /Signature=[^&]+$/
136
+ subject[:query].should match /.+&Signature=[^&]+$/
162
137
  end
163
138
  end
164
139
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -137,4 +137,3 @@ summary: AWS client
137
137
  test_files:
138
138
  - spec/jeff_spec.rb
139
139
  - spec/spec_helper.rb
140
- has_rdoc: