moonrope-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2dd9355b29944c25be8b737ed5e21048753719c4
4
+ data.tar.gz: f22c89a8a92620a2d4eee4b7fb93976c0513d00c
5
+ SHA512:
6
+ metadata.gz: b4007cd0fedfe8342830b7a7a0cb02dcc188c2955f3aa62e7cad9f93dc6d6b49474fb536d59f07c7a981b4b1d13207545547909a6182725a32d8fd3145b73572
7
+ data.tar.gz: 2796cd53024fc5766417ab1a74c2bad370feb2adbf71606a30b81e7e05fd12772535f28fd22004124dd2b91b0bc1bb35ddb378a899ab1e6cdfbf3541625cb4c6
@@ -0,0 +1,101 @@
1
+ module MoonropeClient
2
+ class Connection
3
+
4
+ #
5
+ # Initialize a new connection object to an API
6
+ #
7
+ # moonrope = MoonropeClient::Connection.new('myapp.com', :ssl => true, :path_prefix => 'api')
8
+ #
9
+ # @param host [String] the hostname to connect to
10
+ # @param options [Hash] A hash of options for this connectin
11
+ #
12
+ def initialize(host, options = {})
13
+ @host, @options = host, options
14
+ end
15
+
16
+ #
17
+ # @return [String] the endpoint hostname
18
+ #
19
+ attr_reader :host
20
+
21
+ #
22
+ # @return [String] the path prefix
23
+ #
24
+ def path_prefix
25
+ @options[:path_prefix] || 'api'
26
+ end
27
+
28
+ #
29
+ # @return [Boolean] whether or not SSL is enabled for requests or not
30
+ #
31
+ def ssl
32
+ @options[:ssl] || false
33
+ end
34
+
35
+ #
36
+ # @return [Integer] the port to conncet to
37
+ #
38
+ def port
39
+ @options[:port] || (ssl ? 443 : 80)
40
+ end
41
+
42
+ #
43
+ # @return [Hash] return headers to be set on all requests to the API
44
+ #
45
+ def headers
46
+ @options[:headers] || {}
47
+ end
48
+
49
+ #
50
+ # @return [Integer] the version of the API to use
51
+ #
52
+ def version
53
+ @options[:version] || 1
54
+ end
55
+
56
+ #
57
+ # Make a request and return an appropriate request object.
58
+ #
59
+ # @param controller [Symbol] the controller
60
+ # @param action [Symbol] the action
61
+ # @param params [Hash] parameters
62
+ #
63
+ def request(controller, action, params = {})
64
+ MoonropeClient::Request.new(self, controller, action, params).make
65
+ end
66
+
67
+ def controller(name)
68
+ MoonropeClient::Controller.new(self, name)
69
+ end
70
+
71
+ def method_missing(name, value = nil)
72
+ value.nil? ? self.controller(name) : super
73
+ end
74
+
75
+ #
76
+ # Make a request to the remote API server and return the raw output from
77
+ # the request.
78
+ #
79
+ # @param path [String] the full path to request
80
+ # @param params [Hash] a hash of parameters to send with the request
81
+ #
82
+ def raw_request(path, params = {})
83
+ request = Net::HTTP::Post.new(path)
84
+ request.set_form_data(params)
85
+ request.add_field 'User-Agent', "Moonrope Ruby Library/#{MoonropeClient::VERSION}"
86
+ headers.each { |k,v| request.add_field k, v }
87
+ connection = Net::HTTP.new(self.host, self.port)
88
+ if ssl
89
+ connection.use_ssl = true
90
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
91
+ end
92
+ result = connection.request(request)
93
+ case result.code.to_i
94
+ when 200 then result.body
95
+ when 400 then raise Error, "Bad request"
96
+ else raise Error, "Internal server error occurred"
97
+ end
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,20 @@
1
+ module MoonropeClient
2
+ class Controller
3
+
4
+ def initialize(connection, controller_name)
5
+ @connection = connection
6
+ @controller_name = controller_name
7
+ end
8
+
9
+ attr_reader :name
10
+
11
+ def method_missing(name, params = {})
12
+ request(name, params)
13
+ end
14
+
15
+ def request(action_name, params = {})
16
+ @connection.request(@controller_name, action_name, params)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ module MoonropeClient
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,53 @@
1
+ module MoonropeClient
2
+ class Request
3
+
4
+ #
5
+ # Initialize a new request
6
+ #
7
+ # @param connection [MoonropeClient::Connection]
8
+ # @param controller [Symbol]
9
+ # @param action [Symbol]
10
+ # @param params [Hash]
11
+ #
12
+ def initialize(connection, controller, action, params = {})
13
+ @connection = connection
14
+ @controller = controller.to_sym
15
+ @action = action.to_sym
16
+ @params = params
17
+ end
18
+
19
+ attr_reader :params
20
+
21
+ def make
22
+ raw_data_to_response_object(make_request)
23
+ end
24
+
25
+ private
26
+
27
+ def make_request
28
+ params = {:params => @params.to_json}
29
+ path = "/#{@connection.path_prefix}/v#{@connection.version}/#{@controller}/#{@action}"
30
+ JSON.parse(@connection.raw_request(path, params))
31
+ end
32
+
33
+ #
34
+ # Convert rhe result of a request into an appropriate response object
35
+ #
36
+ def raw_data_to_response_object(data)
37
+ case data['status']
38
+ when 'success'
39
+ if data['flags']['paginated'] && data['data'].is_a?(Array)
40
+ MoonropeClient::Responses::PaginatedCollection.new(self, data)
41
+ else
42
+ MoonropeClient::Responses::Success.new(self, data)
43
+ end
44
+ when 'parameter-error' then MoonropeClient::Responses::ParameterError.new(self, data)
45
+ when 'access-denied' then MoonropeClient::Responses::AccessDenied.new(self, data)
46
+ when 'validation-error' then MoonropeClient::Responses::ValidationError.new(self, data)
47
+ else
48
+ MoonropeClient::Response.new(self, data)
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,52 @@
1
+ module MoonropeClient
2
+ class Response
3
+
4
+ #
5
+ # Initialize a new response object
6
+ #
7
+ # @param request [MoonropeClient::Request]
8
+ # @param data [Hash]
9
+ #
10
+ def initialize(request, data)
11
+ @request = request
12
+ @data = data
13
+ end
14
+
15
+ #
16
+ # Is this a successful response?
17
+ #
18
+ # @return [Boolean]
19
+ #
20
+ def success?
21
+ false
22
+ end
23
+
24
+ #
25
+ # @return [String] the status of the request returned by the server
26
+ #
27
+ def status
28
+ @data['status']
29
+ end
30
+
31
+ #
32
+ # @return [Hash] any flags returned by the server
33
+ def flags
34
+ @data['flags']
35
+ end
36
+
37
+ #
38
+ # @return [Object] the data returned by the server
39
+ #
40
+ def data
41
+ @data['data']
42
+ end
43
+
44
+ #
45
+ # @return [Float] the time the request took at the server
46
+ #
47
+ def time
48
+ @data['time']
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ module MoonropeClient
2
+ module Responses
3
+ class AccessDenied < Response
4
+
5
+ def message
6
+ data['message']
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,71 @@
1
+ module MoonropeClient
2
+ module Responses
3
+ class PaginatedCollection < Success
4
+
5
+ #
6
+ # @return [Hash] all properties regarding pagination
7
+ #
8
+ def pagination_properties
9
+ flags['paginated']
10
+ end
11
+
12
+ #
13
+ # @return [Integer] the current page
14
+ #
15
+ def page
16
+ pagination_properties['page']
17
+ end
18
+
19
+ #
20
+ # @return [Integer] the total records per page
21
+ #
22
+ def per_page
23
+ pagination_properties['per_page']
24
+ end
25
+
26
+ #
27
+ # @return [Integer] the total number of pages
28
+ #
29
+ def total_pages
30
+ pagination_properties['total_pages']
31
+ end
32
+
33
+ #
34
+ # @return [Integer] the total number of records
35
+ #
36
+ def total_records
37
+ pagination_properties['total_records']
38
+ end
39
+
40
+ #
41
+ # @return [Array] all the items
42
+ #
43
+ def records
44
+ data
45
+ end
46
+
47
+ #
48
+ # @return [MoonropeClient::Responses::PaginatedCollection] the collection for the next page
49
+ #
50
+ def next_page
51
+ request = @request.dup
52
+ request.params[:page] = page + 1
53
+ request.make
54
+ end
55
+
56
+ #
57
+ # @return [MoonropeClient::Responses::PaginatedCollection] the collection for the previous page
58
+ #
59
+ def previous_page
60
+ if page > 1
61
+ request = @request.dup
62
+ request.params[:page] = page - 1
63
+ request.make
64
+ else
65
+ raise Error, "Cannot return the previous page as there is no page before page #{page}"
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ module MoonropeClient
2
+ module Responses
3
+ class ParameterError < Response
4
+
5
+ def message
6
+ data['message']
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module MoonropeClient
2
+ module Responses
3
+ class Success < Response
4
+
5
+ def success?
6
+ true
7
+ end
8
+
9
+ #
10
+ # @return [Boolean] whether this is a creation or not
11
+ #
12
+ def creation?
13
+ flags['creation']
14
+ end
15
+
16
+ #
17
+ # @return [Boolean] whether this is a modification or not
18
+ #
19
+ def modification?
20
+ flags['modification']
21
+ end
22
+
23
+ #
24
+ # @return [Boolean] whether this is a deletion or not
25
+ #
26
+ def deletion?
27
+ flags['deletion']
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module MoonropeClient
2
+ module Responses
3
+ class ValidationError < Response
4
+
5
+ def errors
6
+ data['errors']
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module MoonropeClient
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ require 'moonrope_client/version'
5
+ require 'moonrope_client/connection'
6
+ require 'moonrope_client/error'
7
+ require 'moonrope_client/controller'
8
+ require 'moonrope_client/request'
9
+ require 'moonrope_client/response'
10
+ require 'moonrope_client/responses/success'
11
+ require 'moonrope_client/responses/parameter_error'
12
+ require 'moonrope_client/responses/access_denied'
13
+ require 'moonrope_client/responses/validation_error'
14
+ require 'moonrope_client/responses/paginated_collection'
15
+
16
+ module MoonropeClient
17
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moonrope-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.8.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ 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
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.8.0
53
+ description: A full client library allows requests to made to Moonrope-enabled API
54
+ endpoints.
55
+ email:
56
+ - adam@atechmedia.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/moonrope_client.rb
62
+ - lib/moonrope_client/connection.rb
63
+ - lib/moonrope_client/controller.rb
64
+ - lib/moonrope_client/error.rb
65
+ - lib/moonrope_client/request.rb
66
+ - lib/moonrope_client/response.rb
67
+ - lib/moonrope_client/responses/access_denied.rb
68
+ - lib/moonrope_client/responses/paginated_collection.rb
69
+ - lib/moonrope_client/responses/parameter_error.rb
70
+ - lib/moonrope_client/responses/success.rb
71
+ - lib/moonrope_client/responses/validation_error.rb
72
+ - lib/moonrope_client/version.rb
73
+ homepage: http://adamcooke.io
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.0
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A client library for the the Moonrope API server.
97
+ test_files: []
98
+ has_rdoc: