libmagellan 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34304b7e7ce13311b97b509bd020f47705a919e3
4
+ data.tar.gz: c3673f6d4c027ddca67e7778e42dc6ec67379cfb
5
+ SHA512:
6
+ metadata.gz: e6dbb4e6ae4c066e0c9daccf968b328c86b5c93f283e7b04f945e8ad9b2e754a0e9780da53e29233597aded96732aec01824dbb6cc0f3325b9d89205a61f6b7b
7
+ data.tar.gz: 17ff66c7e2ab7cbf5e3dc9a75d29990d7f3fef15834ee0eb236b0480f9ad6dfb1f1140f63b8f6d6aa5cbae2dd292a66099abf34fc575c56a40a601dfab826408
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
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.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in libmagellan.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 akima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Libmagellan
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'libmagellan'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install libmagellan
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/libmagellan/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # rubygems.orgにreleaseしてはいけないので、誤って実行できないようにします。
4
+ ENV['gem_push'] = "no"
5
+
6
+ require "bundler/gem_tasks"
7
+ require "rspec/core/rake_task"
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
data/bin/libmagellan ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'libmagellan'
@@ -0,0 +1,136 @@
1
+ require 'libmagellan'
2
+
3
+ require 'uri'
4
+ require "net/http"
5
+ require "signet/oauth_1/client"
6
+ require 'active_support/core_ext/object/blank'
7
+
8
+ module Libmagellan
9
+ class Base
10
+
11
+ DEFAULT_OPTIONS = {
12
+ consumer_key: "groovenauts-test",
13
+ consumer_secret: "test",
14
+ client_version: "0.0.1",
15
+ }.freeze
16
+
17
+ # LibMagellan.new("localhost")
18
+ # LibMagellan.new("http://localhost:3000")
19
+ # LibMagellan.new(host: "localhost", port: 3000)
20
+ # LibMagellan.new("localhost", consumer_key: "foo", consumer_secret: "bar")
21
+ # LibMagellan.new("http://localhost:3000", consumer_key: "foo", consumer_secret: "bar")
22
+ # LibMagellan.new(host: "localhost", port: 3000, consumer_key: "foo", consumer_secret: "bar")
23
+ def initialize(*args)
24
+ options = DEFAULT_OPTIONS.dup.update((args.last.is_a?(Hash) ? args.pop : {}))
25
+ @consumer_key = options.delete :consumer_key
26
+ @consumer_secret = options.delete :consumer_secret
27
+ @client_version = options.delete :client_version
28
+ arg = args.first
29
+ @base_uri =
30
+ case arg
31
+ when nil then URI::HTTP.build(options)
32
+ when Array then URI::HTTP.build(arg)
33
+ when Hash then URI::HTTP.build({scheme: "http", host: "localhost"}.update(arg))
34
+ when String then (URI.regexp =~ arg) ? URI.parse(arg) : URI::HTTP.build({scheme: "http", host: arg}.update(options))
35
+ end
36
+ end
37
+
38
+ def request_without_oauth(path, method=:get, body="", headers={})
39
+ uri = URI.join(@base_uri.to_s, path)
40
+ http_client = Net::HTTP.new(uri.host, uri.port)
41
+ http_client.use_ssl = true if uri.scheme == 'https'
42
+ response = http_client.start do |http|
43
+ path_query = "#{uri.path}"
44
+ path_query << "?#{uri.query}" unless uri.query.blank?
45
+
46
+ response = case method.downcase.to_sym
47
+ when :delete then http.delete(path_query, headers)
48
+ when :put then http.put(path_query, body, headers)
49
+ when :post then http.post(path_query, body, headers)
50
+ else http.get(path_query, headers)
51
+ end
52
+ yield(response) if block_given?
53
+ end
54
+ return response
55
+ end
56
+
57
+ def request_with_oauth(path, method=:get, body="", headers={})
58
+ uri = URI.join(@base_uri.to_s, path)
59
+ options = {method: method}
60
+ auth_headers = generate_oauth1_2legged_request(uri, options)
61
+ auth_headers["Client-Version"] = @client_version
62
+ headers = headers.update(auth_headers)
63
+ request_without_oauth(path, method, body, headers) do |r|
64
+ yield(r) if block_given?
65
+ r
66
+ end
67
+ end
68
+ alias_method :request, :request_with_oauth
69
+
70
+ def generate_oauth1_2legged_request(uri, options={})
71
+ header = generate_oauth_request(uri, options) do |c, o|
72
+ c.two_legged = true
73
+ [c, o]
74
+ end
75
+ return header
76
+ end
77
+
78
+ def generate_oauth_request(uri, options={})
79
+ init_opt = {client_credential_key: @consumer_key, client_credential_secret: @consumer_secret}
80
+ options[:uri] = uri if options[:uri].blank?
81
+ client = ::Signet::OAuth1::Client.new(init_opt)
82
+ client, options = yield(client, options) if block_given?
83
+ req = client.generate_authenticated_request(options)
84
+ header = { "Authorization" => req["Authorization"], "Cache-Control" => "no-store" }
85
+ return header
86
+ end
87
+
88
+ def json_body(r)
89
+ body = r.body
90
+ begin
91
+ JSON.parse(body)
92
+ rescue ::JSON::ParserError
93
+ body
94
+ end
95
+ end
96
+
97
+ def ping
98
+ request("/ping")
99
+ end
100
+
101
+ def test_ping(options = {})
102
+ test("/ping", options){|r| r.code == '200' or r.body.strip == 'pong'}
103
+ end
104
+
105
+ def test(path, options = {})
106
+ options = {
107
+ method: :get,
108
+ body: "",
109
+ headers: {}
110
+ }.update(options || {})
111
+ method = options.delete(:method)
112
+ body = options.delete(:body)
113
+ headers = options.delete(:headers)
114
+ max_retry_count = options[:max_retry_count] || 5
115
+ retry_interval = options[:retry_interval] || 3
116
+ c = 0
117
+ begin
118
+ r = request(path, method, body, headers)
119
+ unless yield(r)
120
+ raise InvalidResponse, "invalid response code: #{r.code.inspect} body: #{r.body.strip.inspect}"
121
+ end
122
+ return self
123
+ rescue Errno::ECONNREFUSED => e
124
+ c += 1
125
+ if c > max_retry_count
126
+ raise e
127
+ else
128
+ # puts "retrying GET /ping... #{c} times"
129
+ sleep(retry_interval)
130
+ retry
131
+ end
132
+ end
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,3 @@
1
+ module Libmagellan
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require "libmagellan/version"
2
+
3
+ module Libmagellan
4
+ autoload :Base, "libmagellan/base"
5
+
6
+ class << self
7
+ def new(*args, &block)
8
+ Base.new(*args, &block)
9
+ end
10
+ end
11
+
12
+ class InvalidResponse < StandardError
13
+ end
14
+
15
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'libmagellan/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "libmagellan"
8
+ spec.version = Libmagellan::VERSION
9
+ spec.authors = ["akima"]
10
+ spec.email = ["t-akima@groovenauts.jp"]
11
+ spec.summary = %q{ruby client for magellanic cloud}
12
+ spec.description = %q{ruby client for magellanic cloud}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
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)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "signet", "~> 0.5.0"
22
+ spec.add_runtime_dependency "activesupport"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,72 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Libmagellan::Base do
5
+ let(:lm){ Libmagellan.new("http://localhost:3000", consumer_key: "foo", consumer_secret: "bar") }
6
+
7
+ describe :request do
8
+
9
+ let(:http){ double(:http) }
10
+ let(:res){ double(:res) }
11
+
12
+ it :success do
13
+ allow(Net::HTTP).to receive(:start).with("localhost", 3000).and_yield(http)
14
+ allow(http).to receive(:get).\
15
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
16
+ and_return(res)
17
+ r = lm.request("/ping")
18
+ expect(r).to eq res
19
+ end
20
+ end
21
+
22
+ describe :ping do
23
+
24
+ let(:http){ double(:http) }
25
+ let(:res){ double(:res) }
26
+
27
+ it :success do
28
+ allow(Net::HTTP).to receive(:start).with("localhost", 3000).and_yield(http)
29
+ allow(res).to receive(:code).and_return("200")
30
+ allow(res).to receive(:body).and_return("pong\n")
31
+ allow(http).to receive(:get).\
32
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
33
+ and_return(res)
34
+ r = lm.test_ping
35
+ expect(r).to eq lm
36
+ end
37
+
38
+ it :connection_failure_1_time do
39
+ allow(Net::HTTP).to receive(:start).with("localhost", 3000).and_yield(http)
40
+ allow(http).to receive(:get).\
41
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
42
+ once.\
43
+ and_raise(Errno::ECONNREFUSED)
44
+ allow(res).to receive(:code).and_return("200")
45
+ allow(res).to receive(:body).and_return("pong\n")
46
+ allow(http).to receive(:get).\
47
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
48
+ and_return(res)
49
+ r = lm.test_ping
50
+ expect(r).to eq lm
51
+ end
52
+
53
+ it :connection_failure do
54
+ allow(Net::HTTP).to receive(:start).with("localhost", 3000).and_yield(http)
55
+ allow(http).to receive(:get).\
56
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
57
+ exactly(3).times.\
58
+ and_raise(Errno::ECONNREFUSED)
59
+ expect{ lm.test_ping(max_retry_count: 2, retry_interval: 0) }.to raise_error(Errno::ECONNREFUSED)
60
+ end
61
+
62
+ it :error_response do
63
+ allow(Net::HTTP).to receive(:start).with("localhost", 3000).and_yield(http)
64
+ allow(res).to receive(:code).and_return("500")
65
+ allow(res).to receive(:body).and_return("Something wrong!")
66
+ allow(http).to receive(:get).\
67
+ with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
68
+ and_return(res)
69
+ expect{ lm.test_ping }.to raise_error(Libmagellan::InvalidResponse)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Libmagellan do
4
+ it 'has a version number' do
5
+ expect(Libmagellan::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'libmagellan'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libmagellan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - akima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: signet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ruby client for magellanic cloud
84
+ email:
85
+ - t-akima@groovenauts.jp
86
+ executables:
87
+ - libmagellan
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/libmagellan
99
+ - lib/libmagellan.rb
100
+ - lib/libmagellan/base.rb
101
+ - lib/libmagellan/version.rb
102
+ - libmagellan.gemspec
103
+ - spec/libmagellan/base_spec.rb
104
+ - spec/libmagellan_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: ruby client for magellanic cloud
130
+ test_files:
131
+ - spec/libmagellan/base_spec.rb
132
+ - spec/libmagellan_spec.rb
133
+ - spec/spec_helper.rb
134
+ has_rdoc: