xsr 1.2.2 → 1.3.0

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: 39ab889ea831529ef693fb4e8155a602fc967347
4
- data.tar.gz: 197db55a65aa0f54fd1e8f4b0ef886d73c0ad902
3
+ metadata.gz: 931719af15817041f1826a036bd4315cd05ce1df
4
+ data.tar.gz: 26c3470bb7deb98cdce115aad6dc55afdfe31d7f
5
5
  SHA512:
6
- metadata.gz: f3601a3830756dd838531c5d92f5291687c3cfd656efb281a58f490d4792114495d1a077bf2352209210fc69f21701587a099875b673e8bf67e7702d2463bff0
7
- data.tar.gz: 87525098dbf9e68d27db92dc02d455f5ce7d58830a0741b65eddb2e92126630903ac22241bfb3e1f30e6a6b87332143402bf25f6255477691473202e7bacf3db
6
+ metadata.gz: 674f547fdd8f62a5921ff148023e80d58aac8d58f0c414045512ea688a36ea1065d7f4f7c4b39f1298374bd2c0f56f6d43ac01130c4036ed67c47893144d36ff
7
+ data.tar.gz: dea4adc7133a5ba9adc51ea096c6262ea2c6a195fde4a6e72205ef05e9991646e632c00aa719c569781a4ae16dc85c6175409e5d014a9c1513fcdc8787a3e6bc
data/lib/xsr/client.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module XSR
2
+ # XSR Client class, holding methods to handle HTTP requests
2
3
  class Client
3
4
  attr_accessor :base_url
4
5
  attr_accessor :default_header
@@ -24,20 +25,22 @@ module XSR
24
25
  request_with_data(path, options) { |uri| Net::HTTP::Get.new(uri.to_s) }
25
26
  end
26
27
 
27
- private
28
+ private
29
+
28
30
  def request_with_data(path, options = {})
29
- args = options[:args] && URI::encode( options[:args].map{|k,v| "#{k}=#{v}"}.join('&').to_s )
30
- uri = URI( @base_url.to_s + path.to_s + '?' + args.to_s )
31
+ # Set URI & arguments
32
+ args = options[:args] && URI.encode(options[:args].map { |k, v| "#{k}=#{v}" }.join('&').to_s)
33
+ uri = URI(@base_url.to_s + path.to_s + '?' + args.to_s)
31
34
 
32
35
  req = yield(uri)
33
36
 
34
37
  # Set headers
35
38
  req['Content-Type'] = 'application/json'
36
- @base_header && @base_header.each{ |k,v| req[k.to_s] = v }
37
- options[:header] && options[:header].each{ |k,v| req[k.to_s] = v }
39
+ @base_header && @base_header.each { |k, v| req[k.to_s] = v }
40
+ options[:header] && options[:header].each { |k, v| req[k.to_s] = v }
38
41
 
39
42
  # Set body
40
- req.body = options[:body] && MultiJson.dump( options[:body] )
43
+ req.body = options[:body] && MultiJson.dump(options[:body])
41
44
 
42
45
  http_session = Net::HTTP.new(uri.host, uri.port)
43
46
 
@@ -46,7 +49,7 @@ module XSR
46
49
  http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
47
50
  end
48
51
 
49
- XSR::Response.new( http_session.start{ |http| http.request(req) } )
52
+ XSR::Response.new http_session.start { |http| http.request(req) }
50
53
  end
51
54
  end
52
55
  end
data/lib/xsr/response.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  module XSR
2
- class Response
2
+ # Response object is created by Client after a HTTP call
3
+ class Response
3
4
  attr_reader :http_response
4
5
 
5
6
  def initialize(http_response)
6
7
  @http_response = http_response
7
8
  end
8
-
9
+
9
10
  def success?
10
11
  @http_response.is_a? Net::HTTPSuccess
11
12
  end
@@ -18,12 +19,20 @@ module XSR
18
19
  @http_response.is_a? Net::HTTPNotFound
19
20
  end
20
21
 
22
+ def unauthorized?
23
+ @http_response.is_a? Net::HTTPUnauthorized
24
+ end
25
+
26
+ def forbidden?
27
+ @http_response.is_a? Net::HTTPForbidden
28
+ end
29
+
21
30
  def server_error?
22
31
  @http_response.is_a? Net::HTTPServerError
23
32
  end
24
33
 
25
34
  def body
26
- MultiJson.load @http_response.body, symbolize_keys: true
35
+ MultiJson.load @http_response.body, symbolize_keys: true
27
36
  end
28
37
  end
29
38
  end
data/lib/xsr.rb CHANGED
@@ -4,4 +4,4 @@ require 'multi_json'
4
4
  require 'openssl'
5
5
 
6
6
  require 'xsr/client'
7
- require 'xsr/response'
7
+ require 'xsr/response'
data/test/client_test.rb CHANGED
@@ -1,68 +1,78 @@
1
1
  require 'xsr'
2
2
  require_relative 'helper'
3
3
 
4
+ # Tests for Client class
4
5
  module ClientTest
5
6
  prepare do
6
- @client = XSR::Client.new( base_url: 'http://httpbin.org' )
7
+ @client = XSR::Client.new(base_url: 'http://httpbin.org')
7
8
  end
8
9
 
9
- test "get request" do
10
+ test 'get request' do
10
11
  resp = @client.get('/get')
11
-
12
+
12
13
  assert resp.http_response.instance_of?(Net::HTTPOK)
13
14
  end
14
15
 
15
- test "post request" do
16
- req = {some_key: 'some_value'}
17
- resp = @client.post( '/post', {body: req} )
18
-
16
+ test 'post request' do
17
+ req = { some_key: 'some_value' }
18
+ resp = @client.post('/post', body: req)
19
+
19
20
  assert resp.http_response.instance_of?(Net::HTTPOK)
20
- assert_equal MultiJson.load(resp.http_response.body, symbolize_keys: true)[:json], req
21
+ assert_equal MultiJson.load(resp.http_response.body,
22
+ symbolize_keys: true)[:json], req
21
23
  end
22
24
 
23
25
  test 'put request' do
24
- req = {some_key: 'some_value'}
25
- resp = @client.put( '/put', {body: req} )
26
+ req = { some_key: 'some_value' }
27
+ resp = @client.put('/put', body: req)
26
28
 
27
29
  assert resp.http_response.instance_of?(Net::HTTPOK)
28
- assert_equal MultiJson.load(resp.http_response.body, symbolize_keys: true)[:json], req
30
+ assert_equal MultiJson.load(resp.http_response.body,
31
+ symbolize_keys: true)[:json], req
29
32
  end
30
33
 
31
34
  test 'delete request' do
32
- req = {some_key: 'some_value'}
33
- resp = @client.delete( '/delete', {body: req} )
35
+ req = { some_key: 'some_value' }
36
+ resp = @client.delete('/delete', body: req)
34
37
 
35
38
  assert resp.http_response.instance_of?(Net::HTTPOK)
36
- assert_equal MultiJson.load(resp.http_response.body, symbolize_keys: true)[:json], req
39
+ assert_equal MultiJson.load(resp.http_response.body,
40
+ symbolize_keys: true)[:json], req
37
41
  end
38
42
 
39
43
  test 'headers are passed to request' do
40
- h = { 'Some-Header' => 'some_value'}
41
- resp = @client.get( '/headers', {header: h} )
44
+ h = { 'Some-Header' => 'some_value' }
45
+ resp = @client.get('/headers', header: h)
42
46
 
43
47
  assert resp.http_response.instance_of?(Net::HTTPOK)
44
- assert_equal MultiJson.load(resp.http_response.body)['headers']['Some-Header'], 'some_value'
48
+
49
+ header = MultiJson.load(resp.http_response.body)['headers']['Some-Header']
50
+ assert_equal header, 'some_value'
45
51
  end
46
52
 
47
53
  test 'default headers are passed to request' do
48
- h = { 'Some-Header' => 'some_value'}
49
- resp = XSR::Client.new(header: h).get( 'https://httpbin.org/headers' )
54
+ h = { 'Some-Header' => 'some_value' }
55
+ resp = XSR::Client.new(header: h).get('https://httpbin.org/headers')
50
56
 
51
57
  assert resp.http_response.instance_of?(Net::HTTPOK)
52
- assert_equal MultiJson.load(resp.http_response.body)['headers']['Some-Header'], 'some_value'
58
+
59
+ header = MultiJson.load(resp.http_response.body)['headers']['Some-Header']
60
+ assert_equal header, 'some_value'
53
61
  end
54
62
 
55
63
  test 'query string arguments are passed to request' do
56
- p = {some_arg: 'some_value'}
57
- resp = @client.get( '/get', {args: p} )
64
+ p = { some_arg: 'some_value' }
65
+ resp = @client.get('/get', args: p)
66
+
58
67
  assert resp.http_response.instance_of?(Net::HTTPOK)
59
68
 
60
- assert_equal MultiJson.load(resp.http_response.body)['args']['some_arg'], 'some_value'
69
+ argument = MultiJson.load(resp.http_response.body)['args']['some_arg']
70
+ assert_equal argument, 'some_value'
61
71
  end
62
72
 
63
73
  test 'https request' do
64
74
  resp = XSR::Client.new.get('https://httpbin.org/get')
65
-
75
+
66
76
  assert resp.http_response.instance_of?(Net::HTTPOK)
67
77
  end
68
- end
78
+ end
data/test/helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- if ENV["TRAVIS"]
2
- require "codeclimate-test-reporter"
1
+ if ENV['TRAVIS']
2
+ require 'codeclimate-test-reporter'
3
3
  CodeClimate::TestReporter.start
4
4
  end
@@ -1,56 +1,69 @@
1
1
  require 'xsr'
2
2
  require_relative 'helper'
3
3
 
4
+ # Tests for Response class
4
5
  module ResponseTest
5
6
  prepare do
6
- @client = XSR::Client.new( base_url: 'http://httpbin.org' )
7
+ @client = XSR::Client.new(base_url: 'http://httpbin.org')
7
8
  end
8
9
 
9
- test "status 200 is a successful response" do
10
+ test 'status 200 gets a successful response' do
10
11
  res = @client.get('/status/200')
11
12
  assert res.success?
12
13
  end
13
14
 
14
- test "status 201 is a successful response" do
15
+ test 'status 201 gets a successful response' do
15
16
  res = @client.get('/status/201')
16
17
  assert res.success?
17
18
  end
18
19
 
19
- test "status 202 is a successful response" do
20
+ test 'status 202 gets a successful response' do
20
21
  res = @client.get('/status/202')
21
22
  assert res.success?
22
23
  end
23
24
 
24
- test "status 400 is a successful response" do
25
+ test 'status 400 gets a Bad Request response' do
25
26
  res = @client.get('/status/400')
26
27
  assert !res.success?
27
28
  assert res.bad_request?
28
29
  end
29
30
 
30
- test "status 404 is a successful response" do
31
+ test 'status 401 gets an Unauthorized response' do
32
+ res = @client.get('/status/401')
33
+ assert !res.success?
34
+ assert res.unauthorized?
35
+ end
36
+
37
+ test 'status 403 gets a Forbidden response' do
38
+ res = @client.get('/status/403')
39
+ assert !res.success?
40
+ assert res.forbidden?
41
+ end
42
+
43
+ test 'status 404 gets a Not Found response' do
31
44
  res = @client.get('/status/404')
32
45
  assert !res.success?
33
46
  assert res.not_found?
34
47
  end
35
48
 
36
- test "status 500 is a successful response" do
49
+ test 'status 500 gets a Server Error response' do
37
50
  res = @client.get('/status/500')
38
51
  assert !res.success?
39
52
  assert res.server_error?
40
53
  end
41
54
 
42
- test "simple json response" do
43
- req = {some_attrib: 'some_value'}
55
+ test 'simple json response' do
56
+ req = { some_attrib: 'some_value' }
44
57
  res = @client.post('/post', body: req)
45
-
58
+
46
59
  assert res.success?
47
60
  assert_equal res.body[:json], req
48
61
  end
49
62
 
50
- test "json response with nested attributes" do
51
- req = {something: {some_attrib: 'some_value'}}
63
+ test 'json response with nested attributes' do
64
+ req = { something: { some_attrib: 'some_value' } }
52
65
  res = @client.post('/post', body: req)
53
-
66
+
54
67
  assert res.success?
55
68
  assert_equal res.body[:json], req
56
69
  end
data/xsr.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'xsr'
3
- s.version = '1.2.2'
4
- s.summary = "XSR: eXtremely Simple REST Client"
5
- s.description = "A very simple library to talk to a REST/JSON API"
6
- s.authors = ["matiasow"]
3
+ s.version = '1.3.0'
4
+ s.summary = 'XSR: eXtremely Simple REST Client'
5
+ s.description = 'A very simple library to talk to a REST/JSON API'
6
+ s.authors = ['matiasow']
7
7
  s.email = ['matiasow@gmail.com']
8
8
  s.homepage = 'http://github.com/matiasow/xsr'
9
9
  s.license = 'MIT'
10
10
 
11
11
  s.files = `git ls-files`.split("\n")
12
12
 
13
- s.add_dependency "multi_json", '~> 1.11', '>= 1.11.0'
14
- s.add_development_dependency "cutest", '~> 1.2', '>= 1.2.2'
13
+ s.add_dependency 'multi_json', '~> 1.11', '>= 1.11.0'
14
+ s.add_development_dependency 'cutest', '~> 1.2', '>= 1.2.2'
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xsr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - matiasow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-25 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json