scalingo-ruby-api 1.0.0.alpha1 → 1.0.0.alpha2

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: e414ecac6ae02a9b85d286fdfea4bb6c9a8f3c4d
4
- data.tar.gz: b01e544df9c7ea1b4bc39f62ee68699764305d7e
3
+ metadata.gz: 6e9e1714f5175b4ee396f6b2fcca6c5b0aed7dfd
4
+ data.tar.gz: c9b1bcd3fcc9f48c21b9bc82a96f2fdc06052af7
5
5
  SHA512:
6
- metadata.gz: c14839d474e165ebe4deaba60e98c2193aa654a2018df1b9955da73292042a8c17c8f92f8f8a9a0842093a5cf02616eff38180c7eb6cd2f6518abfb02b174aea
7
- data.tar.gz: 40ac42171d6091944d4d540647d132fa4bfc061e28d6932a4ea00f5512b064ffdfea75221e18732af0a4c756eee8f04f55dd223f9e65858b98142c34f5509897
6
+ metadata.gz: 8e367baa5b74001b704192efcecf7cbe5591de034494c672301baf2b82e8d9cf434af9132532c45e29478caea6a21cf4487ac24ea3c7e36eee3ce63612f8f401
7
+ data.tar.gz: 551dfa973509fc74c8a035628378fad2b18ced9815c070b904b0af0fa256cc18d41fddc1e4c6c4b104149a2fe59dbed478a6a3b8435ead0564cddb3b26756044
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Default: run unit tests'
6
+ task :default => :test
7
+
8
+ desc 'Run tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
@@ -10,7 +10,8 @@ module Scalingo
10
10
  :token,
11
11
  :endpoint,
12
12
  :user_agent,
13
- :proxy
13
+ :proxy,
14
+ :parse_json,
14
15
  ].freeze
15
16
 
16
17
  # The adapter that will be used to connect if none is set
@@ -32,6 +33,9 @@ module Scalingo
32
33
  # The user agent that will be sent to the Api endpoint if none is set
33
34
  DEFAULT_USER_AGENT = "Scalingo Ruby Gem #{Scalingo::VERSION}".freeze
34
35
 
36
+ # Parse json by default, only changed when getting text result (e.g. Logs)
37
+ DEFAULT_PARSE_JSON = true
38
+
35
39
  # @private
36
40
  attr_accessor *VALID_OPTIONS_KEYS
37
41
 
@@ -59,6 +63,7 @@ module Scalingo
59
63
  self.endpoint = DEFAULT_ENDPOINT
60
64
  self.user_agent = DEFAULT_USER_AGENT
61
65
  self.proxy = DEFAULT_PROXY
66
+ self.parse_json = DEFAULT_PARSE_JSON
62
67
  end
63
68
  end
64
69
  end
@@ -3,13 +3,10 @@ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
3
 
4
4
  module Scalingo
5
5
  module Connection
6
- protected
7
- def parse_json
8
- true
9
- end
10
-
11
6
  private
12
7
  def connection
8
+ raise MissingToken.new if !token
9
+
13
10
  options = {
14
11
  :headers => {
15
12
  'Accept' => 'application/json; charset=utf-8',
@@ -26,7 +23,7 @@ module Scalingo
26
23
  connection.use Faraday::Response::ParseJson if parse_json
27
24
  connection.use FaradayMiddleware::RaiseHttpException
28
25
  connection.adapter(adapter)
29
- connection.basic_auth('', token) if token
26
+ connection.basic_auth('', token)
30
27
  end
31
28
  end
32
29
  end
@@ -77,7 +77,7 @@ module Scalingo
77
77
  @resource_class ||= begin
78
78
  Scalingo::Endpoint.const_get(self.class.name.singularize.split('::').last)
79
79
  rescue
80
- OpenStruct
80
+ Resource
81
81
  end
82
82
  end
83
83
 
@@ -2,6 +2,9 @@ module Scalingo
2
2
  # Custom error class for rescuing from all Scalingo errors
3
3
  class Error < StandardError; end
4
4
 
5
+ # Raised when Scalingo try a connection without any token
6
+ class MissingToken < Error; end
7
+
5
8
  # Raised when Scalingo returns the HTTP status code 400
6
9
  class BadRequest < Error; end
7
10
 
@@ -6,6 +6,7 @@ module Scalingo
6
6
 
7
7
  def initialize(app)
8
8
  super({endpoint: ''})
9
+ self.parse_json = false
9
10
  @app = app
10
11
  end
11
12
 
@@ -23,10 +24,6 @@ module Scalingo
23
24
  @log_token = log_token.split('=').last
24
25
  end
25
26
 
26
- def parse_json
27
- false
28
- end
29
-
30
27
  def request(method, path, options)
31
28
  options.merge!(token: log_token)
32
29
  super(method, path, options)
@@ -1,4 +1,4 @@
1
1
  module Scalingo
2
- VERSION = '1.0.0.alpha1'
2
+ VERSION = '1.0.0.alpha2'
3
3
  end
4
4
 
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class ConnectionTest < BaseTestCase
4
+ test 'missing token' do
5
+ Scalingo.token = nil
6
+ assert_raise(Scalingo::MissingToken) do
7
+ Scalingo.apps.all
8
+ end
9
+ end
10
+
11
+ test 'parse_json' do
12
+ stub(:get, 'parse_json').to_return(body: {hello: :world}.to_json)
13
+ result = Scalingo.get('parse_json')
14
+ assert_equal({"hello" => "world"}, result)
15
+ end
16
+
17
+ test 'parse_json false' do
18
+ Scalingo.parse_json = false
19
+ stub(:get, 'parse_json').to_return(body: {hello: :world}.to_json)
20
+ result = Scalingo.get('parse_json')
21
+ assert_equal('{"hello":"world"}', result)
22
+ end
23
+ end
24
+
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class EndpointBaseTest < BaseTestCase
4
+ class ApiMock
5
+ attr_accessor :method_called
6
+
7
+ Scalingo::Request::REQUEST_METHODS.each do |method|
8
+ define_method(method) do |path, options = {}|
9
+ self.method_called = OpenStruct.new(http_method: method, path: path, options: options)
10
+ end
11
+ end
12
+ end
13
+ class EndpointBaseClass
14
+ include Scalingo::Endpoint::Base
15
+ end
16
+
17
+ setup do
18
+ @api = ApiMock.new
19
+ @prefix = 'hello'
20
+ @base = EndpointBaseClass.new(@api, @prefix)
21
+ end
22
+
23
+ Scalingo::Request::REQUEST_METHODS.each do |method|
24
+ test "#{method}" do
25
+ @base.send(method)
26
+ assert_equal method, @api.method_called.http_method
27
+ assert_equal "#{@prefix}/", @api.method_called.path
28
+ assert_equal({}, @api.method_called.options)
29
+
30
+ @base.send(method, 'test')
31
+ assert_equal method, @api.method_called.http_method
32
+ assert_equal "#{@prefix}/test", @api.method_called.path
33
+ assert_equal({}, @api.method_called.options)
34
+
35
+ @base.send(method, nil, {a: 1})
36
+ assert_equal method, @api.method_called.http_method
37
+ assert_equal "#{@prefix}/", @api.method_called.path
38
+ assert_equal({a: 1}, @api.method_called.options)
39
+
40
+ @base.send(method, 'test', {a: 1})
41
+ assert_equal method, @api.method_called.http_method
42
+ assert_equal "#{@prefix}/test", @api.method_called.path
43
+ assert_equal({a: 1}, @api.method_called.options)
44
+ end
45
+ end
46
+
47
+ test 'initialize without prefix' do
48
+ @base = EndpointBaseClass.new(@api)
49
+ assert_equal 'endpoint_base_classes', @base.prefix
50
+ end
51
+ end
52
+
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class Scalingo::Endpoint::TestEndpoint
4
+ end
5
+
6
+ class EndpointCollectionTest < BaseTestCase
7
+ class ApiMock
8
+ def get(path, options = {})
9
+ path = path[0..-2]
10
+ {
11
+ path => collection,
12
+ }
13
+ end
14
+
15
+ def collection
16
+ [
17
+ {
18
+ id: 1,
19
+ name: 'test1',
20
+ },
21
+ {
22
+ id: 2,
23
+ name: 'test2',
24
+ },
25
+ {
26
+ id: 4,
27
+ name: 'test4',
28
+ },
29
+ {
30
+ id: 5,
31
+ name: 'test5',
32
+ },
33
+ ]
34
+ end
35
+ end
36
+
37
+ class CollectionTests < Scalingo::Endpoint::Collection
38
+ end
39
+
40
+ setup do
41
+ @api = ApiMock.new
42
+ @collection = CollectionTests.new(@api)
43
+ end
44
+
45
+ test 'all' do
46
+ result = @collection.all
47
+ assert_equal Array, result.class
48
+ assert @collection.class < Enumerable
49
+ assert_equal @api.collection.count, @collection.count
50
+ end
51
+
52
+ test 'find' do
53
+ result = @collection.find(2)
54
+ assert_equal 2, result.id
55
+ assert_equal 'test2', result.name
56
+ end
57
+
58
+ test 'collection_name' do
59
+ assert_equal 'collection_tests', @collection.collection_name
60
+ end
61
+
62
+ test 'resource_class' do
63
+ assert_equal Scalingo::Endpoint::Resource, @collection.resource_class
64
+ end
65
+
66
+ class TestEndpoints < Scalingo::Endpoint::Collection
67
+ end
68
+
69
+ test 'resource_class if singular class exists' do
70
+ @collection = TestEndpoints.new(@api, @prefix)
71
+ assert_equal Scalingo::Endpoint::TestEndpoint, @collection.resource_class
72
+ end
73
+
74
+ test 'find_by' do
75
+ assert_equal 'id', @collection.find_by
76
+ end
77
+ end
78
+
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class EndpointResourceTest < BaseTestCase
4
+ test 'initialize' do
5
+ resource = Scalingo::Endpoint::Resource.new('api', 'prefix', {hello: :world, world: :hello})
6
+ assert_equal 'api', resource.api
7
+ assert_equal 'prefix', resource.prefix
8
+
9
+ assert resource.respond_to?(:hello)
10
+ assert_equal :world, resource.hello
11
+
12
+ assert resource.respond_to?(:world)
13
+ assert_equal :hello, resource.world
14
+ end
15
+ end
16
+
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class RequestTest < BaseTestCase
4
+ setup do
5
+ @request = nil
6
+ stub(:any, /.*/).to_return do |request|
7
+ @request = OpenStruct.new(
8
+ body: request.body,
9
+ http_method: request.method,
10
+ headers: request.headers,
11
+ uri: request.uri,
12
+ query: request.uri.query ? Hash[request.uri.query.split('&').map{|q| q.split('=')}] : {},
13
+ path: request.uri.path[4..-1],
14
+ )
15
+ {status: 200}
16
+ end
17
+ end
18
+
19
+ [:get, :delete].each do |sym|
20
+ test "#{sym}" do
21
+ Scalingo.public_send(sym, '')
22
+ assert_equal sym, @request.http_method
23
+ assert_equal({}, @request.query)
24
+ assert_equal '', @request.path
25
+ assert_equal nil, @request.body
26
+
27
+ Scalingo.public_send(sym, 'hello')
28
+ assert_equal sym, @request.http_method
29
+ assert_equal({}, @request.query)
30
+ assert_equal 'hello', @request.path
31
+ assert_equal nil, @request.body
32
+
33
+ Scalingo.public_send(sym, 'hello', {hello: :world})
34
+ assert_equal sym, @request.http_method
35
+ assert_equal({"hello" => "world"}, @request.query)
36
+ assert_equal 'hello', @request.path
37
+ assert_equal nil, @request.body
38
+ end
39
+ end
40
+
41
+ [:post, :patch, :put].each do |sym|
42
+ test "#{sym}" do
43
+ Scalingo.public_send(sym, '')
44
+ assert_equal sym, @request.http_method
45
+ assert_equal({}, @request.query)
46
+ assert_equal '', @request.path
47
+ assert_equal '', @request.body
48
+
49
+ Scalingo.public_send(sym, 'hello')
50
+ assert_equal sym, @request.http_method
51
+ assert_equal({}, @request.query)
52
+ assert_equal 'hello', @request.path
53
+ assert_equal '', @request.body
54
+
55
+ Scalingo.public_send(sym, 'hello', {hello: :world})
56
+ assert_equal sym, @request.http_method
57
+ assert_equal({}, @request.query)
58
+ assert_equal 'hello', @request.path
59
+ assert_equal({hello: :world}, @request.body)
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class ScalingoTest < BaseTestCase
4
+ test 'configure' do
5
+ Scalingo.configure do |config|
6
+ config.token = 'CONFIGURE_TEST'
7
+ end
8
+ assert_equal 'CONFIGURE_TEST', Scalingo.token
9
+ end
10
+
11
+ test 'client' do
12
+ assert_equal Scalingo::Client, Scalingo.client.class
13
+ end
14
+
15
+ test 'respond_to' do
16
+ assert !Scalingo.methods.include?(:get)
17
+ assert Scalingo.respond_to?(:get)
18
+ end
19
+
20
+ test 'method_missing' do
21
+ stub(:get, '').to_return(status: 200)
22
+ assert_equal Scalingo.client.get(''), Scalingo.get('')
23
+ end
24
+ end
25
+
@@ -0,0 +1,42 @@
1
+ require 'bundler/setup'
2
+ require 'simplecov'
3
+ require 'pry'
4
+
5
+ SimpleCov.configure do
6
+ add_filter '/test/'
7
+ end
8
+ SimpleCov.start if ENV['COVERAGE']
9
+
10
+ require 'minitest/autorun'
11
+ require 'webmock/minitest'
12
+ require 'active_support'
13
+ ActiveSupport.test_order = :random
14
+
15
+ require File.expand_path("../../lib/scalingo", __FILE__)
16
+
17
+ WebMock.disable_net_connect!
18
+
19
+ class BaseTestCase < ActiveSupport::TestCase
20
+ setup do
21
+ Scalingo.reset
22
+ Scalingo.token = 'EXAMPLE_TOKEN'
23
+ end
24
+
25
+ teardown do
26
+ WebMock.reset!
27
+ end
28
+
29
+ def endpoint_uri
30
+ Scalingo.endpoint.split('://').join("://:#{Scalingo.token}@")
31
+ end
32
+
33
+ def stub(method, path)
34
+ case path
35
+ when String
36
+ stub_request(method, "#{endpoint_uri}#{path}")
37
+ when Regexp
38
+ stub_request(method, /#{Regexp.quote(endpoint_uri)}#{path}/)
39
+ end
40
+ end
41
+ end
42
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scalingo-ruby-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha1
4
+ version: 1.0.0.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffroy Planquart
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -84,14 +84,14 @@ dependencies:
84
84
  requirements:
85
85
  - - ~>
86
86
  - !ruby/object:Gem::Version
87
- version: '3'
87
+ version: '4'
88
88
  type: :runtime
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - ~>
93
93
  - !ruby/object:Gem::Version
94
- version: '3'
94
+ version: '4'
95
95
  description: Ruby wrapper around the web API of scalingo.io
96
96
  email:
97
97
  - geoffroy@planquart.fr
@@ -99,6 +99,7 @@ executables: []
99
99
  extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
+ - Rakefile
102
103
  - lib/faraday/raise_http_exception.rb
103
104
  - lib/scalingo.rb
104
105
  - lib/scalingo/api.rb
@@ -121,6 +122,13 @@ files:
121
122
  - lib/scalingo/realtime/logs.rb
122
123
  - lib/scalingo/request.rb
123
124
  - lib/scalingo/version.rb
125
+ - test/connection_test.rb
126
+ - test/endpoint_base_test.rb
127
+ - test/endpoint_collection_test.rb
128
+ - test/endpoint_resource_test.rb
129
+ - test/request_test.rb
130
+ - test/scalingo_test.rb
131
+ - test/test_helper.rb
124
132
  homepage: https://github.com/Aethelflaed/scalingo-ruby-api
125
133
  licenses:
126
134
  - MIT
@@ -145,5 +153,12 @@ rubygems_version: 2.4.6
145
153
  signing_key:
146
154
  specification_version: 4
147
155
  summary: Ruby API for the awesome scalingo project !
148
- test_files: []
156
+ test_files:
157
+ - test/connection_test.rb
158
+ - test/request_test.rb
159
+ - test/endpoint_resource_test.rb
160
+ - test/endpoint_base_test.rb
161
+ - test/endpoint_collection_test.rb
162
+ - test/scalingo_test.rb
163
+ - test/test_helper.rb
149
164
  has_rdoc: