http_mini 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0619d2e911794c66aace9401b1b720b7023ea4f0
4
- data.tar.gz: 57cd6f3882b48473a42477d9f62f009a466e1183
3
+ metadata.gz: b2043cd9aeffdf055e5fde5eadcc7f0b41f77e15
4
+ data.tar.gz: fe4aa4a6c61b5e69adb0e507389264e89d2a83d4
5
5
  SHA512:
6
- metadata.gz: 9d1d1229de8c2fc8b743c193aea9f3fd7b7e3e793829d18021c711d772d56c12b6b273852a7ef4a0dc8237c631a94ef5397275c0458215da4ff1619ef5c8217a
7
- data.tar.gz: 3c5960d0b9f089b701a62ccf52ad252b674790fd5f78f27b4d673208884d85da4a52947679ad1e8426b6720657d2560a437a329e27ee72e160b0495c4e9eddbd
6
+ metadata.gz: f4c28d37b8acafd27471987a48633b3fe36c27e052ff80cd8f879603963078bab050a29e55b0509324a1e5ef236fb896bdfbf571dc9e7809c4cc397209af2e88
7
+ data.tar.gz: 32119819d2015104e88e0aeb377522a0e2eede3d5dbf20172fc9d439c658b09b93b4b893f9b61b09616998947a9fcca11075a109b5d660d8abf9c24bb6c80aec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## vNEXT
2
2
 
3
+ ## v0.2.3
4
+ * Allow to set request headers via options hash
5
+ * Remove unnecessary code since 0.2.2 improved uri handling
6
+
7
+ ## v0.2.2
8
+ * Improve uri handling
9
+
3
10
  ## v0.2.1
4
11
  * Add license to `gemspec`
5
12
 
data/README.md CHANGED
@@ -31,6 +31,9 @@ a one liner to ping or get the response from a web server.
31
31
  # or just the path
32
32
  http_request_html = http.path('/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPRequest.html').get.body
33
33
 
34
+ # Set http headers
35
+ puts HttpMini.new('http://www.google.com', headers: {'user-agent' => 'Fancy UserAgent Name'}).head.code
36
+
34
37
  ## HTTP verbs Support
35
38
 
36
39
  * HEAD
@@ -40,6 +43,12 @@ a one liner to ping or get the response from a web server.
40
43
  * DELETE
41
44
  * OPTIONS
42
45
 
46
+ ## Request options
47
+
48
+ * `:headers` - http headers, defaults to {}
49
+ * `:open_timeout` - number of seconds to wait for the connection to open, defaults to 2
50
+ * `:read_timeout` - number of seconds to wait for one block to be read, defaults to 2
51
+
43
52
  ## Author
44
53
 
45
54
  Jerome Touffe-Blin, [@jtblin](https://twitter.com/jtlbin), [http://www.linkedin.com/in/jtblin](http://www.linkedin.com/in/jtblin)
data/lib/http_mini.rb CHANGED
@@ -10,36 +10,36 @@ class HttpMini
10
10
  IGNORE_ERROR = true
11
11
 
12
12
  def self.VERSION
13
- '0.2.2'
13
+ '0.2.3'
14
14
  end
15
15
 
16
- def initialize(url, opts = {})
17
- self.uri = url
16
+ def initialize(uri, opts = {})
17
+ self.uri = uri
18
18
  self.opts = opts
19
19
  end
20
20
 
21
21
  def head
22
- request { |http| http.head(full_path) }
22
+ request Net::HTTP::Head.new(full_path)
23
23
  end
24
24
 
25
25
  def get
26
- request { |http| http.get(full_path) }
26
+ request Net::HTTP::Get.new(full_path)
27
27
  end
28
28
 
29
29
  def post(data)
30
- request { |http| http.post(full_path, data) }
30
+ request Net::HTTP::Post.new(full_path), data
31
31
  end
32
32
 
33
33
  def put(data)
34
- request { |http| http.put(full_path, data) }
34
+ request Net::HTTP::Put.new(full_path), data
35
35
  end
36
36
 
37
37
  def delete
38
- request { |http| http.delete(full_path) }
38
+ request Net::HTTP::Delete.new(full_path)
39
39
  end
40
40
 
41
41
  def options
42
- request { |http| http.options(full_path) }
42
+ request Net::HTTP::Options.new(full_path)
43
43
  end
44
44
 
45
45
  def poke
@@ -57,7 +57,7 @@ class HttpMini
57
57
  end
58
58
 
59
59
  def host
60
- @uri.host || @uri.path.split('/').first
60
+ @uri.host
61
61
  end
62
62
 
63
63
  def port
@@ -65,7 +65,7 @@ class HttpMini
65
65
  end
66
66
 
67
67
  def path(path=nil)
68
- path.nil? ? clean_path(@uri.path) : set_path(path)
68
+ path.nil? ? default_path(@uri.path) : set_path(path)
69
69
  end
70
70
 
71
71
  private
@@ -78,8 +78,8 @@ class HttpMini
78
78
  uri.match(/https?:\/\//) ? uri : "http://#{uri}"
79
79
  end
80
80
 
81
- def request
82
- Net::HTTP.start(host, port, :use_ssl => ssl?) {|http| set_timeout(http) and yield(http) }
81
+ def request(req, data=nil)
82
+ Net::HTTP.start(host, port, :use_ssl => ssl?) { |http| set_timeout(http) and http.request(set_headers(req), data) }
83
83
  end
84
84
 
85
85
  def ssl?
@@ -90,20 +90,20 @@ class HttpMini
90
90
  http.open_timeout, http.read_timeout = timeouts
91
91
  end
92
92
 
93
- def full_path
94
- @uri.query ? path + '?' + @uri.query : path
93
+ def set_headers(req)
94
+ headers.each { |key, value| req[key] = value } and return req
95
95
  end
96
96
 
97
- def clean_path(path)
98
- default_path remove_host_from_path(path)
97
+ def headers
98
+ opts[:headers] || {}
99
99
  end
100
100
 
101
- def default_path(path)
102
- path.to_s.empty? ? '/' : path
101
+ def full_path
102
+ @uri.query ? path + '?' + @uri.query : path
103
103
  end
104
104
 
105
- def remove_host_from_path(path)
106
- path.to_s.gsub Regexp.new('^' + host), ''
105
+ def default_path(path)
106
+ path.to_s.empty? ? '/' : path
107
107
  end
108
108
 
109
109
  def set_path(path)
data/lib/request.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'net/http'
2
+ require "uri"
3
+
4
+ class Request
5
+
6
+ @@request = HttpMini.new nil
7
+
8
+ class << self
9
+
10
+ def get(uri, opts={})
11
+ @@request.opts = opts
12
+ @@request.uri(uri).get
13
+ end
14
+
15
+ end
16
+
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_mini
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerome Touffe-Blin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-05 00:00:00.000000000 Z
11
+ date: 2013-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -76,6 +76,7 @@ extra_rdoc_files:
76
76
  - CHANGELOG.md
77
77
  files:
78
78
  - lib/http_mini.rb
79
+ - lib/request.rb
79
80
  - LICENSE
80
81
  - README.md
81
82
  - CHANGELOG.md