optimis_client 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +31 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/optimis_client/base.rb +88 -0
- data/lib/optimis_client/version.rb +3 -0
- data/lib/optimis_client.rb +3 -0
- data/optimis_client.gemspec +23 -0
- data/spec/lib/base_spec.rb +25 -0
- data/spec/spec_helper.rb +11 -0
- metadata +90 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
optimis_client (0.2.0)
|
5
|
+
typhoeus (~> 0.2.3)
|
6
|
+
yajl-ruby
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.1.2)
|
12
|
+
mime-types (1.16)
|
13
|
+
rspec (2.5.0)
|
14
|
+
rspec-core (~> 2.5.0)
|
15
|
+
rspec-expectations (~> 2.5.0)
|
16
|
+
rspec-mocks (~> 2.5.0)
|
17
|
+
rspec-core (2.5.1)
|
18
|
+
rspec-expectations (2.5.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.5.0)
|
21
|
+
typhoeus (0.2.4)
|
22
|
+
mime-types
|
23
|
+
mime-types
|
24
|
+
yajl-ruby (0.8.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
optimis_client!
|
31
|
+
rspec (~> 2.5)
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Optimis Client
|
2
|
+
|
3
|
+
a Ruby client library for Optimis Service
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
* typhoeus
|
8
|
+
* yajl-ruby
|
9
|
+
|
10
|
+
### typhoeus requires libcurl >= 7.19 with OpenSSL support
|
11
|
+
|
12
|
+
wget http://curl.haxx.se/download/curl-7.21.6.tar.gz
|
13
|
+
tar zxvf curl-7.21.6.tar.gz
|
14
|
+
cd curl-7.21.6
|
15
|
+
./configure --with-ssl
|
16
|
+
make
|
17
|
+
make install
|
18
|
+
gem install typhoeus
|
19
|
+
|
20
|
+
## Run Unit Test
|
21
|
+
|
22
|
+
bundle exec rspec spec/lib
|
23
|
+
|
24
|
+
## Design Note
|
25
|
+
|
26
|
+
As a client library, it should support:
|
27
|
+
|
28
|
+
* Wrap typhoeus operation
|
29
|
+
* Secure connection
|
30
|
+
* Handle timeout
|
31
|
+
* Provide 'stubbed' option, it will stub return values if set true
|
32
|
+
|
33
|
+
It will not:
|
34
|
+
|
35
|
+
* Rescue exception, it will raise to application layer.
|
data/Rakefile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module OptimisClient
|
2
|
+
|
3
|
+
class ResponseError < StandardError
|
4
|
+
attr_accessor :status, :message, :errors
|
5
|
+
|
6
|
+
def initialize(http_status, message=nil, errors = {})
|
7
|
+
self.status, self.message, self.errors = http_status, message, errors
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Base
|
12
|
+
|
13
|
+
DEFAULT_TIMEOUT = 10*1000 # 10s
|
14
|
+
|
15
|
+
@@hydra = Typhoeus::Hydra.new
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :host, :secure, :api_key, :timeout
|
19
|
+
|
20
|
+
def hydra
|
21
|
+
@@hydra
|
22
|
+
end
|
23
|
+
|
24
|
+
def stubbed?
|
25
|
+
!!@stubbed
|
26
|
+
end
|
27
|
+
|
28
|
+
def stubbed=(value)
|
29
|
+
@stubbed = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def secure=(value, disable_ssl_peer_verification = true)
|
33
|
+
if value && !(Typhoeus::Easy.new.curl_version =~ /OpenSSL/)
|
34
|
+
raise "Your libcurl SSL support not enabled."
|
35
|
+
end
|
36
|
+
@secure = value
|
37
|
+
@disable_ssl_peer_verification = disable_ssl_peer_verification
|
38
|
+
end
|
39
|
+
|
40
|
+
def http_protocol
|
41
|
+
(self.secure)? "https://" : "http://"
|
42
|
+
end
|
43
|
+
|
44
|
+
def hydra_run_all
|
45
|
+
self.hydra.run unless self.stubbed?
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def new_request(url, options={})
|
51
|
+
options = { :disable_ssl_peer_verification => @disable_ssl_peer_verification,
|
52
|
+
:timeout => (@timeout || DEFAULT_TIMEOUT) }.merge(options)
|
53
|
+
|
54
|
+
options[:params] ||= {}
|
55
|
+
options[:params].merge!( :api_key => self.api_key ) unless options[:params][:api_key]
|
56
|
+
|
57
|
+
options[:headers] ||= {}
|
58
|
+
options[:headers].merge!( "Authorization" => self.api_key ) unless options[:headers][:api_key]
|
59
|
+
|
60
|
+
Typhoeus::Request.new(url, options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_json(response)
|
64
|
+
begin
|
65
|
+
Yajl::Parser.parse(response.body)
|
66
|
+
rescue
|
67
|
+
raise ResponseError.new( 502, "Parsing service JSON error: #{response.body}")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def hydra_run(request)
|
72
|
+
self.hydra.queue(request)
|
73
|
+
self.hydra.run
|
74
|
+
response = request.response
|
75
|
+
|
76
|
+
if response.timed_out?
|
77
|
+
# response.curl_return_code => 28
|
78
|
+
# http://curl.haxx.se/libcurl/c/libcurl-errors.html
|
79
|
+
raise ResponseError.new( 504, "Service Timeout")
|
80
|
+
else
|
81
|
+
return response
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "optimis_client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "optimis_client"
|
7
|
+
s.version = OptimisClient::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["OptimisDev Taipei Team"]
|
10
|
+
s.email = ["devteam-tw@optimiscorp.com"]
|
11
|
+
s.homepage = "http://optimisdev.com"
|
12
|
+
s.summary = %q{Optimis Client library}
|
13
|
+
s.description = %q{a Ruby client library for Optimis Service}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
s.add_dependency "typhoeus", "~> 0.2.3"
|
20
|
+
s.add_dependency "yajl-ruby"
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec", "~> 2.5"
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OptimisClient::Base do
|
4
|
+
|
5
|
+
before do
|
6
|
+
OptimisClient::Base.host = 'service.optimis.local'
|
7
|
+
OptimisClient::Base.api_key = '1234567890'
|
8
|
+
OptimisClient::Base.secure = false
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { OptimisClient::Base }
|
12
|
+
|
13
|
+
describe "Setup HTTP protocol" do
|
14
|
+
|
15
|
+
it "supports https" do
|
16
|
+
lambda { OptimisClient::Base.secure = true }.should_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should raise error if your libcurl SSL support not enabled." do
|
20
|
+
Typhoeus::Easy.stub!(:new).and_return(mock("", :curl_version => "libcurl/7.19.7 OpenXXX/0.9.8l zlib/1.2.3"))
|
21
|
+
lambda { OptimisClient::Base.secure = true }.should raise_error(RuntimeError)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path("../../lib/optimis_client", __FILE__)
|
2
|
+
|
3
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
4
|
+
# in spec/support/ and its subdirectories.
|
5
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
|
9
|
+
config.mock_with :rspec
|
10
|
+
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optimis_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- OptimisDev Taipei Team
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: typhoeus
|
16
|
+
requirement: &70127267720740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70127267720740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yajl-ruby
|
27
|
+
requirement: &70127267719860 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70127267719860
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70127267718340 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.5'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70127267718340
|
47
|
+
description: a Ruby client library for Optimis Service
|
48
|
+
email:
|
49
|
+
- devteam-tw@optimiscorp.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/optimis_client.rb
|
59
|
+
- lib/optimis_client/base.rb
|
60
|
+
- lib/optimis_client/version.rb
|
61
|
+
- optimis_client.gemspec
|
62
|
+
- spec/lib/base_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: http://optimisdev.com
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.11
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Optimis Client library
|
88
|
+
test_files:
|
89
|
+
- spec/lib/base_spec.rb
|
90
|
+
- spec/spec_helper.rb
|