mushikago-sdk 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mushikago/auth/signature.rb +18 -0
- data/lib/mushikago/auth/signer.rb +23 -0
- data/lib/mushikago/auth.rb +7 -0
- data/lib/mushikago/client.rb +35 -0
- data/lib/mushikago/configuration.rb +49 -0
- data/lib/mushikago/http/request.rb +70 -0
- data/lib/mushikago/http/response.rb +18 -0
- data/lib/mushikago/http.rb +7 -0
- data/lib/mushikago/tombo/capture_request.rb +27 -0
- data/lib/mushikago/tombo/captures_request.rb +23 -0
- data/lib/mushikago/tombo/client.rb +25 -0
- data/lib/mushikago/tombo/delete_request.rb +18 -0
- data/lib/mushikago/tombo/info_request.rb +10 -0
- data/lib/mushikago/tombo/request.rb +16 -0
- data/lib/mushikago/tombo.rb +10 -0
- data/lib/mushikago/version.rb +3 -0
- data/lib/mushikago-sdk.rb +1 -0
- data/lib/mushikago.rb +15 -0
- data/spec/mushikago/auth/signature_spec.rb +29 -0
- data/spec/mushikago/auth/signer_spec.rb +7 -0
- data/spec/mushikago/client_spec.rb +43 -0
- data/spec/mushikago/configuration_spec.rb +64 -0
- data/spec/mushikago/http/request_spec.rb +40 -0
- data/spec/mushikago/http/response_spec.rb +26 -0
- data/spec/mushikago/tombo/capture_request_spec.rb +54 -0
- data/spec/mushikago/tombo/captures_request_spec.rb +40 -0
- data/spec/mushikago/tombo/client_spec.rb +86 -0
- data/spec/mushikago/tombo/delete_request_spec.rb +16 -0
- data/spec/mushikago/tombo/info_request_spec.rb +14 -0
- data/spec/spec_helper.rb +4 -0
- metadata +205 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Auth
|
3
|
+
module Signature
|
4
|
+
def string_to_sign
|
5
|
+
[
|
6
|
+
http_method,
|
7
|
+
host,
|
8
|
+
path,
|
9
|
+
url_encoded_params,
|
10
|
+
].join("\n")
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_signature! signer
|
14
|
+
set_param('signature', signer.sign(string_to_sign))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
module Mushikago
|
5
|
+
module Auth
|
6
|
+
class Signer
|
7
|
+
attr_reader :secret_key
|
8
|
+
def initialize secret_key
|
9
|
+
@secret_key = secret_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def sign(string_to_sign, digest_method='sha256')
|
13
|
+
Base64.encode64(
|
14
|
+
OpenSSL::HMAC.digest(
|
15
|
+
OpenSSL::Digest::Digest.new(digest_method),
|
16
|
+
secret_key,
|
17
|
+
string_to_sign
|
18
|
+
)
|
19
|
+
).strip
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
module Mushikago
|
4
|
+
class Client
|
5
|
+
# @return [String] api_key
|
6
|
+
attr_reader :api_key
|
7
|
+
|
8
|
+
# @return [Signer] signer
|
9
|
+
attr_reader :signer
|
10
|
+
|
11
|
+
# @param [Hash] options
|
12
|
+
# @option options [String] :api_key(Configured value) APIキー
|
13
|
+
# @option options [String] :secret_key(Configured value) 秘密鍵
|
14
|
+
def initialize options={}
|
15
|
+
@api_key = (options[:api_key] || Mushikago.config.api_key).to_s
|
16
|
+
@signer = Mushikago::Auth::Signer.new((options[:secret_key] || Mushikago.config.secret_key).to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [Mushikago::Http::Request] request
|
20
|
+
# @return [Mushikago::Http::Response] response
|
21
|
+
def send_request request
|
22
|
+
# add authorization
|
23
|
+
request.set_param('api_key', api_key)
|
24
|
+
request.set_param('timestamp', Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ'))
|
25
|
+
request.add_signature!(signer)
|
26
|
+
|
27
|
+
# send request
|
28
|
+
Net::HTTP.start(request.host) do |http|
|
29
|
+
http_request = request.to_http_request
|
30
|
+
http_response = http.request(http_request)
|
31
|
+
return Mushikago::Http::Response.new(JSON.parse(http_response.body))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
module Mushikago
|
3
|
+
class Config
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
# @param [Hash] options optionsをロードします
|
7
|
+
# @option options [string] :api_key('環境変数:MUSHIKAGO_API_KEY') 発行されたAPIKeyを設定する
|
8
|
+
# @option options [string] :secret_key('環境変数:MUSHIKAGO_SECRET_KEY') 発行されたSecretKeyを設定する
|
9
|
+
# @option options [string] :tombo_endpoint('tombo.mushikago.org') tomboサービスのエンドポイントを設定する
|
10
|
+
# @example
|
11
|
+
# Mushikago::Config.load(:api_key => 'ABCDEFG', :secret_key => 'HIJKLMN')
|
12
|
+
def load options={}
|
13
|
+
options.each do |key, value|
|
14
|
+
supplied[key.to_sym] = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# @param [Symbol] name オプション名
|
20
|
+
# @param [Object] default_value デフォルト値
|
21
|
+
# @param [Hash] options 型情報を補足的に追加することができる
|
22
|
+
# @param [Block] transform 渡された値を変換するブロック
|
23
|
+
def add_option name, default_value = nil, options = {}, &transform
|
24
|
+
name = name.to_sym
|
25
|
+
raise "The option #{name} is already defined!" if self.respond_to?(name)
|
26
|
+
|
27
|
+
define_method(name) do
|
28
|
+
value = supplied.has_key?(name) ? supplied[name] : default_value
|
29
|
+
transform ? transform.call(value) : value
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_class_method("#{name}?", name) if options[:boolean]
|
33
|
+
end
|
34
|
+
|
35
|
+
def load options={}
|
36
|
+
instance.load options
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
add_option :api_key, ENV['MUSHIKAGO_API_KEY'] || ENV['MUSHIKAGO_API_KEY_ID']
|
41
|
+
add_option :secret_key, ENV['MUSHIKAGO_SECRET_KEY'] || ENV['MUSHIKAGO_SECRET_ACCESS_KEY']
|
42
|
+
add_option :tombo_endpoint, 'tombo.mushikago.org'
|
43
|
+
|
44
|
+
private
|
45
|
+
def supplied
|
46
|
+
@supplied ||= {}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'cgi'
|
3
|
+
module Mushikago
|
4
|
+
module Http
|
5
|
+
class Request
|
6
|
+
# @return [Hash] headers
|
7
|
+
attr_reader :headers
|
8
|
+
# @return [String] http_method
|
9
|
+
attr_accessor :http_method
|
10
|
+
# @return [String] host
|
11
|
+
attr_accessor :host
|
12
|
+
# @return [String] path
|
13
|
+
attr_accessor :path
|
14
|
+
# @return [String] params
|
15
|
+
attr_reader :params
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@headers = {}
|
19
|
+
@host = ''
|
20
|
+
@path = '/'
|
21
|
+
@params = {}
|
22
|
+
@http_method = new_http_request.method
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_param key, value
|
26
|
+
params[key] = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_param key
|
30
|
+
param = params.detect{|p| p[0] == key}
|
31
|
+
param ? param[1] : nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def url_encoded_params
|
35
|
+
params.sort.collect{|pp| pp.map{|p| encode p}.join('=')}.join('&')
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_http_request
|
39
|
+
http_request = new_http_request
|
40
|
+
headers.each do |key, value|
|
41
|
+
http_request[key] = value
|
42
|
+
end
|
43
|
+
http_request.body = url_encoded_params if http_request.request_body_permitted?
|
44
|
+
return http_request
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
def new_http_request
|
49
|
+
Net::HTTP::Get.new("#{path}?#{url_encoded_params}")
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def encode s
|
54
|
+
CGI.escape(s).gsub('+', '%20')
|
55
|
+
end
|
56
|
+
|
57
|
+
class << self
|
58
|
+
def add_param name, &transform
|
59
|
+
attr_accessor name
|
60
|
+
define_method("#{name}=") do |value|
|
61
|
+
value = transform ? transform.call(value) : value
|
62
|
+
set_param(name.to_s, value)
|
63
|
+
instance_variable_set("@#{name}".to_sym, value)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Http
|
3
|
+
class Response
|
4
|
+
attr_reader :meta
|
5
|
+
attr_reader :response
|
6
|
+
|
7
|
+
def initialize options={}
|
8
|
+
@meta = options[:meta] || options['meta']
|
9
|
+
@response = options[:response] || options['response']
|
10
|
+
end
|
11
|
+
|
12
|
+
def [] key
|
13
|
+
response[key]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
class CaptureRequest < Request
|
4
|
+
add_param :url
|
5
|
+
add_param :image_format
|
6
|
+
add_param :image_quality
|
7
|
+
add_param :thumbnail do |v| (v.to_i != 0 ? 1 : 0).to_s end
|
8
|
+
add_param :tags do |v| [v].flatten.compact.join(',') end
|
9
|
+
|
10
|
+
def initialize url, options={}
|
11
|
+
super(options)
|
12
|
+
self.url = url
|
13
|
+
self.image_format = options[:image_format] if options.has_key?(:image_format)
|
14
|
+
self.image_quality = options[:image_quality] if options.has_key?(:image_quality)
|
15
|
+
self.thumbnail = options[:thumbnail] if options.has_key?(:thumbnail)
|
16
|
+
self.tags = options[:tags] if options.has_key?(:tags)
|
17
|
+
@path = "/#{api_version}/capture.json"
|
18
|
+
@headers['Content-type'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
19
|
+
end
|
20
|
+
|
21
|
+
def new_http_request
|
22
|
+
Net::HTTP::Post.new(path)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
class CapturesRequest < Request
|
4
|
+
add_param :id
|
5
|
+
add_param :limit
|
6
|
+
add_param :offset
|
7
|
+
add_param :domain
|
8
|
+
add_param :tag
|
9
|
+
|
10
|
+
def initialize options={}
|
11
|
+
super(options)
|
12
|
+
self.id = options[:id] if options.has_key?(:id)
|
13
|
+
self.limit = options[:limit] if options.has_key?(:limit)
|
14
|
+
self.offset = options[:offset] if options.has_key?(:offset)
|
15
|
+
self.domain = options[:domain] if options.has_key?(:domain)
|
16
|
+
self.tag = options[:tag] if options.has_key?(:tag)
|
17
|
+
@host = options[:endpoint] ||= Mushikago.config.tombo_endpoint
|
18
|
+
@path = "/#{api_version}/captures.json"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
class Client < Mushikago::Client
|
4
|
+
def capture url, options={}
|
5
|
+
request = CaptureRequest.new(url, options)
|
6
|
+
send_request(request)
|
7
|
+
end
|
8
|
+
|
9
|
+
def captures options={}
|
10
|
+
request = CapturesRequest.new(options)
|
11
|
+
send_request(request)
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete id, options={}
|
15
|
+
request = DeleteRequest.new(id, options)
|
16
|
+
send_request(request)
|
17
|
+
end
|
18
|
+
|
19
|
+
def info options={}
|
20
|
+
request = InfoRequest.new(options)
|
21
|
+
send_request(request)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
class DeleteRequest < Request
|
4
|
+
add_param :id
|
5
|
+
|
6
|
+
def initialize id, options={}
|
7
|
+
super(options)
|
8
|
+
self.id = id
|
9
|
+
@path = "/#{api_version}/delete.json"
|
10
|
+
end
|
11
|
+
|
12
|
+
def new_http_request
|
13
|
+
Net::HTTP::Delete.new("#{path}?#{url_encoded_params}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
class Request < Mushikago::Http::Request
|
4
|
+
include Mushikago::Auth::Signature
|
5
|
+
|
6
|
+
def initialize options={}
|
7
|
+
super()
|
8
|
+
@host = options[:endpoint] ||= Mushikago.config.tombo_endpoint
|
9
|
+
end
|
10
|
+
|
11
|
+
def api_version
|
12
|
+
1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Mushikago
|
2
|
+
module Tombo
|
3
|
+
autoload :Client, 'mushikago/tombo/client'
|
4
|
+
autoload :Request, 'mushikago/tombo/request'
|
5
|
+
autoload :CaptureRequest, 'mushikago/tombo/capture_request'
|
6
|
+
autoload :CapturesRequest, 'mushikago/tombo/captures_request'
|
7
|
+
autoload :DeleteRequest, 'mushikago/tombo/delete_request'
|
8
|
+
autoload :InfoRequest, 'mushikago/tombo/info_request'
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'mushikago'
|
data/lib/mushikago.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Mushikago
|
2
|
+
# class
|
3
|
+
autoload :Config, 'mushikago/configuration'
|
4
|
+
autoload :Client, 'mushikago/client'
|
5
|
+
|
6
|
+
# module
|
7
|
+
autoload :Http, 'mushikago/http'
|
8
|
+
autoload :Auth, 'mushikago/auth'
|
9
|
+
autoload :Tombo, 'mushikago/tombo'
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
Config.instance
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestRequest < Mushikago::Http::Request
|
4
|
+
include Mushikago::Auth::Signature
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Mushikago::Auth::Signature do
|
8
|
+
before do
|
9
|
+
@request = TestRequest.new
|
10
|
+
@request.http_method = 'GET'
|
11
|
+
@request.host = 'mushikago.org'
|
12
|
+
@request.path = '/1/someapi.json'
|
13
|
+
@request.set_param('api_key', 'api_key')
|
14
|
+
@request.set_param('timestamp', '2011-09-01T00:00:00Z')
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'generates string to sign' do
|
18
|
+
subject{ @request.string_to_sign }
|
19
|
+
it{ should == "GET\nmushikago.org\n/1/someapi.json\napi_key=api_key×tamp=2011-09-01T00%3A00%3A00Z" }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'add signature' do
|
23
|
+
before do
|
24
|
+
@request.add_signature!(Mushikago::Auth::Signer.new('secret_key'))
|
25
|
+
end
|
26
|
+
subject{ @request }
|
27
|
+
it{ subject.get_param('signature').should == 'sJSFFQREYbNPd2kJK3kuAR3uSgvcbuWtxGFjan+R3w8=' }
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Auth::Signer do
|
4
|
+
subject{ Mushikago::Auth::Signer.new('secret_key') }
|
5
|
+
it{ subject.sign('a').should == 'xkEgZ9jFgFpvfp6KFGd8tGXDZkuS0FGMAsyvyP6YglQ=' }
|
6
|
+
it{ subject.sign('b').should == 'RIPOrMecy+TXyaLH2cLiETj4B713c5UD9YVZuOMHVR8=' }
|
7
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestRequest < Mushikago::Http::Request
|
4
|
+
include Mushikago::Auth::Signature
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
describe Mushikago::Client do
|
9
|
+
context 'construct without options' do
|
10
|
+
before :all do
|
11
|
+
@client = Mushikago::Client.new
|
12
|
+
end
|
13
|
+
|
14
|
+
subject{ @client }
|
15
|
+
|
16
|
+
it{ subject.api_key.should == Mushikago.config.api_key.to_s }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'construct with options' do
|
20
|
+
before :all do
|
21
|
+
@client = Mushikago::Client.new(
|
22
|
+
:api_key => 'mushikago api key'
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
subject{ @client }
|
27
|
+
|
28
|
+
it{ subject.api_key.should == 'mushikago api key' }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'send test request' do
|
32
|
+
before :all do
|
33
|
+
request = TestRequest.new
|
34
|
+
request.host = 'tombo.mushikago.org'
|
35
|
+
request.path = '/1/info.json'
|
36
|
+
client = Mushikago::Client.new
|
37
|
+
@response = client.send_request(request)
|
38
|
+
end
|
39
|
+
subject{ @response }
|
40
|
+
it{ should respond_to(:meta, :response) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Config do
|
4
|
+
# Mushikago::Configはシングルトンである
|
5
|
+
context 'is singleton class' do
|
6
|
+
subject{ Mushikago::Config.instance }
|
7
|
+
it{ should == Mushikago::Config.instance }
|
8
|
+
|
9
|
+
# 直接newすることはできない
|
10
|
+
context 'and cannot call new' do
|
11
|
+
subject{ proc{ Mushikago::Config.new } }
|
12
|
+
it{ should raise_error }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Mushikago::Configは以下のキーを持っている
|
17
|
+
context 'has keys' do
|
18
|
+
subject{ Mushikago::Config.instance }
|
19
|
+
it{ should respond_to(:api_key) }
|
20
|
+
it{ should respond_to(:secret_key) }
|
21
|
+
it{ should respond_to(:tombo_endpoint) }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Mushikago::Configはデフォルトの値を持っている
|
25
|
+
context 'has default value' do
|
26
|
+
subject{ Mushikago::Config.instance }
|
27
|
+
it{ subject.api_key.should == (ENV['MUSHIKAGO_API_KEY'] || ENV['MUSHIKAGO_API_KEY_ID']) }
|
28
|
+
it{ subject.secret_key.should == (ENV['MUSHIKAGO_SECRET_KEY'] || ENV['MUSHIKAGO_SECRET_ACCESS_KEY']) }
|
29
|
+
it{ subject.tombo_endpoint.should == 'tombo.mushikago.org' }
|
30
|
+
end
|
31
|
+
|
32
|
+
# Mushikago::Configは値をロードすることができる
|
33
|
+
context 'loads option value' do
|
34
|
+
before :all do
|
35
|
+
Mushikago::Config.load(
|
36
|
+
:api_key => 'api_key',
|
37
|
+
:secret_key => 'secret_key',
|
38
|
+
:tombo_endpoint => 'tombo.mushikago.org'
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
subject{ Mushikago::Config.instance }
|
43
|
+
it{ subject.api_key.should == 'api_key' }
|
44
|
+
it{ subject.secret_key.should == 'secret_key' }
|
45
|
+
it{ subject.tombo_endpoint.should == 'tombo.mushikago.org' }
|
46
|
+
|
47
|
+
# ロードする際のキーは文字列でもOK
|
48
|
+
context 'loading options key can be string' do
|
49
|
+
before :all do
|
50
|
+
Mushikago::Config.load(
|
51
|
+
'api_key' => 'api_key',
|
52
|
+
'secret_key' => 'secret_key',
|
53
|
+
'tombo_endpoint' => 'tombo.mushikago.org'
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
subject{ Mushikago::Config.instance }
|
58
|
+
it{ subject.api_key.should == 'api_key' }
|
59
|
+
it{ subject.secret_key.should == 'secret_key' }
|
60
|
+
it{ subject.tombo_endpoint.should == 'tombo.mushikago.org' }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Http::Request do
|
4
|
+
before :all do
|
5
|
+
@request = Mushikago::Http::Request.new
|
6
|
+
@request.host = 'tombo.ap-northeast-1.mushikago.org'
|
7
|
+
@request.path = '/1/list.json'
|
8
|
+
@request.headers['hoge'] = 'fuga'
|
9
|
+
@request.set_param('param1', 'p1')
|
10
|
+
@request.set_param('param2', 'p2')
|
11
|
+
@request.set_param('timestamp', '2011-09-01T00:00:00Z')
|
12
|
+
end
|
13
|
+
|
14
|
+
subject{ @request }
|
15
|
+
|
16
|
+
it{ should respond_to(:http_method) }
|
17
|
+
it{ should respond_to(:host, :host=) }
|
18
|
+
it{ should respond_to(:path, :path=) }
|
19
|
+
it{ should respond_to(:headers) }
|
20
|
+
it{ should respond_to(:params) }
|
21
|
+
it{ should respond_to(:set_param) }
|
22
|
+
it{ should respond_to(:get_param) }
|
23
|
+
|
24
|
+
it{ subject.http_method.should == 'GET' }
|
25
|
+
it{ subject.host.should == 'tombo.ap-northeast-1.mushikago.org' }
|
26
|
+
it{ subject.path.should == '/1/list.json' }
|
27
|
+
it{ subject.headers.should be_a_kind_of(Hash) }
|
28
|
+
it{ subject.headers['hoge'].should == 'fuga' }
|
29
|
+
it{ subject.get_param('param1').should == 'p1' }
|
30
|
+
it{ subject.get_param('param2').should == 'p2' }
|
31
|
+
it{ subject.get_param('timestamp').should == '2011-09-01T00:00:00Z' }
|
32
|
+
|
33
|
+
it{ subject.url_encoded_params.should == 'param1=p1¶m2=p2×tamp=2011-09-01T00%3A00%3A00Z' }
|
34
|
+
|
35
|
+
context 'to http request' do
|
36
|
+
subject{ @request.to_http_request }
|
37
|
+
it{ should be_a_kind_of(Net::HTTPRequest) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Http::Response do
|
4
|
+
before do
|
5
|
+
response = {
|
6
|
+
:meta => {
|
7
|
+
:status => 200,
|
8
|
+
:message => 'OK'
|
9
|
+
},
|
10
|
+
:response => {
|
11
|
+
:hello => 'world'
|
12
|
+
}
|
13
|
+
}
|
14
|
+
@response = Mushikago::Http::Response.new(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
subject{ @response }
|
18
|
+
|
19
|
+
it{ should respond_to(:meta, :response) }
|
20
|
+
it{ should respond_to(:[]) }
|
21
|
+
|
22
|
+
it{ subject.meta[:status].should == 200 }
|
23
|
+
it{ subject.meta[:message].should == 'OK' }
|
24
|
+
it{ subject.response[:hello].should == 'world' }
|
25
|
+
it{ subject[:hello].should == 'world' }
|
26
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Tombo::CaptureRequest do
|
4
|
+
before :all do
|
5
|
+
@request = Mushikago::Tombo::CaptureRequest.new('http://www.mushikago.org/')
|
6
|
+
end
|
7
|
+
|
8
|
+
subject{ @request }
|
9
|
+
|
10
|
+
it{ should respond_to(:url) }
|
11
|
+
it{ should respond_to(:image_format) }
|
12
|
+
it{ should respond_to(:image_quality) }
|
13
|
+
it{ should respond_to(:thumbnail) }
|
14
|
+
it{ should respond_to(:tags) }
|
15
|
+
it{ should respond_to(:add_signature!) }
|
16
|
+
|
17
|
+
context 'with options' do
|
18
|
+
before :all do
|
19
|
+
options = {
|
20
|
+
:image_format => 'png',
|
21
|
+
:image_quality => '20',
|
22
|
+
:thumbnail => '1',
|
23
|
+
:tags => 'hoge,fuga',
|
24
|
+
}
|
25
|
+
@request = Mushikago::Tombo::CaptureRequest.new('http://www.mushikago.org/', options)
|
26
|
+
end
|
27
|
+
|
28
|
+
subject{ @request }
|
29
|
+
|
30
|
+
it{ subject.host.should == Mushikago.config.tombo_endpoint }
|
31
|
+
it{ subject.http_method.should == 'POST' }
|
32
|
+
it{ subject.path.should == '/1/capture.json' }
|
33
|
+
it{ subject.url.should == 'http://www.mushikago.org/' }
|
34
|
+
it{ subject.image_format.should == 'png' }
|
35
|
+
it{ subject.image_quality.should == '20' }
|
36
|
+
it{ subject.thumbnail.should == '1' }
|
37
|
+
it{ subject.tags.should == 'hoge,fuga' }
|
38
|
+
it{ subject.url_encoded_params.should == 'image_format=png&image_quality=20&tags=hoge%2Cfuga&thumbnail=1&url=http%3A%2F%2Fwww.mushikago.org%2F' }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'tags can be array and thumbnail is 0' do
|
42
|
+
before :all do
|
43
|
+
options = {
|
44
|
+
:thumbnail => 0,
|
45
|
+
:tags => ['hoge','fuga'],
|
46
|
+
}
|
47
|
+
@request = Mushikago::Tombo::CaptureRequest.new('http://www.mushikago.org/', options)
|
48
|
+
end
|
49
|
+
|
50
|
+
subject{ @request }
|
51
|
+
it{ subject.thumbnail.should == '0' }
|
52
|
+
it{ subject.tags.should == 'hoge,fuga' }
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Tombo::CapturesRequest do
|
4
|
+
before :all do
|
5
|
+
@request = Mushikago::Tombo::CapturesRequest.new
|
6
|
+
end
|
7
|
+
|
8
|
+
subject{ @request }
|
9
|
+
|
10
|
+
it{ should respond_to(:id) }
|
11
|
+
it{ should respond_to(:limit) }
|
12
|
+
it{ should respond_to(:offset) }
|
13
|
+
it{ should respond_to(:domain) }
|
14
|
+
it{ should respond_to(:tag) }
|
15
|
+
|
16
|
+
context 'with options' do
|
17
|
+
before :all do
|
18
|
+
options = {
|
19
|
+
:id => 'id',
|
20
|
+
:limit => '2',
|
21
|
+
:offset => '5',
|
22
|
+
:domain => 'hogehoge.com',
|
23
|
+
:tag => 'tag',
|
24
|
+
}
|
25
|
+
@request = Mushikago::Tombo::CapturesRequest.new(options)
|
26
|
+
end
|
27
|
+
|
28
|
+
subject{ @request }
|
29
|
+
|
30
|
+
it{ subject.host.should == Mushikago.config.tombo_endpoint }
|
31
|
+
it{ subject.http_method.should == 'GET' }
|
32
|
+
it{ subject.path.should == '/1/captures.json' }
|
33
|
+
it{ subject.id.should == 'id' }
|
34
|
+
it{ subject.limit.should == '2' }
|
35
|
+
it{ subject.offset.should == '5' }
|
36
|
+
it{ subject.domain.should == 'hogehoge.com' }
|
37
|
+
it{ subject.tag.should == 'tag' }
|
38
|
+
it{ subject.url_encoded_params.should == 'domain=hogehoge.com&id=id&limit=2&offset=5&tag=tag' }
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Tombo::Client do
|
4
|
+
before :all do
|
5
|
+
@client = Mushikago::Tombo::Client.new(
|
6
|
+
:api_key => ENV['MUSHIKAGO_API_KEY'],
|
7
|
+
:secret_key => ENV['MUSHIKAGO_SECRET_KEY']
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
subject{ @client }
|
12
|
+
|
13
|
+
it{ should respond_to(:capture) }
|
14
|
+
it{ should respond_to(:captures) }
|
15
|
+
it{ should respond_to(:delete) }
|
16
|
+
it{ should respond_to(:info) }
|
17
|
+
=begin
|
18
|
+
context 'send capture request' do
|
19
|
+
before :all do
|
20
|
+
@response = @client.capture('http://www.tombo.ne.jp/', :thumbnail=>1)
|
21
|
+
end
|
22
|
+
|
23
|
+
subject{ @response }
|
24
|
+
|
25
|
+
it{ subject.meta['status'].should == 200 }
|
26
|
+
it{ subject.meta['message'].should == 'OK' }
|
27
|
+
it{ subject.response.should_not be_nil }
|
28
|
+
it{ subject['id'].should_not be_nil }
|
29
|
+
it{ subject['image_url'].should_not be_nil }
|
30
|
+
it{ subject['thumbnail_url'].should_not be_nil }
|
31
|
+
end
|
32
|
+
=end
|
33
|
+
|
34
|
+
context 'send captures request' do
|
35
|
+
before :all do
|
36
|
+
@response = @client.captures
|
37
|
+
end
|
38
|
+
|
39
|
+
subject{ @response }
|
40
|
+
it{ subject.meta['status'].should == 200 }
|
41
|
+
it{ subject.meta['message'].should == 'OK' }
|
42
|
+
it{ subject.response.should_not be_nil }
|
43
|
+
it{ subject['total'].should_not be_nil }
|
44
|
+
it{ subject['images'].should_not be_nil }
|
45
|
+
it{ subject['images'].should be_a_kind_of(Enumerable) }
|
46
|
+
it{ subject['images'].should have_at_most(subject['total'].to_i).items }
|
47
|
+
|
48
|
+
context 'returned images should have keys' do
|
49
|
+
subject{ @response['images'].first }
|
50
|
+
it{ subject['image_id'].should_not be_nil }
|
51
|
+
it{ subject['image_url'].should_not be_nil }
|
52
|
+
it{ subject['image_size'].should_not be_nil }
|
53
|
+
it{ subject['source_url'].should_not be_nil }
|
54
|
+
it{ subject['image_format'].should_not be_nil }
|
55
|
+
it{ subject['image_quality'].should_not be_nil }
|
56
|
+
it{ subject['state'].should_not be_nil }
|
57
|
+
it{ subject['tags'].should_not be_nil }
|
58
|
+
it{ subject['tags'].should be_a_kind_of(Enumerable) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'send delete request' do
|
63
|
+
before :all do
|
64
|
+
@response = @client.delete('391d7fe4-e9d4-4975-85a3-e5d38a4cd97f')
|
65
|
+
end
|
66
|
+
|
67
|
+
subject{ @response }
|
68
|
+
|
69
|
+
it{ subject.meta['status'].should == 200 }
|
70
|
+
it{ subject['id'].should_not be_nil }
|
71
|
+
it{ subject['id'].should == '391d7fe4-e9d4-4975-85a3-e5d38a4cd97f' }
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'send info request' do
|
75
|
+
before :all do
|
76
|
+
@response = @client.info
|
77
|
+
end
|
78
|
+
|
79
|
+
subject{ @response }
|
80
|
+
|
81
|
+
it{ subject.meta['status'].should == 200 }
|
82
|
+
it{ subject['api_count'].should_not be_nil }
|
83
|
+
it{ subject['disk_usage'].should_not be_nil }
|
84
|
+
it{ subject['image_num'].should_not be_nil }
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Tombo::DeleteRequest do
|
4
|
+
before :all do
|
5
|
+
@request = Mushikago::Tombo::DeleteRequest.new('391d7fe4-e9d4-4975-85a3-e5d38a4cd97f')
|
6
|
+
end
|
7
|
+
|
8
|
+
subject{ @request }
|
9
|
+
|
10
|
+
it{ should respond_to(:id) }
|
11
|
+
it{ subject.host.should == Mushikago.config.tombo_endpoint }
|
12
|
+
it{ subject.http_method.should == 'DELETE' }
|
13
|
+
it{ subject.path.should == '/1/delete.json' }
|
14
|
+
it{ subject.id.should == '391d7fe4-e9d4-4975-85a3-e5d38a4cd97f' }
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mushikago::Tombo::InfoRequest do
|
4
|
+
before :all do
|
5
|
+
@request = Mushikago::Tombo::InfoRequest.new
|
6
|
+
end
|
7
|
+
|
8
|
+
subject{ @request }
|
9
|
+
|
10
|
+
it{ subject.host.should == Mushikago.config.tombo_endpoint }
|
11
|
+
it{ subject.http_method.should == 'GET' }
|
12
|
+
it{ subject.path.should == '/1/info.json' }
|
13
|
+
end
|
14
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mushikago-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Toru Matsuoka
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-05 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: maruku
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: rspec
|
78
|
+
prerelease: false
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ~>
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 23
|
85
|
+
segments:
|
86
|
+
- 2
|
87
|
+
- 6
|
88
|
+
- 0
|
89
|
+
version: 2.6.0
|
90
|
+
type: :development
|
91
|
+
version_requirements: *id005
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: ZenTest
|
94
|
+
prerelease: false
|
95
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
type: :development
|
105
|
+
version_requirements: *id006
|
106
|
+
- !ruby/object:Gem::Dependency
|
107
|
+
name: bundler
|
108
|
+
prerelease: false
|
109
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
type: :development
|
119
|
+
version_requirements: *id007
|
120
|
+
description: A SDK for Mushikago.
|
121
|
+
email:
|
122
|
+
- t.matsuoka@miningbrownie.co.jp
|
123
|
+
executables: []
|
124
|
+
|
125
|
+
extensions: []
|
126
|
+
|
127
|
+
extra_rdoc_files: []
|
128
|
+
|
129
|
+
files:
|
130
|
+
- lib/mushikago.rb
|
131
|
+
- lib/mushikago/tombo.rb
|
132
|
+
- lib/mushikago/auth/signer.rb
|
133
|
+
- lib/mushikago/auth/signature.rb
|
134
|
+
- lib/mushikago/tombo/request.rb
|
135
|
+
- lib/mushikago/tombo/info_request.rb
|
136
|
+
- lib/mushikago/tombo/capture_request.rb
|
137
|
+
- lib/mushikago/tombo/captures_request.rb
|
138
|
+
- lib/mushikago/tombo/delete_request.rb
|
139
|
+
- lib/mushikago/tombo/client.rb
|
140
|
+
- lib/mushikago/version.rb
|
141
|
+
- lib/mushikago/auth.rb
|
142
|
+
- lib/mushikago/configuration.rb
|
143
|
+
- lib/mushikago/http/request.rb
|
144
|
+
- lib/mushikago/http/response.rb
|
145
|
+
- lib/mushikago/client.rb
|
146
|
+
- lib/mushikago/http.rb
|
147
|
+
- lib/mushikago-sdk.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
- spec/mushikago/client_spec.rb
|
150
|
+
- spec/mushikago/auth/signer_spec.rb
|
151
|
+
- spec/mushikago/auth/signature_spec.rb
|
152
|
+
- spec/mushikago/tombo/client_spec.rb
|
153
|
+
- spec/mushikago/tombo/delete_request_spec.rb
|
154
|
+
- spec/mushikago/tombo/captures_request_spec.rb
|
155
|
+
- spec/mushikago/tombo/info_request_spec.rb
|
156
|
+
- spec/mushikago/tombo/capture_request_spec.rb
|
157
|
+
- spec/mushikago/configuration_spec.rb
|
158
|
+
- spec/mushikago/http/response_spec.rb
|
159
|
+
- spec/mushikago/http/request_spec.rb
|
160
|
+
homepage: http://www.mushikago.org/
|
161
|
+
licenses: []
|
162
|
+
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
hash: 3
|
174
|
+
segments:
|
175
|
+
- 0
|
176
|
+
version: "0"
|
177
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
none: false
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
hash: 3
|
183
|
+
segments:
|
184
|
+
- 0
|
185
|
+
version: "0"
|
186
|
+
requirements: []
|
187
|
+
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 1.8.10
|
190
|
+
signing_key:
|
191
|
+
specification_version: 3
|
192
|
+
summary: A SDK for Mushikago.
|
193
|
+
test_files:
|
194
|
+
- spec/spec_helper.rb
|
195
|
+
- spec/mushikago/client_spec.rb
|
196
|
+
- spec/mushikago/auth/signer_spec.rb
|
197
|
+
- spec/mushikago/auth/signature_spec.rb
|
198
|
+
- spec/mushikago/tombo/client_spec.rb
|
199
|
+
- spec/mushikago/tombo/delete_request_spec.rb
|
200
|
+
- spec/mushikago/tombo/captures_request_spec.rb
|
201
|
+
- spec/mushikago/tombo/info_request_spec.rb
|
202
|
+
- spec/mushikago/tombo/capture_request_spec.rb
|
203
|
+
- spec/mushikago/configuration_spec.rb
|
204
|
+
- spec/mushikago/http/response_spec.rb
|
205
|
+
- spec/mushikago/http/request_spec.rb
|