moonrope-client 1.0.0 → 1.0.5

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
- SHA1:
3
- metadata.gz: 2dd9355b29944c25be8b737ed5e21048753719c4
4
- data.tar.gz: f22c89a8a92620a2d4eee4b7fb93976c0513d00c
2
+ SHA256:
3
+ metadata.gz: e354b83a2648af524ba82261be24f4eade87122b4bd042c2b13d81638105d505
4
+ data.tar.gz: 0a2fef71abb2c870938b97ce60e241fb39ef1b66990be7889a938afd0059c39d
5
5
  SHA512:
6
- metadata.gz: b4007cd0fedfe8342830b7a7a0cb02dcc188c2955f3aa62e7cad9f93dc6d6b49474fb536d59f07c7a981b4b1d13207545547909a6182725a32d8fd3145b73572
7
- data.tar.gz: 2796cd53024fc5766417ab1a74c2bad370feb2adbf71606a30b81e7e05fd12772535f28fd22004124dd2b91b0bc1bb35ddb378a899ab1e6cdfbf3541625cb4c6
6
+ metadata.gz: 77438b37bc152c59ecdb5a7e4325aa33688ee2f28383fc65c538d51c798e98a00c9b8013041d5a6536f89814c49945461d0fa9e101b1c1551e61703fae509474
7
+ data.tar.gz: 5e793e6c03c223f0bec41ea11c212edf1fe7b2d938fc392a4c3c80a9cfb293b87391353b4294f775819e8a04bc1b8dabf22924798c0d8c2cbf40fb6b4c3ea694
@@ -12,6 +12,3 @@ require 'moonrope_client/responses/parameter_error'
12
12
  require 'moonrope_client/responses/access_denied'
13
13
  require 'moonrope_client/responses/validation_error'
14
14
  require 'moonrope_client/responses/paginated_collection'
15
-
16
- module MoonropeClient
17
- end
@@ -12,20 +12,20 @@ module MoonropeClient
12
12
  def initialize(host, options = {})
13
13
  @host, @options = host, options
14
14
  end
15
-
15
+
16
16
  #
17
17
  # @return [String] the endpoint hostname
18
18
  #
19
19
  attr_reader :host
20
-
20
+
21
21
  #
22
22
  # @return [String] the path prefix
23
23
  #
24
24
  def path_prefix
25
25
  @options[:path_prefix] || 'api'
26
26
  end
27
-
28
- #
27
+
28
+ #
29
29
  # @return [Boolean] whether or not SSL is enabled for requests or not
30
30
  #
31
31
  def ssl
@@ -33,26 +33,33 @@ module MoonropeClient
33
33
  end
34
34
 
35
35
  #
36
- # @return [Integer] the port to conncet to
36
+ # @return [Integer] the port to conncet to
37
37
  #
38
38
  def port
39
39
  @options[:port] || (ssl ? 443 : 80)
40
40
  end
41
-
42
- #
41
+
42
+ #
43
43
  # @return [Hash] return headers to be set on all requests to the API
44
44
  #
45
45
  def headers
46
46
  @options[:headers] || {}
47
47
  end
48
-
48
+
49
49
  #
50
50
  # @return [Integer] the version of the API to use
51
51
  #
52
52
  def version
53
53
  @options[:version] || 1
54
54
  end
55
-
55
+
56
+ #
57
+ # @return [String] the User-Agent to send with the request
58
+ #
59
+ def user_agent
60
+ @options[:user_agent] || "Moonrope Ruby Library/#{MoonropeClient::VERSION}"
61
+ end
62
+
56
63
  #
57
64
  # Make a request and return an appropriate request object.
58
65
  #
@@ -63,15 +70,19 @@ module MoonropeClient
63
70
  def request(controller, action, params = {})
64
71
  MoonropeClient::Request.new(self, controller, action, params).make
65
72
  end
66
-
73
+
74
+ def request!(controller, action, params = {})
75
+ MoonropeClient::Request.new(self, controller, action, params).make!
76
+ end
77
+
67
78
  def controller(name)
68
79
  MoonropeClient::Controller.new(self, name)
69
80
  end
70
-
81
+
71
82
  def method_missing(name, value = nil)
72
83
  value.nil? ? self.controller(name) : super
73
84
  end
74
-
85
+
75
86
  #
76
87
  # Make a request to the remote API server and return the raw output from
77
88
  # the request.
@@ -82,20 +93,33 @@ module MoonropeClient
82
93
  def raw_request(path, params = {})
83
94
  request = Net::HTTP::Post.new(path)
84
95
  request.set_form_data(params)
85
- request.add_field 'User-Agent', "Moonrope Ruby Library/#{MoonropeClient::VERSION}"
96
+ request.add_field 'User-Agent', self.user_agent
86
97
  headers.each { |k,v| request.add_field k, v }
87
98
  connection = Net::HTTP.new(self.host, self.port)
99
+ connection.open_timeout = @options[:connect_timeout] || 10
88
100
  if ssl
89
101
  connection.use_ssl = true
90
- connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
102
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
91
103
  end
92
104
  result = connection.request(request)
93
105
  case result.code.to_i
94
106
  when 200 then result.body
95
- when 400 then raise Error, "Bad request"
96
- else raise Error, "Internal server error occurred"
107
+ when 400
108
+ raise Error.new("Bad request (400)", body: result.body)
109
+ when 403
110
+ raise Error.new("Access denied (403)", body: result.body)
111
+ when 404
112
+ raise Error.new("Page not found (404)", body: result.body)
113
+ when 301..309
114
+ raise Error.new("Redirect (#{result.code})", body: result.body, location: result['Location'])
115
+ when 500
116
+ raise Error.new("Internal server error (500)", body: result.body)
117
+ else
118
+ raise Error.new("Error (#{result.code.to_i})", body: result.body)
97
119
  end
120
+ rescue Net::OpenTimeout
121
+ raise Error.new("Connection timeout to #{self.host}:#{self.port}")
98
122
  end
99
-
123
+
100
124
  end
101
- end
125
+ end
@@ -1,20 +1,24 @@
1
1
  module MoonropeClient
2
2
  class Controller
3
-
3
+
4
4
  def initialize(connection, controller_name)
5
5
  @connection = connection
6
6
  @controller_name = controller_name
7
7
  end
8
-
8
+
9
9
  attr_reader :name
10
-
10
+
11
11
  def method_missing(name, params = {})
12
12
  request(name, params)
13
13
  end
14
-
14
+
15
15
  def request(action_name, params = {})
16
- @connection.request(@controller_name, action_name, params)
16
+ if action_name =~ /\A(.*)\!\z/
17
+ @connection.request!(@controller_name, $1, params)
18
+ else
19
+ @connection.request(@controller_name, action_name, params)
20
+ end
17
21
  end
18
-
22
+
19
23
  end
20
24
  end
@@ -1,4 +1,16 @@
1
1
  module MoonropeClient
2
2
  class Error < StandardError
3
+ def initialize(message, options = {})
4
+ @message = message
5
+ @options = options
6
+ end
7
+
8
+ def to_s
9
+ @message
10
+ end
11
+
12
+ def options
13
+ @options
14
+ end
3
15
  end
4
16
  end
@@ -1,6 +1,8 @@
1
+ require 'moonrope_client/request_error'
2
+
1
3
  module MoonropeClient
2
4
  class Request
3
-
5
+
4
6
  #
5
7
  # Initialize a new request
6
8
  #
@@ -15,21 +17,30 @@ module MoonropeClient
15
17
  @action = action.to_sym
16
18
  @params = params
17
19
  end
18
-
20
+
19
21
  attr_reader :params
20
-
22
+
21
23
  def make
22
24
  raw_data_to_response_object(make_request)
23
25
  end
24
-
26
+
27
+ def make!
28
+ result = raw_data_to_response_object(make_request)
29
+ if result.success?
30
+ result
31
+ else
32
+ raise MoonropeClient::RequestError.new(result), "Request was not successful. Got #{result.class} (#{result.exception_message})."
33
+ end
34
+ end
35
+
25
36
  private
26
-
37
+
27
38
  def make_request
28
39
  params = {:params => @params.to_json}
29
40
  path = "/#{@connection.path_prefix}/v#{@connection.version}/#{@controller}/#{@action}"
30
41
  JSON.parse(@connection.raw_request(path, params))
31
42
  end
32
-
43
+
33
44
  #
34
45
  # Convert rhe result of a request into an appropriate response object
35
46
  #
@@ -48,6 +59,6 @@ module MoonropeClient
48
59
  MoonropeClient::Response.new(self, data)
49
60
  end
50
61
  end
51
-
62
+
52
63
  end
53
64
  end
@@ -0,0 +1,8 @@
1
+ module MoonropeClient
2
+ class RequestError < StandardError
3
+ def initialize(result)
4
+ @result = result
5
+ end
6
+ attr_reader :result
7
+ end
8
+ end
@@ -1,9 +1,9 @@
1
1
  module MoonropeClient
2
2
  class Response
3
-
3
+
4
4
  #
5
5
  # Initialize a new response object
6
- #
6
+ #
7
7
  # @param request [MoonropeClient::Request]
8
8
  # @param data [Hash]
9
9
  #
@@ -11,7 +11,7 @@ module MoonropeClient
11
11
  @request = request
12
12
  @data = data
13
13
  end
14
-
14
+
15
15
  #
16
16
  # Is this a successful response?
17
17
  #
@@ -20,33 +20,40 @@ module MoonropeClient
20
20
  def success?
21
21
  false
22
22
  end
23
-
24
- #
23
+
24
+ #
25
25
  # @return [String] the status of the request returned by the server
26
26
  #
27
27
  def status
28
28
  @data['status']
29
29
  end
30
-
30
+
31
31
  #
32
32
  # @return [Hash] any flags returned by the server
33
33
  def flags
34
34
  @data['flags']
35
35
  end
36
-
36
+
37
37
  #
38
38
  # @return [Object] the data returned by the server
39
39
  #
40
40
  def data
41
41
  @data['data']
42
42
  end
43
-
43
+
44
44
  #
45
45
  # @return [Float] the time the request took at the server
46
46
  #
47
47
  def time
48
48
  @data['time']
49
49
  end
50
-
50
+
51
+ #
52
+ # @return [Strig] the string for representing this response in an exception
53
+ #
54
+ def exception_message
55
+ self.data
56
+ end
57
+
51
58
  end
52
59
  end
@@ -1,11 +1,15 @@
1
1
  module MoonropeClient
2
2
  module Responses
3
3
  class AccessDenied < Response
4
-
4
+
5
5
  def message
6
6
  data['message']
7
7
  end
8
-
8
+
9
+ def exception_message
10
+ self.message
11
+ end
12
+
9
13
  end
10
14
  end
11
15
  end
@@ -1,11 +1,15 @@
1
1
  module MoonropeClient
2
2
  module Responses
3
3
  class ParameterError < Response
4
-
4
+
5
5
  def message
6
6
  data['message']
7
7
  end
8
-
8
+
9
+ def exception_message
10
+ self.message
11
+ end
12
+
9
13
  end
10
14
  end
11
15
  end
@@ -1,11 +1,15 @@
1
1
  module MoonropeClient
2
2
  module Responses
3
3
  class ValidationError < Response
4
-
4
+
5
5
  def errors
6
6
  data['errors']
7
7
  end
8
-
8
+
9
+ def exception_message
10
+ self.errors.join(', ')
11
+ end
12
+
9
13
  end
10
14
  end
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module MoonropeClient
2
- VERSION = '1.0.0'
3
- end
2
+ VERSION = '1.0.5'
3
+ end
metadata CHANGED
@@ -1,55 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonrope-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-09 00:00:00.000000000 Z
11
+ date: 2020-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.8'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 1.8.0
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '1.8'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 1.8.0
33
- - !ruby/object:Gem::Dependency
34
- name: yard
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '0.8'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 0.8.0
43
- type: :development
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '0.8'
50
24
  - - ">="
51
25
  - !ruby/object:Gem::Version
52
- version: 0.8.0
26
+ version: '0'
53
27
  description: A full client library allows requests to made to Moonrope-enabled API
54
28
  endpoints.
55
29
  email:
@@ -63,6 +37,7 @@ files:
63
37
  - lib/moonrope_client/controller.rb
64
38
  - lib/moonrope_client/error.rb
65
39
  - lib/moonrope_client/request.rb
40
+ - lib/moonrope_client/request_error.rb
66
41
  - lib/moonrope_client/response.rb
67
42
  - lib/moonrope_client/responses/access_denied.rb
68
43
  - lib/moonrope_client/responses/paginated_collection.rb
@@ -89,10 +64,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
64
  - !ruby/object:Gem::Version
90
65
  version: '0'
91
66
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.2.0
67
+ rubygems_version: 3.0.3
94
68
  signing_key:
95
69
  specification_version: 4
96
70
  summary: A client library for the the Moonrope API server.
97
71
  test_files: []
98
- has_rdoc: