songkick-transport 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,8 +9,9 @@ module Songkick
9
9
  DEFAULT_TIMEOUT = 5
10
10
  DEFAULT_FORMAT = :json
11
11
 
12
- HTTP_VERBS = %w[get post put delete head]
13
- USE_BODY = %w[post put]
12
+ HTTP_VERBS = %w[get post put delete head]
13
+ USE_BODY = %w[post put]
14
+ FORM_ENCODING = 'application/x-www-form-urlencoded'
14
15
 
15
16
  ROOT = File.expand_path('..', __FILE__)
16
17
 
@@ -39,10 +39,10 @@ module Songkick
39
39
  end
40
40
 
41
41
  def headers
42
- {
42
+ Headers.new(
43
43
  'Connection' => 'close',
44
44
  'User-Agent' => user_agent || ''
45
- }
45
+ )
46
46
  end
47
47
 
48
48
  def logger
@@ -45,7 +45,6 @@ module Songkick
45
45
  end
46
46
 
47
47
  if req.use_body?
48
- connection.headers['Content-Type'] ||= req.content_type
49
48
  connection.__send__("http_#{req.verb}", req.body)
50
49
  else
51
50
  connection.__send__("http_#{req.verb}")
@@ -4,6 +4,18 @@ module Songkick
4
4
  class Headers
5
5
  include Enumerable
6
6
 
7
+ def self.new(hash)
8
+ return hash if self === hash
9
+ super
10
+ end
11
+
12
+ def self.normalize(header_name)
13
+ header_name.
14
+ gsub(/^HTTP_/, '').gsub('_', '-').
15
+ downcase.
16
+ gsub(/(^|-)([a-z])/) { $1 + $2.upcase }
17
+ end
18
+
7
19
  def initialize(hash = {})
8
20
  @hash = {}
9
21
  hash.each do |key, value|
@@ -19,15 +31,18 @@ module Songkick
19
31
  @hash[self.class.normalize(header_name)]
20
32
  end
21
33
 
34
+ def []=(header_name, value)
35
+ @hash[self.class.normalize(header_name)] = value
36
+ end
37
+
22
38
  def merge(hash)
23
- @hash.merge(hash)
39
+ headers = self.class.new(to_hash)
40
+ hash.each { |k,v| headers[k] = v }
41
+ headers
24
42
  end
25
43
 
26
- def self.normalize(header_name)
27
- header_name.
28
- gsub(/^HTTP_/, '').gsub('_', '-').
29
- downcase.
30
- gsub(/(^|-)([a-z])/) { $1 + $2.upcase }
44
+ def to_hash
45
+ @hash.dup
31
46
  end
32
47
  end
33
48
 
@@ -26,12 +26,7 @@ module Songkick
26
26
  timeout = req.timeout || self.class.default_options[:timeout]
27
27
 
28
28
  response = if req.use_body?
29
- if req.multipart?
30
- head = req.headers.merge('Content-Type' => req.content_type)
31
- self.class.__send__(req.verb, req.path, :body => req.body, :headers => head, :timeout => timeout)
32
- else
33
- self.class.__send__(req.verb, req.path, :body => req.body, :headers => req.headers, :timeout => timeout)
34
- end
29
+ self.class.__send__(req.verb, req.path, :body => req.body, :headers => req.headers, :timeout => timeout)
35
30
  else
36
31
  self.class.__send__(req.verb, req.url, :headers => req.headers, :timeout => timeout)
37
32
  end
@@ -2,15 +2,14 @@ module Songkick
2
2
  module Transport
3
3
 
4
4
  class Request
5
- attr_reader :endpoint,
6
- :verb,
7
- :path,
8
- :params,
9
- :headers,
10
- :timeout,
11
- :start_time,
12
- :response,
13
- :error
5
+ attr_reader :endpoint,
6
+ :verb,
7
+ :path,
8
+ :params,
9
+ :timeout,
10
+ :start_time,
11
+ :response,
12
+ :error
14
13
 
15
14
  alias :http_method :verb
16
15
 
@@ -18,11 +17,15 @@ module Songkick
18
17
  @endpoint = endpoint
19
18
  @verb = verb.to_s.downcase
20
19
  @path = path
21
- @headers = headers
20
+ @headers = Headers.new(headers)
22
21
  @params = params
23
22
  @timeout = timeout
24
23
  @start_time = start_time || Time.now
25
24
  @multipart = Serialization.multipart?(params)
25
+
26
+ if use_body?
27
+ @headers['Content-Type'] ||= @multipart ? multipart_request[:content_type] : FORM_ENCODING
28
+ end
26
29
  end
27
30
 
28
31
  def response=(response)
@@ -40,6 +43,10 @@ module Songkick
40
43
  (@end_time.to_f - @start_time.to_f) * 1000
41
44
  end
42
45
 
46
+ def headers
47
+ @headers.to_hash
48
+ end
49
+
43
50
  def use_body?
44
51
  USE_BODY.include?(@verb)
45
52
  end
@@ -48,15 +55,6 @@ module Songkick
48
55
  @multipart
49
56
  end
50
57
 
51
- def content_type
52
- return nil unless use_body?
53
- if @multipart
54
- multipart_request[:content_type]
55
- else
56
- 'application/x-www-form-urlencoded'
57
- end
58
- end
59
-
60
58
  def body
61
59
  return nil unless use_body?
62
60
  if @multipart
@@ -82,7 +80,6 @@ module Songkick
82
80
  end
83
81
  return command unless use_body?
84
82
  query = Serialization.build_query_string(params, true, true)
85
- command << " -H 'Content-Type: #{content_type}'"
86
83
  command << " -d '#{query}'"
87
84
  command
88
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: songkick-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-07 00:00:00.000000000 Z
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multipart-post