fluffy-ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5293108ca23502a00f4b6692fc74e9ee94c6a449
4
- data.tar.gz: 5af10d47249e648805cd4f558868cee181518e35
3
+ metadata.gz: ff7ee179b75b1a98e5e4555b43c5d712885bb148
4
+ data.tar.gz: b28e79d8f015d1c4425d7116584bcf9b78d84c72
5
5
  SHA512:
6
- metadata.gz: 9c1ac80ca83f6f1bccad05ed5fe7b1356e8974553674f3e098db7af75886746d09c3d98ee16113881fc778e8a8d870ddec3aea05f801bc6ef24d71e5442d5a1e
7
- data.tar.gz: 068f82d4fcdcddd8bb6313ad7de89df9010e1c458ded93c2f0604c41591ca22579fa402032c29612dc42ec96fe2cae978c3f17b6148fa9d1b5d3a85fd9bb9760
6
+ metadata.gz: 50d36445fba495fbbc532ce88abdf274b72226c05889a6cab4f745800777866a715b937530118e008b972837f3bc37373ecb934388d5d15eefc12b3b495f5f2e
7
+ data.tar.gz: 3e1ffe1d7ad28c6cf2e7dde01b53d88e3ef33d1a4b28225b26ff2f0dac568cd10c03072294d5434c6002cf53fa21ec1108bdd6cb8d514445f1272481f1fc27d8
data/README.md CHANGED
@@ -7,6 +7,8 @@ To install it simply issue the following command:
7
7
  ```
8
8
  gem install fluffy-ruby
9
9
  ```
10
+ ## Usage
11
+ Online documentation is available at [rubydoc](http://www.rubydoc.info/gems/fluffy-ruby/).
10
12
 
11
13
  ## Contact
12
14
  Matteo Cerutti - matteo.cerutti@hotmail.co.uk
data/lib/fluffy/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'exceptions'
2
+
1
3
  module Fluffy
2
4
  module API
3
5
  @@api = nil
@@ -6,51 +8,58 @@ module Fluffy
6
8
  @@api
7
9
  end
8
10
 
9
- # @return [HTTPClient] HTTP client instance
10
- attr_reader :http
11
- # @return [String] Fluffy REST API's URL
12
- attr_reader :url
11
+ class RESTClient
12
+ # @return [HTTPClient] HTTP client instance
13
+ attr_reader :http
14
+ # @return [String] Fluffy REST API's URL
15
+ attr_reader :url
13
16
 
14
- # Create a Fluffy Client API object
15
- #
16
- # @param url [String] Fluffy REST API's URL
17
- #
18
- def initialize(url:)
19
- @http = HTTPClient.new
20
- @url = url
21
- end
17
+ # Create a Fluffy Client API object
18
+ #
19
+ # @param url [String] Fluffy REST API's URL
20
+ #
21
+ def initialize(url:)
22
+ @http = HTTPClient.new
23
+ @url = url
24
+ end
22
25
 
23
- # Perform a HTTP GET request
24
- #
25
- # @param endpoint [Array[String], String] HTTP API endpoint
26
- # @param query [String] HTTP API query parameters
27
- # @return [Array[Hash], Hash] API JSON response
28
- #
29
- def get(endpoint:, query: nil)
30
- resp = self.http.get([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), query, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
31
- JSON.parse(resp.body)
32
- end
26
+ # Perform a HTTP GET request
27
+ #
28
+ # @param endpoint [Array[String], String] HTTP API endpoint
29
+ # @param query [String] HTTP API query parameters
30
+ # @return [Array[Hash], Hash] API JSON response
31
+ #
32
+ def get(endpoint:, query: nil)
33
+ resp = self.http.get([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), query, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
34
+ data = JSON.parse(resp.body)
35
+ raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400
36
+ data
37
+ end
33
38
 
34
- # Perform a HTTP POST request
35
- #
36
- # @param endpoint [Array[String], String] HTTP API endpoint
37
- # @param params [Hash] HTTP API body
38
- # @return [Hash, nil] API JSON response
39
- #
40
- def post(endpoint:, params: {})
41
- puts [self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/')
42
- resp = self.http.post([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), params.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
43
- JSON.parse(resp.body)
44
- end
39
+ # Perform a HTTP POST request
40
+ #
41
+ # @param endpoint [Array[String], String] HTTP API endpoint
42
+ # @param params [Hash] HTTP API body
43
+ # @return [Hash, nil] API JSON response
44
+ #
45
+ def post(endpoint:, params: {})
46
+ resp = self.http.post([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), params.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
47
+ data = JSON.parse(resp.body)
48
+ raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400
49
+ data['message']
50
+ end
45
51
 
46
- # Perform a HTTP DELETE request
47
- #
48
- # @param endpoint [Array[String], String] HTTP API endpoint
49
- # @return [Hash, nil] API JSON response
50
- #
51
- def delete(endpoint:)
52
- resp = self.http.delete([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
53
- JSON.parse(resp.body)
52
+ # Perform a HTTP DELETE request
53
+ #
54
+ # @param endpoint [Array[String], String] HTTP API endpoint
55
+ # @return [Hash, nil] API JSON response
56
+ #
57
+ def delete(endpoint:)
58
+ resp = self.http.delete([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
59
+ data = JSON.parse(resp.body)
60
+ raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400
61
+ data['message']
62
+ end
54
63
  end
55
64
  end
56
65
  end
data/lib/fluffy/client.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'httpclient'
2
2
  require 'json'
3
3
 
4
- require_relative 'client/api'
4
+ #require_relative 'api'
5
5
  require_relative 'sessions'
6
6
 
7
7
  module Fluffy
@@ -19,7 +19,7 @@ module Fluffy
19
19
  def initialize(url:, version: 1)
20
20
  @url = url
21
21
  @version = version
22
- @@api = API.new(url: URI.join(url, "v#{version}"))
22
+ @@api = RESTClient.new(url: URI.join(url, "v#{version}"))
23
23
  @sessions = Sessions.new
24
24
  end
25
25
 
@@ -0,0 +1,15 @@
1
+ module Fluffy
2
+ class APIError < Exception
3
+ # @return [Integer] HTTP error code
4
+ attr_reader :code
5
+ # @return [String] Reason for error message
6
+ attr_reader :error
7
+
8
+ def initialize(msg, error, code)
9
+ @code = code
10
+ @error = error
11
+
12
+ super(msg)
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  module Fluffy
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
4
  def self.version
5
5
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluffy-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Cerutti
@@ -48,6 +48,7 @@ files:
48
48
  - README.md
49
49
  - lib/fluffy/api.rb
50
50
  - lib/fluffy/client.rb
51
+ - lib/fluffy/exceptions.rb
51
52
  - lib/fluffy/session.rb
52
53
  - lib/fluffy/session/addressbook.rb
53
54
  - lib/fluffy/session/chains.rb