hyperclient 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +6 -2
- data/examples/{cs.rb → cyberscore.rb} +24 -4
- data/lib/hyperclient/http.rb +14 -1
- data/lib/hyperclient/version.rb +1 -1
- data/lib/hyperclient.rb +10 -2
- data/test/hyperclient/http_test.rb +12 -0
- metadata +5 -5
data/Readme.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
# Hyperclient
|
1
|
+
# Hyperclient
|
2
|
+
[![Build Status](https://secure.travis-ci.org/codegram/hyperclient.png)](http://travis-ci.org/codegram/hyperclient)
|
3
|
+
[![Dependency Status](https://gemnasium.com/codegram/hyperclient.png)](http://gemnasium.com/codegram/hyperclient)
|
4
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/codegram/hyperclient)
|
2
5
|
|
3
6
|
Hyperclient is a Ruby Hypermedia API client written in Ruby.
|
4
7
|
|
5
8
|
## Documentation
|
6
9
|
|
7
|
-
[Hyperclient
|
10
|
+
[Hyperclient API documentation on rdoc.info][rdoc]
|
8
11
|
|
9
12
|
## Usage
|
10
13
|
|
@@ -16,6 +19,7 @@ class MyAPIClient
|
|
16
19
|
|
17
20
|
entry_point 'http://myapp.com/api'
|
18
21
|
auth :digest, 'user', 'password'
|
22
|
+
http_options {headers: {'accept-encoding' => 'deflate, gzip'}}
|
19
23
|
end
|
20
24
|
````
|
21
25
|
|
@@ -1,9 +1,29 @@
|
|
1
1
|
require 'hyperclient'
|
2
2
|
|
3
|
-
class
|
3
|
+
class Cyberscore
|
4
4
|
include Hyperclient
|
5
5
|
|
6
6
|
entry_point 'http://cs-api.heroku.com/api/'
|
7
|
+
|
8
|
+
def news
|
9
|
+
links.feeds.links.submissions.resources.news
|
10
|
+
end
|
11
|
+
|
12
|
+
def submissions
|
13
|
+
links.feeds.links.submissions.resources.submissions
|
14
|
+
end
|
15
|
+
|
16
|
+
def games
|
17
|
+
links.feeds.links.games.resources.games
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_game(name)
|
21
|
+
links.feeds.links.submissions.post({name: name})
|
22
|
+
end
|
23
|
+
|
24
|
+
def motd
|
25
|
+
attributes['motd']
|
26
|
+
end
|
7
27
|
end
|
8
28
|
|
9
29
|
def print_resources(resources)
|
@@ -23,7 +43,7 @@ def print_games(games)
|
|
23
43
|
end
|
24
44
|
|
25
45
|
|
26
|
-
api =
|
46
|
+
api = Cyberscore.new
|
27
47
|
|
28
48
|
puts "Let's inspect the API:"
|
29
49
|
puts "\n"
|
@@ -33,7 +53,7 @@ print_resources(api.links)
|
|
33
53
|
puts "\n"
|
34
54
|
|
35
55
|
puts 'How is the server feeling today?'
|
36
|
-
puts api.
|
56
|
+
puts api.motd
|
37
57
|
puts "\n"
|
38
58
|
|
39
59
|
puts "Let's read the feeds:"
|
@@ -41,4 +61,4 @@ print_resources(api.links.feeds.links)
|
|
41
61
|
puts "\n"
|
42
62
|
|
43
63
|
puts "I like games!"
|
44
|
-
print_games(api.
|
64
|
+
print_games(api.games)
|
data/lib/hyperclient/http.rb
CHANGED
@@ -25,6 +25,7 @@ module Hyperclient
|
|
25
25
|
def initialize(resource, options = {})
|
26
26
|
@resource = resource
|
27
27
|
authenticate(options[:auth]) if options && options.include?(:auth)
|
28
|
+
headers(options[:headers]) if options && options.include?(:headers)
|
28
29
|
end
|
29
30
|
|
30
31
|
# Public: Sends a GET request the the resource url.
|
@@ -74,7 +75,7 @@ module Hyperclient
|
|
74
75
|
end
|
75
76
|
|
76
77
|
private
|
77
|
-
# Internal: Sets the
|
78
|
+
# Internal: Sets the authentication method for HTTParty.
|
78
79
|
#
|
79
80
|
# options - An options Hash to set the authentication options.
|
80
81
|
# :type - A String or Symbol to set the authentication type.
|
@@ -86,5 +87,17 @@ module Hyperclient
|
|
86
87
|
auth_method = options[:type].to_s + '_auth'
|
87
88
|
self.class.send(auth_method, *options[:credentials])
|
88
89
|
end
|
90
|
+
|
91
|
+
# Internal: Adds default headers for all the requests.
|
92
|
+
#
|
93
|
+
# headers - A Hash with the header.
|
94
|
+
#
|
95
|
+
# Example:
|
96
|
+
# headers({'accept-encoding' => 'deflate, gzip'})
|
97
|
+
#
|
98
|
+
# Returns nothing.
|
99
|
+
def headers(headers)
|
100
|
+
self.class.send(:headers, headers)
|
101
|
+
end
|
89
102
|
end
|
90
103
|
end
|
data/lib/hyperclient/version.rb
CHANGED
data/lib/hyperclient.rb
CHANGED
@@ -52,8 +52,16 @@ module Hyperclient
|
|
52
52
|
http_options({auth: {type: type, credentials: [user, password]}})
|
53
53
|
end
|
54
54
|
|
55
|
-
# Public:
|
56
|
-
#
|
55
|
+
# Public: Sets the HTTP options that will be used to initialize
|
56
|
+
# Hyperclient::HTTP.
|
57
|
+
#
|
58
|
+
# options - A Hash with options to pass to HTTP.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
#
|
62
|
+
# http_options {headers: {'accept-encoding' => 'deflate, gzip'}}
|
63
|
+
#
|
64
|
+
# Returns a Hash.
|
57
65
|
def http_options(options = {})
|
58
66
|
@@http_options ||= {}
|
59
67
|
@@http_options.merge!(options)
|
@@ -23,6 +23,18 @@ module Hyperclient
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
describe 'headers' do
|
27
|
+
it 'sets headers from the given option' do
|
28
|
+
http = HTTP.new(resource, {headers: {'accept-encoding' => 'deflate, gzip'}})
|
29
|
+
|
30
|
+
stub_request(:get, 'api.example.org/productions/1').
|
31
|
+
with(headers: {'Accept-Encoding' => 'deflate, gzip'}).
|
32
|
+
to_return(body: 'This is the resource')
|
33
|
+
|
34
|
+
http.get
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
26
38
|
describe 'get' do
|
27
39
|
it 'sends a GET request and returns the response body' do
|
28
40
|
stub_request(:get, 'api.example.org/productions/1').
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyperclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
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-06-
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
@@ -92,7 +92,7 @@ files:
|
|
92
92
|
- MIT-LICENSE
|
93
93
|
- Rakefile
|
94
94
|
- Readme.md
|
95
|
-
- examples/
|
95
|
+
- examples/cyberscore.rb
|
96
96
|
- examples/hal_shop.rb
|
97
97
|
- hyperclient.gemspec
|
98
98
|
- lib/hyperclient.rb
|
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
segments:
|
128
128
|
- 0
|
129
|
-
hash:
|
129
|
+
hash: -2046368484385540531
|
130
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
131
|
none: false
|
132
132
|
requirements:
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash:
|
138
|
+
hash: -2046368484385540531
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
141
|
rubygems_version: 1.8.23
|