nestful 0.0.7 → 0.0.8

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -1,18 +1,18 @@
1
- require "net/http"
2
- require "uri"
3
- require "tempfile"
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'tempfile'
4
4
 
5
- require "active_support/core_ext"
6
- require "active_support/inflector"
5
+ require 'active_support/core_ext'
6
+ require 'active_support/inflector'
7
7
 
8
8
  $:.unshift(File.dirname(__FILE__))
9
9
 
10
- require "nestful/exceptions"
11
- require "nestful/formats"
12
- require "nestful/connection"
13
- require "nestful/request/callbacks"
14
- require "nestful/request"
15
- require "nestful/resource"
10
+ require 'nestful/exceptions'
11
+ require 'nestful/formats'
12
+ require 'nestful/connection'
13
+ require 'nestful/request/callbacks'
14
+ require 'nestful/request'
15
+ require 'nestful/resource'
16
16
 
17
17
  module Nestful
18
18
  extend self
@@ -7,11 +7,11 @@ module Nestful
7
7
  class Connection
8
8
 
9
9
  HTTP_FORMAT_HEADER_NAMES = {
10
- :get => 'Accept',
11
- :put => 'Content-Type',
12
- :post => 'Content-Type',
10
+ :get => 'Accept',
11
+ :put => 'Content-Type',
12
+ :post => 'Content-Type',
13
13
  :delete => 'Accept',
14
- :head => 'Accept'
14
+ :head => 'Accept'
15
15
  }
16
16
 
17
17
  attr_reader :site, :user, :password, :auth_type, :timeout, :proxy, :ssl_options
@@ -14,12 +14,12 @@ module Nestful
14
14
  end
15
15
  end
16
16
 
17
- autoload :BlankFormat, 'nestful/formats/blank_format'
18
- autoload :TextFormat, 'nestful/formats/text_format'
17
+ autoload :BlankFormat, 'nestful/formats/blank_format'
18
+ autoload :TextFormat, 'nestful/formats/text_format'
19
19
  autoload :MultipartFormat, 'nestful/formats/multipart_format'
20
- autoload :FormFormat, 'nestful/formats/form_format'
21
- autoload :XmlFormat, 'nestful/formats/xml_format'
22
- autoload :JsonFormat, 'nestful/formats/json_format'
20
+ autoload :FormFormat, 'nestful/formats/form_format'
21
+ autoload :XmlFormat, 'nestful/formats/xml_format'
22
+ autoload :JsonFormat, 'nestful/formats/json_format'
23
23
 
24
24
  # Lookup the format class from a mime type reference symbol. Example:
25
25
  #
@@ -2,7 +2,7 @@ module Nestful
2
2
  module Formats
3
3
  class BlankFormat < Format
4
4
  def encode(params, options = nil)
5
- raise "Choose an encoding format, such as :form"
5
+ raise 'Choose an encoding format, such as :form'
6
6
  end
7
7
 
8
8
  def decode(body)
@@ -2,7 +2,7 @@ module Nestful
2
2
  module Formats
3
3
  class FormFormat < Format
4
4
  def mime_type
5
- "application/x-www-form-urlencoded"
5
+ 'application/x-www-form-urlencoded'
6
6
  end
7
7
 
8
8
  def encode(params, options = nil)
@@ -5,11 +5,11 @@ module Nestful
5
5
  module Formats
6
6
  class JsonFormat < Format
7
7
  def extension
8
- "json"
8
+ 'json'
9
9
  end
10
10
 
11
11
  def mime_type
12
- "application/json"
12
+ 'application/json'
13
13
  end
14
14
 
15
15
  def encode(hash, options = nil)
@@ -1,4 +1,4 @@
1
- require "active_support/secure_random"
1
+ require 'securerandom'
2
2
 
3
3
  module Nestful
4
4
  module Formats
@@ -9,7 +9,7 @@ module Nestful
9
9
 
10
10
  def initialize(*args)
11
11
  super
12
- @boundary = ActiveSupport::SecureRandom.hex(10)
12
+ @boundary = SecureRandom.hex(10)
13
13
  @stream = Tempfile.new("nf.#{rand(1000)}")
14
14
  @stream.binmode
15
15
  end
@@ -72,8 +72,8 @@ module Nestful
72
72
  def filename(body)
73
73
  return body.original_filename if body.respond_to?(:original_filename)
74
74
  return File.basename(body.path) if body.respond_to?(:path)
75
- "Unknown"
75
+ 'Unknown'
76
76
  end
77
77
  end
78
78
  end
79
- end
79
+ end
@@ -2,7 +2,7 @@ module Nestful
2
2
  module Formats
3
3
  class TextFormat < Format
4
4
  def mime_type
5
- "text/plain"
5
+ 'text/plain'
6
6
  end
7
7
 
8
8
  def encode(body)
@@ -4,11 +4,11 @@ module Nestful
4
4
  module Formats
5
5
  class XmlFormat < Format
6
6
  def extension
7
- "xml"
7
+ 'xml'
8
8
  end
9
9
 
10
10
  def mime_type
11
- "application/xml"
11
+ 'application/xml'
12
12
  end
13
13
 
14
14
  def encode(hash, options={})
@@ -1,4 +1,4 @@
1
- require "roauth"
1
+ require 'roauth'
2
2
 
3
3
  module Nestful
4
4
  class Request
@@ -16,7 +16,7 @@ module Nestful
16
16
  )
17
17
 
18
18
  self.headers ||= {}
19
- self.headers["Authorization"] = signature
19
+ self.headers['Authorization'] = signature
20
20
  end
21
21
 
22
22
  before_request(&:oauth_sign)
@@ -6,7 +6,7 @@ module Nestful
6
6
  @callbacks[type] ||= []
7
7
  end
8
8
 
9
- attr_reader :url, :options, :format
9
+ attr_reader :options, :format, :url
10
10
  attr_accessor :params, :body, :buffer, :method, :headers, :callbacks, :raw, :extension
11
11
 
12
12
  # Connection options
@@ -14,20 +14,18 @@ module Nestful
14
14
 
15
15
  def initialize(url, options = {})
16
16
  @url = url.to_s
17
+
17
18
  @options = options
18
- @options.each {|key, val|
19
+ @options.each do |key, val|
19
20
  method = "#{key}="
20
21
  send(method, val) if respond_to?(method)
21
- }
22
+ end
23
+
22
24
  self.method ||= :get
23
- self.format ||= :blank
24
- self.headers ||= {}
25
25
  self.params ||= {}
26
+ self.headers ||= {}
26
27
  self.body ||= ''
27
-
28
- if self.uri.query
29
- populate_query_params
30
- end
28
+ self.format ||= :blank
31
29
  end
32
30
 
33
31
  def format=(mime_or_format)
@@ -46,17 +44,33 @@ module Nestful
46
44
  conn
47
45
  end
48
46
 
47
+ def url=(value)
48
+ @url = value
49
+ @uri = nil
50
+ @url
51
+ end
52
+
49
53
  def uri
50
- http_url = url.match(/^http/) ? url : "http://#{url}"
51
- uri = URI.parse(http_url)
52
- uri.path = "/" if uri.path.empty?
54
+ return @uri if @uri
55
+
56
+ url = @url.match(/^http/) ? @url : "http://#{@url}"
57
+
58
+ @uri = URI.parse(url)
59
+ @uri.path = "/" if @uri.path.empty?
60
+
53
61
  if extension
54
62
  extension = format.extension if extension.is_a?(Boolean)
55
- uri.path += ".#{extension}"
56
- end
57
- uri
63
+ @uri.path += ".#{extension}"
64
+ end
65
+
66
+ @uri.query.split("&").each do |res|
67
+ key, value = res.split("=")
68
+ @params[key] = value
69
+ end if @uri.query
70
+
71
+ @uri
58
72
  end
59
-
73
+
60
74
  def path
61
75
  uri.path
62
76
  end
@@ -74,23 +88,26 @@ module Nestful
74
88
  callback(:before_request, self)
75
89
  result = nil
76
90
  if [:post, :put].include?(method)
77
- connection.send(method, path, encoded, headers) {|res|
78
- result = decoded(res)
79
- result.class_eval { attr_accessor :response }
80
- result.response = res
81
- }
91
+ connection.send(method, path, encoded, headers) do |res|
92
+ result = decoded(res)
93
+ result.class_eval { attr_accessor :response }
94
+ result.response = res
95
+ end
82
96
  else
83
- connection.send(method, query_path, headers) {|res|
84
- result = decoded(res)
85
- result.class_eval { attr_accessor :response }
86
- result.response = res
87
- }
97
+ connection.send(method, query_path, headers) do |res|
98
+ result = decoded(res)
99
+ result.class_eval { attr_accessor :response }
100
+ result.response = res
101
+ end
88
102
  end
89
103
  callback(:after_request, self, result)
90
104
  result
105
+ rescue Redirection => error
106
+ self.url = error.response['Location']
107
+ execute
91
108
  end
92
109
 
93
- protected
110
+ protected
94
111
  def encoded
95
112
  params.any? ? format.encode(params) : body
96
113
  end
@@ -101,10 +118,10 @@ module Nestful
101
118
  size = 0
102
119
  total = result.content_length
103
120
 
104
- result.read_body {|chunk|
121
+ result.read_body do |chunk|
105
122
  callback(:progress, self, total, size += chunk.size)
106
123
  data.write(chunk)
107
- }
124
+ end
108
125
 
109
126
  data.rewind
110
127
  data
@@ -115,15 +132,6 @@ module Nestful
115
132
  end
116
133
  end
117
134
 
118
- def populate_query_params
119
- uri_query = self.uri.query.split("&").inject({}) {|hash, res|
120
- key, value = res.split("=")
121
- hash[key] = value
122
- hash
123
- }
124
- self.params.merge!(uri_query)
125
- end
126
-
127
135
  def callbacks(type = nil)
128
136
  @callbacks ||= {}
129
137
  return @callbacks unless type
@@ -30,5 +30,9 @@ module Nestful
30
30
  def json_post(params = nil)
31
31
  post(:format => :json, :params => params)
32
32
  end
33
+
34
+ def request(options = {})
35
+ Request.new(url, options.merge(@options)).execute
36
+ end
33
37
  end
34
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-15 00:00:00.000000000Z
12
+ date: 2012-01-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70154433041120 !ruby/object:Gem::Requirement
16
+ requirement: &70352776847120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.0.0.beta
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70154433041120
24
+ version_requirements: *70352776847120
25
25
  description: Simple Ruby HTTP/REST client with a sane API
26
26
  email: info@eribium.org
27
27
  executables: []