joumae 0.1.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: d5ab0c54854665d91ae1ba7c7638f325fc630dbe
4
+ data.tar.gz: 9721b41223c894b49004bd1f84ece6ad982ebbe3
5
+ SHA512:
6
+ metadata.gz: f3a9d53966637570389ec1812f4965e7c34cd4515dbd3eb1dda93109e0f53acdea497e41a07ed98fd7b5c7a79db83e3d6cd8de062889c6a08328a49eb7c5c3a5
7
+ data.tar.gz: 7a092e0fc7531f07d55d3ba81f8498ed3bd1d55735cade8fced13dd35f6e5e2ab41ec89c3778a91f4e7878c1f919248ca1b7725f9917f5a33048ae12bab78956
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *~
2
+ .env
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in joumae.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ joumae (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.4.2)
11
+ rspec (3.3.0)
12
+ rspec-core (~> 3.3.0)
13
+ rspec-expectations (~> 3.3.0)
14
+ rspec-mocks (~> 3.3.0)
15
+ rspec-core (3.3.2)
16
+ rspec-support (~> 3.3.0)
17
+ rspec-expectations (3.3.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.3.0)
20
+ rspec-mocks (3.3.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.3.0)
23
+ rspec-support (3.3.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.10)
30
+ joumae!
31
+ rake (~> 10.0)
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 1.10.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yusuke KUOKA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Joumae
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/joumae`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'joumae'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install joumae
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/joumae. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "joumae"
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
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'securerandom'
4
+ require 'joumae'
5
+
6
+ client = Joumae::Client.create
7
+
8
+ uuid = SecureRandom.uuid
9
+ p client.create(uuid)
10
+ p client.acquire(uuid, 'test')
11
+ p client.renew(uuid, 'tet')
12
+ p client.release(uuid, 'test')
13
+
14
+ transaction = Joumae::Transaction.new(resource_name: uuid, owner: 'test', client: client)
15
+
16
+ transaction.start
17
+
18
+ sleep 10
19
+
20
+ transaction.finish
data/joumae.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'joumae/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "joumae"
8
+ spec.version = Joumae::VERSION
9
+ spec.authors = ["Yusuke KUOKA"]
10
+ spec.email = ["yusuke.kuoka@crowdworks.co.jp"]
11
+
12
+ spec.summary = %q{A Ruby client library for the Joumae lock service.}
13
+ spec.description = %q{A Ruby client library for the Joumae lock service.}
14
+ spec.homepage = "https://github.com/crowdworks/joumae-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,85 @@
1
+ require 'net/https'
2
+ require 'logger'
3
+ require 'json'
4
+
5
+ module Joumae
6
+ class Client
7
+ attr_reader :api_endpoint
8
+ attr_reader :api_key
9
+
10
+ def self.create(api_endpoint: nil, api_key: nil)
11
+ new(api_endpoint: api_endpoint || ENV['JOUMAE_API_ENDPOINT'], api_key: api_key || ENV['JOUMAE_API_KEY'])
12
+ end
13
+
14
+ def initialize(api_endpoint:, api_key:)
15
+ @api_endpoint = api_endpoint
16
+ @api_key = api_key
17
+ end
18
+
19
+ def create(name)
20
+ post_json(resources_url, {api_key: api_key, name: name})
21
+ end
22
+
23
+ def acquire(name, acquired_by)
24
+ post_json(acquire_resource_url, {api_key: api_key, name: name, acquiredBy: acquired_by})
25
+ end
26
+
27
+ def renew(name, acquired_by)
28
+ post_json(renew_resource_url, {api_key: api_key, name: name, acquired_by: acquired_by})
29
+ end
30
+
31
+ def release(name, acquired_by)
32
+ post_json(release_resource_url, {api_key: api_key, name: name, acquired_by: acquired_by})
33
+ end
34
+
35
+ def post_json(url, params={})
36
+ begin
37
+ logger.info "POST #{url} #{params.to_json}"
38
+ request = Net::HTTP::Post.new(url.path, {'Content-Type' =>'application/json'})
39
+ request.body = params.to_json
40
+ https = Net::HTTP.new(url.hostname, 443)
41
+ https.use_ssl = true
42
+ response = https.start do
43
+ https.request(request)
44
+ end
45
+ # response = httpclient.post_content(url, params.to_json, header: {'Content-Type' => 'application/json'})
46
+
47
+ if response.code == '404'
48
+ fail "Not found."
49
+ elsif response.code != '200'
50
+ fail "Unexpected status: #{response.code}"
51
+ end
52
+
53
+ response_body = JSON.parse(response.body)
54
+ response_body
55
+ rescue => e
56
+ logger.debug "Dumping HTTP response:\n#{response.inspect} #{response.body}"
57
+ fail e
58
+ end
59
+ end
60
+
61
+ def resources_url
62
+ URI.parse("#{api_endpoint}/resources")
63
+ end
64
+
65
+ def acquire_resource_url
66
+ URI.parse("#{api_endpoint}/resources/acquire")
67
+ end
68
+
69
+ def renew_resource_url
70
+ URI.parse("#{api_endpoint}/resources/renew")
71
+ end
72
+
73
+ def release_resource_url
74
+ URI.parse("#{api_endpoint}/resources/release")
75
+ end
76
+
77
+ def httpclient
78
+ @httpclient ||= HTTPClient.new
79
+ end
80
+
81
+ def logger
82
+ @logger ||= Logger.new(STDOUT)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,44 @@
1
+ module Joumae
2
+ class Transaction
3
+ def initialize(resource_name:, owner:, client:, renew_interval: 1)
4
+ @resource_name = resource_name
5
+ @owner = owner
6
+ @client = client
7
+ @renew_interval = renew_interval
8
+ @finished = false
9
+ end
10
+
11
+ def start
12
+ @client.acquire(@resource_name, @owner)
13
+
14
+ start_thread
15
+ end
16
+
17
+ def start_thread
18
+ fail "Thread already started" if @thread
19
+
20
+ @thread = Thread.start {
21
+ loop do
22
+ sleep @renew_interval
23
+ exit if finished?
24
+ @client.renew(@resource_name, @owner)
25
+ end
26
+ }
27
+ end
28
+
29
+ def stop_thread
30
+ @finished = true
31
+
32
+ @thread.join
33
+ end
34
+
35
+ def finish
36
+ stop_thread
37
+ @client.release(@resource_name, @owner)
38
+ end
39
+
40
+ def finished?
41
+ @finished
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Joumae
2
+ VERSION = "0.1.0"
3
+ end
data/lib/joumae.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "joumae/version"
2
+ require "joumae/client"
3
+ require "joumae/transaction"
4
+
5
+ module Joumae
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joumae
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke KUOKA
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Ruby client library for the Joumae lock service.
56
+ email:
57
+ - yusuke.kuoka@crowdworks.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - integration_test.rb
72
+ - joumae.gemspec
73
+ - lib/joumae.rb
74
+ - lib/joumae/client.rb
75
+ - lib/joumae/transaction.rb
76
+ - lib/joumae/version.rb
77
+ homepage: https://github.com/crowdworks/joumae-ruby
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A Ruby client library for the Joumae lock service.
101
+ test_files: []
102
+ has_rdoc: