lolp 0.2.1 → 0.3.0

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: 06211ad50dea46a165db75b6366faac072ea603e
4
- data.tar.gz: ea05b85aadd33fe4c5b70b1f99e1493032c7649d
3
+ metadata.gz: 48f64d7c464aa54df35fa07f1fbd58c1aa48a09d
4
+ data.tar.gz: 520ceff36e7affc10c78a5b3ca98d392b6e5557f
5
5
  SHA512:
6
- metadata.gz: 04d7c72e65adb59897cce3ec79bce9f4c65505a793d1ef3b2d916fb72a60367130e9df7c5ef31a0c45dfb068e5cbd81b3e542571efb3221dab045cdd1f530e43
7
- data.tar.gz: 2999e2153e4667b49ca7cb9e3a0e6e9d6c635e8aee28aa066163d526ba9421b47ca3b20942718459b0c639bec781825a1caf59759d599296be1e8cc9ef3463c3
6
+ metadata.gz: 8d743865d68e6a667b5e808d580c2bc73e788baaec23e1f9156c3792177bd55fffd7068a72e2198a2dccced2e450fb635fd6438aa846da7037116d1241fd5f76
7
+ data.tar.gz: e34ba0f0aa017fa03fe3caf33a461148ebd8b1d39dcac4b96500a3fd41dad0338d94c25bd37e076fe73df5f3cc5d11bb82d2a0ceb0811ae0409301e2618ea20b
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.3
4
+ - 2.5
5
5
  - 2.4
6
6
  - ruby-head
7
7
 
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in lolp.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ gem 'awesome_print'
9
+ end
data/Rakefile CHANGED
@@ -7,4 +7,8 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList["test/**/*_test.rb"]
8
8
  end
9
9
 
10
+ task :clean do
11
+ sh 'rm -rf test/fixtures/vcr_cassettes'
12
+ end
13
+
10
14
  task :default => :test
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'lolp'
5
+ require 'awesome_print'
6
+ require 'pry'
7
+ Pry.start
@@ -11,7 +11,6 @@ module Lolp
11
11
  if client.respond_to?(method_name)
12
12
  return client.send(method_name, *args, &block)
13
13
  end
14
-
15
14
  super
16
15
  end
17
16
  end
@@ -1,14 +1,21 @@
1
1
  require 'lolp/connection'
2
- require 'lolp/project'
3
2
  require 'lolp/configuration'
3
+ require 'lolp/client/project'
4
+ require 'lolp/client/authentication'
4
5
 
5
6
  module Lolp
6
7
  class Client
7
- include Lolp::Project
8
8
  include Lolp::Configuration
9
+ include Lolp::Connection
10
+ include Lolp::Client::Project
11
+ include Lolp::Client::Authentication
9
12
 
10
- def connection
11
- @connection ||= Lolp::Connection.new(config)
13
+ def initialize(config = {})
14
+ defaults
15
+
16
+ config.each do |k,v|
17
+ instance_variable_set(:"@#{key}", v)
18
+ end
12
19
  end
13
20
  end
14
21
  end
@@ -0,0 +1,12 @@
1
+ module Lolp
2
+ class Client
3
+ module Authentication
4
+ def authenticate(username = nil, password = nil)
5
+ @token = post('v1/authenticate',
6
+ username: username || @username,
7
+ password: password || @password)
8
+ end
9
+ alias login authenticate
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module Lolp
2
+ class Client
3
+ module Project
4
+ def projects
5
+ get('v1/projects')
6
+ end
7
+
8
+ def project(name)
9
+ get("v1/projects/#{name}")
10
+ end
11
+
12
+ def create_project(type, params = {})
13
+ post('v1/projects', params.merge(type: type))
14
+ end
15
+
16
+ def delete_project(name)
17
+ delete("v1/projects/#{name}")
18
+ end
19
+
20
+ def create_custom_domain(domain, custom_domain)
21
+ post("v1/projects/#{domain}/custom-domains", domain: custom_domain)
22
+ end
23
+
24
+ def delete_custom_domain(domain, custom_domain)
25
+ delete("v1/projects/#{domain}/custom-domains/#{custom_domain}")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,21 +1,16 @@
1
1
  module Lolp
2
2
  module Configuration
3
- def config
4
- @config ||= Config.new
5
- end
3
+ attr_accessor :api_endpoint, :username, :password, :token
6
4
 
7
5
  def configure
8
- yield config
6
+ yield self
9
7
  end
10
8
 
11
- class Config
12
- attr_accessor :api_endpoint, :username, :password
13
-
14
- def initialize
15
- @api_endpoint = ENV['LOLIPOP_MC_API_ENDPOINT'] || 'https://mc.lolipop.jp/api'
16
- @username = ENV['LOLIPOP_MC_USERNAME']
17
- @password = ENV['LOLIPOP_MC_PASSWORD']
18
- end
9
+ def defaults
10
+ @api_endpoint = ENV['LOLIPOP_MC_API_ENDPOINT'] || 'https://api.mc.lolipop.jp/'
11
+ @username = ENV['LOLIPOP_MC_USERNAME']
12
+ @password = ENV['LOLIPOP_MC_PASSWORD']
13
+ @token = ENV['LOLIPOP_MC_TOKEN']
19
14
  end
20
15
  end
21
16
  end
@@ -1,38 +1,73 @@
1
1
  require 'faraday'
2
+ require 'lolp/errors'
2
3
  require 'faraday_middleware'
3
4
 
4
5
  module Lolp
5
- class Connection
6
- def initialize(config)
7
- @config = config
8
- authenticate
9
- @connection = connection
10
- end
11
-
12
- def get(path)
13
- @connection.get(path)
6
+ module Connection
7
+ def get(path, params = {})
8
+ request(:get, path, params)
14
9
  end
15
10
 
16
11
  def post(path, params = {})
17
- @connection.post(path, params)
12
+ request(:post, path, params)
18
13
  end
19
14
 
20
15
  def delete(path, params = {})
21
- @connection.delete(path, params)
16
+ request(:delete, path, params)
22
17
  end
23
18
 
24
- private
19
+ def authenticated?
20
+ !!@token
21
+ end
25
22
 
26
- def connection
27
- Faraday.new(url: @config.api_endpoint, headers: { Authorization: "Bearer #{@token}" }) do |faraday|
28
- faraday.request :json
29
- faraday.response :json, content_type: /\bjson$/
30
- faraday.adapter Faraday.default_adapter
23
+ def last_response
24
+ @last_response if defined? @last_response
25
+ end
26
+
27
+ def auto_loginable?(url)
28
+ !authenticated? && url !~ /\/authenticate/ && @username && @password
29
+ end
30
+
31
+ def request(method, url = nil, data = nil, headers = nil, &block)
32
+ login if auto_loginable?(url)
33
+
34
+ @last_response = if %i(post put patch).include?(method)
35
+ connection.run_request(method, url, data, headers, &block)
36
+ else
37
+ connection.run_request(method, url, nil, headers) { |r|
38
+ r.params.update(data) if data
39
+ yield(r) if block_given?
40
+ }
41
+ end
42
+
43
+ if error = Error.from_response(@last_response)
44
+ raise error
31
45
  end
46
+ @last_response.body
32
47
  end
33
48
 
34
- def authenticate
35
- @token ||= connection.post('login', username: @config.username, password: @config.password).body
49
+ private
50
+
51
+ def connection
52
+ project_url = 'https://github.com/pepabo/lolp.rb'
53
+ user_agent = "lolp/#{VERSION} (+#{project_url}; ruby#{RUBY_VERSION})"
54
+ ssl_verify = true
55
+
56
+ args = {
57
+ url: @api_endpoint,
58
+ ssl: { verify: ssl_verify },
59
+ headers: {
60
+ user_agent: user_agent,
61
+ content_type: 'application/json'
62
+ }
63
+ }
64
+
65
+ @connection.authorization(:Bearer, @token) if @connection && authenticated?
66
+ @connection ||= Faraday.new(args) do |f|
67
+ f.request :json
68
+ f.response :json, content_type: /\bjson$/
69
+ f.adapter Faraday.default_adapter
70
+ end
36
71
  end
37
72
  end
38
73
  end
@@ -0,0 +1,54 @@
1
+ module Lolp
2
+ class Error < StandardError
3
+ class << self
4
+ def from_response(r)
5
+ if klass = case r.status.to_i
6
+ when 400 then BadRequest
7
+ when 401 then Unauthorized
8
+ when 402 then PaymentRequired
9
+ when 403 then Forbidden
10
+ when 404 then NotFound
11
+ when 406 then NotAcceptable
12
+ when 409 then Conflict
13
+ when 429 then TooManyRequests
14
+ when 422 then UnprocessableEntity
15
+ when 400..499 then ClientError
16
+ when 500 then InternalServerError
17
+ when 501 then NotImplemented
18
+ when 502 then BadGateway
19
+ when 503 then ServiceUnavailable
20
+ when 500..599 then ServerError
21
+ end
22
+ klass.new(build_error_message(r))
23
+ end
24
+ end
25
+
26
+ def build_error_message(res = nil)
27
+ return nil if res.nil?
28
+ message = "#{res.env.method.to_s.upcase} "
29
+ message << "#{res.env.url}: "
30
+ message << "#{res.status} - "
31
+ message << "#{res.body}" if res.body
32
+ message
33
+ end
34
+ end
35
+ end
36
+
37
+ class BadRequest < Error; end
38
+ class Unauthorized < Error; end
39
+ class PaymentRequired < Error; end
40
+ class Forbidden < Error; end
41
+ class NotFound < Error; end
42
+ class NotAcceptable < Error; end
43
+ class Conflict < Error; end
44
+ class TooManyRequests; end
45
+ class UnprocessableEntity < Error; end
46
+ class ClientError < Error; end
47
+
48
+ class InternalServerError < Error; end
49
+ class NotImplemented < Error; end
50
+ class BadGateway < Error; end
51
+ class ServiceUnavailable < Error; end
52
+ class ServerError < Error; end
53
+ end
54
+
@@ -1,3 +1,3 @@
1
1
  module Lolp
2
- VERSION = "0.2.1"
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lolp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - linyows
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-13 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,11 +135,14 @@ files:
135
135
  - Gemfile
136
136
  - README.md
137
137
  - Rakefile
138
+ - bin/console
138
139
  - lib/lolp.rb
139
140
  - lib/lolp/client.rb
141
+ - lib/lolp/client/authentication.rb
142
+ - lib/lolp/client/project.rb
140
143
  - lib/lolp/configuration.rb
141
144
  - lib/lolp/connection.rb
142
- - lib/lolp/project.rb
145
+ - lib/lolp/errors.rb
143
146
  - lib/lolp/version.rb
144
147
  - lolp.gemspec
145
148
  homepage: https://lolipop.jp
@@ -161,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
164
  version: '0'
162
165
  requirements: []
163
166
  rubyforge_project:
164
- rubygems_version: 2.6.13
167
+ rubygems_version: 2.6.14
165
168
  signing_key:
166
169
  specification_version: 4
167
170
  summary: The lolipop! client
@@ -1,23 +0,0 @@
1
- module Lolp
2
- module Project
3
- def projects
4
- connection.get('projects')
5
- end
6
-
7
- def create_project(type, params = {})
8
- connection.post('projects', params.merge(type: type))
9
- end
10
-
11
- def delete_project(name)
12
- connection.delete("projects/#{name}")
13
- end
14
-
15
- def create_custom_domain(domain, custom_domain)
16
- connection.post("projects/#{domain}/custom-domains", domain: custom_domain)
17
- end
18
-
19
- def delete_custom_domain(domain, custom_domain)
20
- connection.delete("projects/#{domain}/custom-domains/#{custom_domain}")
21
- end
22
- end
23
- end