ServerPilot 0.0.3 → 0.0.4

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: 66cb9861282795741aadd7d9ce6d82804e7bf835
4
- data.tar.gz: d922e0c149d2ff21b1a47efd4e2c505fad5cab0a
3
+ metadata.gz: 1d44a36c34d4a41c172bf7a433091e810ed64fbf
4
+ data.tar.gz: 1f1f65da52d92ee8bcfeeefe71c243237b64c497
5
5
  SHA512:
6
- metadata.gz: 35ade37164b91ec0763b04db0c34269110ac6f2b886bf6f50ee617f9fc98eb96dd6bd95614efc7c9a82263aeb01f6df0a5ce48e39fe851dc855e3bc153d4d601
7
- data.tar.gz: e43399b08252874c5136e46544cc59ac56f494bd2023bb3dd66fc9af8722c260ec1cce2de3a8786a2fc5d8e5c7c14cdf52e777a59a23af57264a71c9df595153
6
+ metadata.gz: 124910be3eaa3cabd523078dbc35a5a83811ee5ac3362169875f61472b86b7b094beb7c8d06e63fc3d31ba427feb1653ef7b05a4a35e9133124c8d7c21771181
7
+ data.tar.gz: 6e72616b8065db191ddc615c7aa6a40c3fbd9e3c1c98e8f4781ff03beb89b2b8607c6ed4f802b8f74b4d0d6bdae2cca8a3e1999e2c948f19fb7f418a695f2191
data/.autotest ADDED
@@ -0,0 +1,2 @@
1
+ require 'autotest/fsevent'
2
+ require 'autotest/growl'
data/.gitignore CHANGED
@@ -12,3 +12,5 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .idea
16
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/README.md CHANGED
@@ -1,36 +1,56 @@
1
1
  # ServerPilot
2
2
 
3
- ServerPilot
4
- ===========
5
-
6
3
  Ruby client gem for accessing <a href="http://www.serverpilot.io" target="_blank">ServerPilot</a> API
4
+ Current version (0.0.4). This version has been tested against a sinatra app at
5
+ http://www.github.com/signalfire/serverpilot-sinatra to mock up calls to API methods.
7
6
 
8
- Install
9
- -------
7
+ ## Installation
10
8
 
11
- Install from RubyGems with **gem install serverpilot** or clone from this github
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ServerPilot'
13
+ ```
12
14
 
13
- Project on RubyGems at <a href="https://rubygems.org/gems/serverpilot" target="_blank">https://rubygems.org/gems/serverpilot</a>
15
+ And then execute:
14
16
 
15
- To create a connection **require 'serverpilot'**
17
+ $ bundle
16
18
 
17
- In your code add something like **sp = Signalfire::ServerPilot.new(client_id,key)**
19
+ Or install it yourself as:
20
+
21
+ $ gem install ServerPilot
22
+
23
+ ## Usage
24
+
25
+ To create a connection **require 'ServerPilot'**
26
+ In your code add something like **sp = ServerPilot::API.new(api_client, api_key)**
18
27
  where client_id and key are your API information from ServerPilot.
19
28
 
20
- Functions
21
- ---------
29
+ If the serverpilot api changes host, port, or scheme you can alter these using calls
30
+ to properties on the class
31
+
32
+ sp.scheme = 'http' or 'https'
33
+
34
+ sp.host = 'newserverpilotaddress.io'
35
+
36
+ sp.port = 1000
37
+
38
+ These should be called before you start doing anything with the class
22
39
 
40
+ ## Functions
23
41
  Commands are issued such as sp.[verb]_[action]
24
42
 
25
43
  In use it would look something like:
44
+
26
45
  sp.get_servers 'UXOSIYrdtL4cSGp3'
46
+
27
47
  or using hash params:
28
- sp.post_sysusers {'serverid'=>'UXOSIYrdtL4cSGp3','name'=>'abc123',=>'password'=>'1234'}
48
+
49
+ sp.post_sysusers {serverid: 'UXOSIYrdtL4cSGp3', name: 'abc123', password: '1234'}
29
50
 
30
51
  This is a list of the ruby functions to call on the server pilot object (e.g sp.get_servers)
31
52
  and matching ServerPilot API methods. [id] is the identifier of the server, app, database
32
- that you can retrieve via the API. [params] is a hash (Hash.new) of data to send to the
33
- api as JSON
53
+ that you can retrieve via the API. [params] is a hash of data to send to the api as JSON
34
54
 
35
55
  <table width="100%">
36
56
  <tr>
@@ -154,32 +174,16 @@ api as JSON
154
174
  <td>GET /actions/:id</td>
155
175
  </tr>
156
176
  </table>
157
- This is early code and my first ever Ruby Gem so things may possibly break or not be quite
158
- ruby enough....
159
-
160
- ## Installation
161
177
 
162
- Add this line to your application's Gemfile:
163
-
164
- ```ruby
165
- gem 'ServerPilot'
166
- ```
178
+ ## Development
167
179
 
168
- And then execute:
169
-
170
- $ bundle
171
-
172
- Or install it yourself as:
173
-
174
- $ gem install ServerPilot
175
-
176
- ## Usage
180
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
177
181
 
178
- TODO: Write usage instructions here
182
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
179
183
 
180
184
  ## Contributing
181
185
 
182
- 1. Fork it ( https://github.com/signalfire/ServerPilot/fork )
186
+ 1. Fork it ( https://github.com/[my-github-username]/ServerPilot/fork )
183
187
  2. Create your feature branch (`git checkout -b my-new-feature`)
184
188
  3. Commit your changes (`git commit -am 'Add some feature'`)
185
189
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
- require "bundler/gem_tasks"
2
-
1
+ require 'bundler/gem_tasks'
data/ServerPilot.gemspec CHANGED
@@ -8,18 +8,18 @@ Gem::Specification.new do |spec|
8
8
  spec.version = ServerPilot::VERSION
9
9
  spec.authors = ['Robert Coster']
10
10
  spec.email = ['rob@signalfire.co.uk']
11
- spec.summary ='ServerPilot'
12
- spec.description = 'An API client gem to allow interaction from Ruby with the ServerPilot.io API'
13
- spec.homepage = 'http://www.signalfire.co.uk'
14
- spec.license = 'MIT'
15
11
 
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
12
+ spec.summary = 'An API client for interaction with ServerPilot API'
13
+ spec.description = 'An API client for interaction with ServerPilot API'
14
+ spec.homepage = 'http://www.github.com/signalfire/serverpilot'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.required_ruby_version = '>= 2.2'
22
+ spec.add_runtime_dependency 'rest-client', '>= 1.8.0'
23
+ spec.add_development_dependency 'bundler', '~> 1.8'
22
24
  spec.add_development_dependency 'rake', '~> 10.0'
23
- spec.required_ruby_version = '~> 2.1'
24
-
25
25
  end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ServerPilot'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/ServerPilot.rb CHANGED
@@ -1,106 +1,130 @@
1
1
  require 'ServerPilot/version'
2
- require 'ServerPilot/connection'
2
+ require 'rest-client'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'ostruct'
3
6
 
4
7
  module ServerPilot
5
-
8
+ # Class to access the serverpilot api
6
9
  class API
10
+ attr_accessor :scheme
11
+ attr_accessor :host
12
+ attr_accessor :port
13
+ attr_accessor :client_id
14
+ attr_accessor :secret_key
15
+
16
+ def initialize(api_client, api_key)
17
+ @client_id = api_client
18
+ @secret_key = api_key
19
+ @scheme = 'https'
20
+ @host = 'api.serverpilot.io'
21
+ @port = 443
22
+ end
7
23
 
8
- def initialize(api_client_id, api_key)
9
-
10
- @sp_client_id = api_client_id
11
-
12
- @sp_key = api_key
24
+ def build_path(action, id = nil)
25
+ if action == 'ssl'
26
+ ['/v1', 'apps', id, 'ssl'].join('/') unless id.nil?
27
+ else
28
+ (id.nil?) ? ['/v1', action].join('/') : ['/v1', action, id].join('/')
29
+ end
30
+ end
13
31
 
32
+ def check_function_name_valid(name)
33
+ (name.to_s.include? '_') && (name.to_s.count('_') == 1) ? true : false
14
34
  end
15
35
 
16
- def build_path(action, id = nil)
36
+ def get_function_verb(name)
37
+ case name.to_s.downcase
38
+ when /^get/ then 'get'
39
+ when /^post/ then 'post'
40
+ when /^delete/ then 'delete'
41
+ end
42
+ end
17
43
 
18
- if action === 'ssl'
44
+ def get_params(arguments)
45
+ params = nil
46
+ arguments.each do |argument|
47
+ params = argument if argument.is_a?(Hash)
48
+ end
49
+ params
50
+ end
19
51
 
20
- ['/v1', 'apps', id, 'ssl'] * '/'
52
+ def get_action_name(name)
53
+ name.to_s.downcase.split('_').last
54
+ end
21
55
 
56
+ def get_path(arguments, action)
57
+ if arguments.length == 0
58
+ build_path(action)
22
59
  else
23
-
24
- if id
25
- ['/v1', action, id] * '/'
60
+ if arguments[0].is_a?(Integer) || arguments[0].is_a?(String)
61
+ build_path(action, arguments[0])
26
62
  else
27
- ['/v1', action] * '/'
63
+ build_path(action)
28
64
  end
29
-
30
65
  end
31
-
32
66
  end
33
67
 
34
- def method_missing(name, *arguments)
35
-
36
- #Check if name includes one and only one underscore
37
- if (name.to_s.include? '_') && (name.to_s.count('_') === 1)
38
-
39
- #Get the verb to call
40
- verb = case name.to_s.downcase
41
- when /^get/ then 'get'
42
- when /^post/ then 'post'
43
- when /^delete/ then 'delete'
44
- else nil
45
- end
46
-
47
- #Check if the verb used is not nil
48
- if verb
49
-
50
- #Get the action to execute
51
- action = name.to_s.downcase.split('_').last
52
-
53
- #Check the number of arguments passed is zero
54
- if arguments.length === 0
55
-
56
- #Get the path (e.g. /servers) to call without id
57
- path = build_path action
58
-
59
- else
60
-
61
- #Check if the first argument is an integer value
62
- if (arguments[0].is_a? Integer) || (arguments[0].is_a? String)
63
-
64
- #Get the path (e.g. /servers) to call passing the integer
65
- path = build_path action, arguments[0]
66
-
67
- #Check if the second argument is a hash and assign to params
68
- if arguments[1].is_a? Hash
69
- params = arguments[1]
70
- end
71
-
72
- else
73
-
74
- #Get the path (e.g. /servers) to call
75
- path = build_path action
76
-
77
- #If the first argument is a hash
78
- if arguments[0].is_a? Hash
79
- params = arguments[0]
80
- end
68
+ def create_uri(path)
69
+ URI::Generic.build(
70
+ scheme: @scheme,
71
+ host: @host,
72
+ path: path,
73
+ port: @port
74
+ ).to_s
75
+ end
81
76
 
82
- end
77
+ def create_rest_client(verb, path, params = nil)
78
+ if params
79
+ RestClient::Request.new(
80
+ method: verb,
81
+ url: create_uri(path),
82
+ user: @client_id,
83
+ password: @secret_key,
84
+ headers: { 'Content-Type' => 'application/json' },
85
+ payload: params.to_json
86
+ )
87
+ else
88
+ RestClient::Request.new(
89
+ method: verb,
90
+ url: create_uri(path),
91
+ user: @client_id,
92
+ password: @secret_key
93
+ )
94
+ end
95
+ end
83
96
 
84
- end
97
+ def execute_api_command(verb, path, params = nil)
98
+ client = create_rest_client verb, path, params
99
+ client.execute
100
+ end
85
101
 
86
- #Create a new connection
87
- connection = ServerPilot::Connection.new(@sp_client_id, @sp_key)
102
+ def parse_json(response)
103
+ body = JSON.parse(response.to_str) if response.code == 200
104
+ OpenStruct.new(code: response.code, body: body)
105
+ end
88
106
 
89
- #Execute the command via the connection and return the results
90
- connection.execute verb, path, params
107
+ def execute_request(verb, name, arguments)
108
+ parse_json(
109
+ execute_api_command(
110
+ verb,
111
+ get_path(arguments, get_action_name(name)),
112
+ get_params(arguments)
113
+ )
114
+ )
115
+ end
91
116
 
117
+ def method_missing(name, *arguments)
118
+ if check_function_name_valid name
119
+ verb = get_function_verb name
120
+ if verb
121
+ execute_request verb, name, arguments
92
122
  else
93
- #Raise an exception based on the verb used is not correct
94
- raise ArgumentError, 'Unsupported verb, functions should begin get_ post_ or delete_', caller
123
+ fail 'Invalid verb (get, post or delete supported)'
95
124
  end
96
-
97
125
  else
98
- #Raise an exception based on the name of the function being incorrect
99
- raise ArgumentError, 'Multiple underscores not allowed in function call', caller
126
+ fail 'Invalid function name'
100
127
  end
101
-
102
128
  end
103
-
104
129
  end
105
-
106
130
  end
@@ -1,3 +1,4 @@
1
+ # Gem version
1
2
  module ServerPilot
2
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
3
4
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ServerPilot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Coster
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2015-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '1.7'
33
+ version: '1.8'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '1.7'
40
+ version: '1.8'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,26 +52,28 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '10.0'
41
- description: An API client gem to allow interaction from Ruby with the ServerPilot.io
42
- API
55
+ description: An API client for interaction with ServerPilot API
43
56
  email:
44
57
  - rob@signalfire.co.uk
45
58
  executables: []
46
59
  extensions: []
47
60
  extra_rdoc_files: []
48
61
  files:
62
+ - ".autotest"
49
63
  - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
50
66
  - Gemfile
51
67
  - LICENSE.txt
52
68
  - README.md
53
69
  - Rakefile
54
70
  - ServerPilot.gemspec
71
+ - bin/console
72
+ - bin/setup
55
73
  - lib/ServerPilot.rb
56
- - lib/ServerPilot/connection.rb
57
74
  - lib/ServerPilot/version.rb
58
- homepage: http://www.signalfire.co.uk
59
- licenses:
60
- - MIT
75
+ homepage: http://www.github.com/signalfire/serverpilot
76
+ licenses: []
61
77
  metadata: {}
62
78
  post_install_message:
63
79
  rdoc_options: []
@@ -65,9 +81,9 @@ require_paths:
65
81
  - lib
66
82
  required_ruby_version: !ruby/object:Gem::Requirement
67
83
  requirements:
68
- - - "~>"
84
+ - - ">="
69
85
  - !ruby/object:Gem::Version
70
- version: '2.1'
86
+ version: '2.2'
71
87
  required_rubygems_version: !ruby/object:Gem::Requirement
72
88
  requirements:
73
89
  - - ">="
@@ -75,8 +91,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
91
  version: '0'
76
92
  requirements: []
77
93
  rubyforge_project:
78
- rubygems_version: 2.2.2
94
+ rubygems_version: 2.4.6
79
95
  signing_key:
80
96
  specification_version: 4
81
- summary: ServerPilot
97
+ summary: An API client for interaction with ServerPilot API
82
98
  test_files: []
@@ -1,69 +0,0 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'json'
4
- require 'ostruct'
5
-
6
- module ServerPilot
7
-
8
- class Connection
9
-
10
- ENDPOINT = {
11
- :scheme => 'https',
12
- :host => 'api.serverpilot.io',
13
- :port => 443
14
- }
15
-
16
- def initialize(api_client_id, api_key)
17
- @sp_client_id = api_client_id
18
- @sp_key = api_key
19
- end
20
-
21
- def create_uri(path)
22
-
23
- URI::Generic.build({:scheme=>ENDPOINT[:scheme], :host=>ENDPOINT[:host], :path=>path, :port=>ENDPOINT[:port]})
24
-
25
- end
26
-
27
- def request(verb, path, params = nil)
28
-
29
- uri = create_uri path
30
-
31
- Net::HTTP.start(uri.host,uri.port,:use_ssl => true) do |http|
32
-
33
- case verb.downcase
34
- when 'get'
35
- request = Net::HTTP::Get.new uri.path
36
- when 'post'
37
- request = Net::HTTP::Post.new uri.path, initheader = {'Content-Type' => 'application/json'}
38
- request.body = params.to_json
39
- when 'delete'
40
- request = Net::HTTP::Delete.new uri.path
41
- end
42
-
43
- request.basic_auth @sp_client_id, @sp_key
44
-
45
- http.request request
46
-
47
- end
48
-
49
- end
50
-
51
- def parse_json(response)
52
-
53
- body = JSON.parse(response.body)
54
-
55
- OpenStruct.new(:code => response.code, :body => body)
56
-
57
- end
58
-
59
- def execute(verb, path, params = nil)
60
-
61
- response = request(verb, path, params)
62
-
63
- parse_json response
64
-
65
- end
66
-
67
- end
68
-
69
- end