mushikago-sdk 0.1.3 → 0.1.4
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.
- data/lib/mushikago/auth/signature.rb +9 -0
- data/lib/mushikago/auth/signer.rb +7 -0
- data/lib/mushikago/auth.rb +1 -0
- data/lib/mushikago/configuration.rb +6 -8
- data/lib/mushikago/http/client.rb +37 -0
- data/lib/mushikago/http/response.rb +32 -0
- data/lib/mushikago/http.rb +2 -0
- data/lib/mushikago/tombo/capture_request.rb +7 -1
- data/lib/mushikago/tombo/captures_request.rb +6 -0
- data/lib/mushikago/tombo/client.rb +51 -1
- data/lib/mushikago/tombo/delete_request.rb +2 -0
- data/lib/mushikago/tombo/info_request.rb +1 -0
- data/lib/mushikago/tombo.rb +1 -0
- data/lib/mushikago/version.rb +1 -1
- data/lib/mushikago.rb +2 -1
- data/spec/mushikago/configuration_spec.rb +2 -2
- data/spec/mushikago/{client_spec.rb → http/client_spec.rb} +4 -4
- data/spec/mushikago/http/response_spec.rb +3 -0
- data/spec/mushikago/tombo/capture_request_spec.rb +1 -0
- metadata +6 -6
- data/lib/mushikago/client.rb +0 -35
@@ -1,6 +1,13 @@
|
|
1
1
|
module Mushikago
|
2
2
|
module Auth
|
3
|
+
# MushikagoのリクエストにMix-inして利用する
|
3
4
|
module Signature
|
5
|
+
# リクエストの情報から以下のような署名用文字列を作成する
|
6
|
+
# GET
|
7
|
+
# localhost
|
8
|
+
# /1/info.json
|
9
|
+
# api_key=api_key×tamp=2011-09-01T00%3A00%3A00Z
|
10
|
+
# @return [String] 署名用の文字列
|
4
11
|
def string_to_sign
|
5
12
|
[
|
6
13
|
http_method,
|
@@ -10,6 +17,8 @@ module Mushikago
|
|
10
17
|
].join("\n")
|
11
18
|
end
|
12
19
|
|
20
|
+
# リクエストに署名を追加する
|
21
|
+
# @param signer [Mushikago::Auth::Signer] 署名を作成するオブジェクト
|
13
22
|
def add_signature! signer
|
14
23
|
set_param('signature', signer.sign(string_to_sign))
|
15
24
|
end
|
@@ -3,12 +3,19 @@ require 'openssl'
|
|
3
3
|
|
4
4
|
module Mushikago
|
5
5
|
module Auth
|
6
|
+
# 署名を作成するクラス
|
6
7
|
class Signer
|
8
|
+
# @return [String] シークレットキー
|
7
9
|
attr_reader :secret_key
|
10
|
+
|
11
|
+
# @param [String] secret_key シークレットキー
|
8
12
|
def initialize secret_key
|
9
13
|
@secret_key = secret_key
|
10
14
|
end
|
11
15
|
|
16
|
+
# @param [String] string_to_sign 署名の元になる文字列
|
17
|
+
# @param [String] digest_method 署名作成のアルゴリズム
|
18
|
+
# @return [String] 署名
|
12
19
|
def sign(string_to_sign, digest_method='sha256')
|
13
20
|
Base64.encode64(
|
14
21
|
OpenSSL::HMAC.digest(
|
data/lib/mushikago/auth.rb
CHANGED
@@ -4,11 +4,11 @@ module Mushikago
|
|
4
4
|
include Singleton
|
5
5
|
|
6
6
|
# @param [Hash] options optionsをロードします
|
7
|
-
# @option options [
|
8
|
-
# @option options [
|
9
|
-
# @option options [
|
7
|
+
# @option options [String] :api_key(ENV['MUSHIKAGO_API_KEY']) 発行されたAPIKeyを設定する
|
8
|
+
# @option options [String] :secret_key(ENV['MUSHIKAGO_SECRET_KEY']) 発行されたSecretKeyを設定する
|
9
|
+
# @option options [String] :tombo_endpoint('tombo.mushikago.org') tomboサービスのエンドポイントを設定する
|
10
10
|
# @example
|
11
|
-
# Mushikago
|
11
|
+
# Mushikago.config.load(:api_key => 'ABCDEFG', :secret_key => 'HIJKLMN')
|
12
12
|
def load options={}
|
13
13
|
options.each do |key, value|
|
14
14
|
supplied[key.to_sym] = value
|
@@ -20,6 +20,8 @@ module Mushikago
|
|
20
20
|
# @param [Object] default_value デフォルト値
|
21
21
|
# @param [Hash] options 型情報を補足的に追加することができる
|
22
22
|
# @param [Block] transform 渡された値を変換するブロック
|
23
|
+
# @option options [Symbol] :boolean 自動的にname?のエイリアスメソッドが生成されます
|
24
|
+
private
|
23
25
|
def add_option name, default_value = nil, options = {}, &transform
|
24
26
|
name = name.to_sym
|
25
27
|
raise "The option #{name} is already defined!" if self.respond_to?(name)
|
@@ -31,10 +33,6 @@ module Mushikago
|
|
31
33
|
|
32
34
|
alias_class_method("#{name}?", name) if options[:boolean]
|
33
35
|
end
|
34
|
-
|
35
|
-
def load options={}
|
36
|
-
instance.load options
|
37
|
-
end
|
38
36
|
end
|
39
37
|
|
40
38
|
add_option :api_key, ENV['MUSHIKAGO_API_KEY'] || ENV['MUSHIKAGO_API_KEY_ID']
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
module Mushikago
|
4
|
+
module Http
|
5
|
+
class Client
|
6
|
+
# @return [String] api_key
|
7
|
+
attr_reader :api_key
|
8
|
+
|
9
|
+
# @return [Signer] signer
|
10
|
+
attr_reader :signer
|
11
|
+
|
12
|
+
# @param [Hash] options
|
13
|
+
# @option options [String] :api_key(Configured value) APIキー
|
14
|
+
# @option options [String] :secret_key(Configured value) 秘密鍵
|
15
|
+
def initialize options={}
|
16
|
+
@api_key = (options[:api_key] || Mushikago.config.api_key).to_s
|
17
|
+
@signer = Mushikago::Auth::Signer.new((options[:secret_key] || Mushikago.config.secret_key).to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param [Mushikago::Http::Request] request
|
21
|
+
# @return [Mushikago::Http::Response] response
|
22
|
+
def send_request request
|
23
|
+
# add authorization
|
24
|
+
request.set_param('api_key', api_key)
|
25
|
+
request.set_param('timestamp', Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ'))
|
26
|
+
request.add_signature!(signer)
|
27
|
+
|
28
|
+
# send request
|
29
|
+
Net::HTTP.start(request.host, request.port) do |http|
|
30
|
+
http_request = request.to_http_request
|
31
|
+
http_response = http.request(http_request)
|
32
|
+
return Mushikago::Http::Response.new(JSON.parse(http_response.body))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,14 +1,46 @@
|
|
1
1
|
module Mushikago
|
2
2
|
module Http
|
3
|
+
# Mushikagoサービスのレスポンスを扱うクラス
|
4
|
+
# Mushikagoサービスのレスポンスは以下のような構造になっている
|
5
|
+
# {
|
6
|
+
# meta : {
|
7
|
+
# status : 200,
|
8
|
+
# message : 'OK'
|
9
|
+
# },
|
10
|
+
# response : {
|
11
|
+
# // サービス毎に異なる情報
|
12
|
+
# }
|
13
|
+
# }
|
14
|
+
#
|
3
15
|
class Response
|
16
|
+
# @return [Object] レスポンスのメタ情報
|
4
17
|
attr_reader :meta
|
18
|
+
# @return [Object] レスポンスの本体
|
5
19
|
attr_reader :response
|
6
20
|
|
21
|
+
# @param [Hash] options オプション
|
22
|
+
# @option options [Object] :meta レスポンスのメタ情報
|
23
|
+
# @option options [Object] :response レスポンスの本体
|
7
24
|
def initialize options={}
|
8
25
|
@meta = options[:meta] || options['meta']
|
9
26
|
@response = options[:response] || options['response']
|
10
27
|
end
|
11
28
|
|
29
|
+
# メタ情報中のstatusを返す
|
30
|
+
# @return [Integer] ステータス
|
31
|
+
def status
|
32
|
+
meta[:status].to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
# メタ情報中のmessageを返す
|
36
|
+
# @return [String] メッセージ
|
37
|
+
def message
|
38
|
+
meta[:message]
|
39
|
+
end
|
40
|
+
|
41
|
+
# レスポンスの各要素にアクセスする
|
42
|
+
# @param [Symbol] key レスポンスの各要素へのキー
|
43
|
+
# @return [Object] レスポンスの要素
|
12
44
|
def [] key
|
13
45
|
response[key]
|
14
46
|
end
|
data/lib/mushikago/http.rb
CHANGED
@@ -3,10 +3,16 @@ module Mushikago
|
|
3
3
|
class CaptureRequest < Request
|
4
4
|
add_param :url
|
5
5
|
add_param :image_format
|
6
|
-
add_param :image_quality
|
6
|
+
add_param :image_quality do |v| v.to_i.to_s end
|
7
7
|
add_param :thumbnail do |v| (v.to_i != 0 ? 1 : 0).to_s end
|
8
8
|
add_param :tags do |v| [v].flatten.compact.join(',') end
|
9
9
|
|
10
|
+
# @param [String] url キャプチャ対象のURL
|
11
|
+
# @param [Hash] options リクエストのオプション
|
12
|
+
# @option options [String] :image_format('jpg') 画像のフォーマット(jpg,png)
|
13
|
+
# @option options [Integer] :image_quality(80) 画像の品質(0-100)
|
14
|
+
# @option options [Boolean] :thumbnail(0) サムネイル取得フラグ(false:取得しない,true:取得する)
|
15
|
+
# @option options [String,Array] :tags タグ
|
10
16
|
def initialize url, options={}
|
11
17
|
super(options)
|
12
18
|
self.url = url
|
@@ -7,6 +7,12 @@ module Mushikago
|
|
7
7
|
add_param :domain
|
8
8
|
add_param :tag
|
9
9
|
|
10
|
+
# @param [Hash] options リクエストのオプション
|
11
|
+
# @option options [String] :id 画像のID
|
12
|
+
# @option options [String] :domain 指定したドメインの画像一覧を取得する
|
13
|
+
# @option options [String] :tag 指定したタグの画像一覧を取得する
|
14
|
+
# @option options [Integer] :limit(10) 最大取得件数(1-100)
|
15
|
+
# @option options [Integer] :offset(0) 取得オフセット
|
10
16
|
def initialize options={}
|
11
17
|
super(options)
|
12
18
|
self.id = options[:id] if options.has_key?(:id)
|
@@ -1,21 +1,71 @@
|
|
1
1
|
module Mushikago
|
2
2
|
module Tombo
|
3
|
-
|
3
|
+
# Tomboサービスを利用する
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# client = Mushikago::Tombo::Client.new(:api_key => '<api_key>', :secret_key => '<secret_key>')
|
7
|
+
#
|
8
|
+
# client.capture('http://www.tombo.ne.jp/', :thumbnail => true, :tags => ['tombo', 'webservice'])
|
9
|
+
#
|
10
|
+
# captures = client.captures
|
11
|
+
# captures['images'].each do |image|
|
12
|
+
# puts image['image_url']
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @example APIキーをファイルから読み込む場合
|
16
|
+
# Mushikago.config.load(YAML.load(File.read(mushikago.yml)))
|
17
|
+
# client = Mushikago::Tombo::Client.new
|
18
|
+
class Client < Mushikago::Http::Client
|
19
|
+
# 指定したURLのキャプチャを取得する
|
20
|
+
#
|
21
|
+
# @param [String] url キャプチャ対象のURL
|
22
|
+
# @param [Hash] options リクエストのオプション
|
23
|
+
# @option options [String] :image_format('jpg') 画像のフォーマット(jpg,png)
|
24
|
+
# @option options [Integer] :image_quality(80) 画像の品質(0-100)
|
25
|
+
# @option options [Boolean] :thumbnail(0) サムネイル取得フラグ(false:取得しない,true:取得する)
|
26
|
+
# @option options [String,Array] :tags タグ
|
27
|
+
# @example
|
28
|
+
# client.capture('http://www.tombo.ne.jp/', :thumbnail => true, :tags => ['tombo', 'webservice'])
|
29
|
+
# @return [Mushikago::Http::Response] リクエストの結果
|
4
30
|
def capture url, options={}
|
5
31
|
request = CaptureRequest.new(url, options)
|
6
32
|
send_request(request)
|
7
33
|
end
|
8
34
|
|
35
|
+
# いままでキャプチャした画像の情報を取得する
|
36
|
+
#
|
37
|
+
# @param [Hash] options リクエストのオプション
|
38
|
+
# @option options [String] :id 画像のID
|
39
|
+
# @option options [String] :domain 指定したドメインの画像一覧を取得する
|
40
|
+
# @option options [String] :tag 指定したタグの画像一覧を取得する
|
41
|
+
# @option options [Integer] :limit(10) 最大取得件数(1-100)
|
42
|
+
# @option options [Integer] :offset(0) 取得オフセット
|
43
|
+
# @example
|
44
|
+
# client.captures(:tag => 'webservice')
|
45
|
+
# @return [Mushikago::Http::Response] リクエストの結果
|
9
46
|
def captures options={}
|
10
47
|
request = CapturesRequest.new(options)
|
11
48
|
send_request(request)
|
12
49
|
end
|
13
50
|
|
51
|
+
# 指定した画像を削除する
|
52
|
+
#
|
53
|
+
# @param [String] id 画像のID
|
54
|
+
# @param [Hash] options リクエストのオプション
|
55
|
+
# @example
|
56
|
+
# client.delete('5a6cdfa3-xxx3-47d6-8xxx-5f83af6b66cc')
|
57
|
+
# @return [Mushikago::Http::Response] リクエストの結果
|
14
58
|
def delete id, options={}
|
15
59
|
request = DeleteRequest.new(id, options)
|
16
60
|
send_request(request)
|
17
61
|
end
|
18
62
|
|
63
|
+
# APIの使用状況を取得する
|
64
|
+
#
|
65
|
+
# @param [Hash] options リクエストのオプション
|
66
|
+
# @example
|
67
|
+
# client.info
|
68
|
+
# @return [Mushikago::Http::Response] リクエストの結果
|
19
69
|
def info options={}
|
20
70
|
request = InfoRequest.new(options)
|
21
71
|
send_request(request)
|
data/lib/mushikago/tombo.rb
CHANGED
data/lib/mushikago/version.rb
CHANGED
data/lib/mushikago.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
+
# Mushikagoサービスを提供するモジュール
|
1
2
|
module Mushikago
|
2
3
|
# class
|
3
4
|
autoload :Config, 'mushikago/configuration'
|
4
|
-
autoload :Client, 'mushikago/client'
|
5
5
|
|
6
6
|
# module
|
7
7
|
autoload :Http, 'mushikago/http'
|
8
8
|
autoload :Auth, 'mushikago/auth'
|
9
9
|
autoload :Tombo, 'mushikago/tombo'
|
10
10
|
|
11
|
+
# @return [Mushikago::Config] config コンフィグのインスタンスへのアクセスを提供する
|
11
12
|
def self.config
|
12
13
|
Config.instance
|
13
14
|
end
|
@@ -32,7 +32,7 @@ describe Mushikago::Config do
|
|
32
32
|
# Mushikago::Configは値をロードすることができる
|
33
33
|
context 'loads option value' do
|
34
34
|
before :all do
|
35
|
-
Mushikago::Config.load(
|
35
|
+
Mushikago::Config.instance.load(
|
36
36
|
:api_key => 'api_key',
|
37
37
|
:secret_key => 'secret_key',
|
38
38
|
:tombo_endpoint => 'tombo.mushikago.org'
|
@@ -47,7 +47,7 @@ describe Mushikago::Config do
|
|
47
47
|
# ロードする際のキーは文字列でもOK
|
48
48
|
context 'loading options key can be string' do
|
49
49
|
before :all do
|
50
|
-
Mushikago::Config.load(
|
50
|
+
Mushikago::Config.instance.load(
|
51
51
|
'api_key' => 'api_key',
|
52
52
|
'secret_key' => 'secret_key',
|
53
53
|
'tombo_endpoint' => 'tombo.mushikago.org'
|
@@ -5,10 +5,10 @@ class TestRequest < Mushikago::Http::Request
|
|
5
5
|
end
|
6
6
|
|
7
7
|
|
8
|
-
describe Mushikago::Client do
|
8
|
+
describe Mushikago::Http::Client do
|
9
9
|
context 'construct without options' do
|
10
10
|
before :all do
|
11
|
-
@client = Mushikago::Client.new
|
11
|
+
@client = Mushikago::Http::Client.new
|
12
12
|
end
|
13
13
|
|
14
14
|
subject{ @client }
|
@@ -18,7 +18,7 @@ describe Mushikago::Client do
|
|
18
18
|
|
19
19
|
context 'construct with options' do
|
20
20
|
before :all do
|
21
|
-
@client = Mushikago::Client.new(
|
21
|
+
@client = Mushikago::Http::Client.new(
|
22
22
|
:api_key => 'mushikago api key'
|
23
23
|
)
|
24
24
|
end
|
@@ -33,7 +33,7 @@ describe Mushikago::Client do
|
|
33
33
|
request = TestRequest.new
|
34
34
|
request.host = 'tombo.mushikago.org'
|
35
35
|
request.path = '/1/info.json'
|
36
|
-
client = Mushikago::Client.new
|
36
|
+
client = Mushikago::Http::Client.new
|
37
37
|
@response = client.send_request(request)
|
38
38
|
end
|
39
39
|
subject{ @response }
|
@@ -17,10 +17,13 @@ describe Mushikago::Http::Response do
|
|
17
17
|
subject{ @response }
|
18
18
|
|
19
19
|
it{ should respond_to(:meta, :response) }
|
20
|
+
it{ should respond_to(:status, :message) }
|
20
21
|
it{ should respond_to(:[]) }
|
21
22
|
|
22
23
|
it{ subject.meta[:status].should == 200 }
|
24
|
+
it{ subject.status.should == 200 }
|
23
25
|
it{ subject.meta[:message].should == 'OK' }
|
26
|
+
it{ subject.message.should == 'OK' }
|
24
27
|
it{ subject.response[:hello].should == 'world' }
|
25
28
|
it{ subject[:hello].should == 'world' }
|
26
29
|
end
|
@@ -35,6 +35,7 @@ describe Mushikago::Tombo::CaptureRequest do
|
|
35
35
|
it{ subject.image_quality.should == '20' }
|
36
36
|
it{ subject.thumbnail.should == '1' }
|
37
37
|
it{ subject.tags.should == 'hoge,fuga' }
|
38
|
+
it{ subject.headers['Content-type'].should == 'application/x-www-form-urlencoded; charset=utf-8' }
|
38
39
|
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
40
|
end
|
40
41
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mushikago-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Toru Matsuoka
|
@@ -142,11 +142,10 @@ files:
|
|
142
142
|
- lib/mushikago/configuration.rb
|
143
143
|
- lib/mushikago/http/request.rb
|
144
144
|
- lib/mushikago/http/response.rb
|
145
|
-
- lib/mushikago/client.rb
|
145
|
+
- lib/mushikago/http/client.rb
|
146
146
|
- lib/mushikago/http.rb
|
147
147
|
- lib/mushikago-sdk.rb
|
148
148
|
- spec/spec_helper.rb
|
149
|
-
- spec/mushikago/client_spec.rb
|
150
149
|
- spec/mushikago/auth/signer_spec.rb
|
151
150
|
- spec/mushikago/auth/signature_spec.rb
|
152
151
|
- spec/mushikago/tombo/client_spec.rb
|
@@ -156,6 +155,7 @@ files:
|
|
156
155
|
- spec/mushikago/tombo/info_request_spec.rb
|
157
156
|
- spec/mushikago/tombo/capture_request_spec.rb
|
158
157
|
- spec/mushikago/configuration_spec.rb
|
158
|
+
- spec/mushikago/http/client_spec.rb
|
159
159
|
- spec/mushikago/http/response_spec.rb
|
160
160
|
- spec/mushikago/http/request_spec.rb
|
161
161
|
homepage: http://www.mushikago.org/
|
@@ -193,7 +193,6 @@ specification_version: 3
|
|
193
193
|
summary: A SDK for Mushikago.
|
194
194
|
test_files:
|
195
195
|
- spec/spec_helper.rb
|
196
|
-
- spec/mushikago/client_spec.rb
|
197
196
|
- spec/mushikago/auth/signer_spec.rb
|
198
197
|
- spec/mushikago/auth/signature_spec.rb
|
199
198
|
- spec/mushikago/tombo/client_spec.rb
|
@@ -203,5 +202,6 @@ test_files:
|
|
203
202
|
- spec/mushikago/tombo/info_request_spec.rb
|
204
203
|
- spec/mushikago/tombo/capture_request_spec.rb
|
205
204
|
- spec/mushikago/configuration_spec.rb
|
205
|
+
- spec/mushikago/http/client_spec.rb
|
206
206
|
- spec/mushikago/http/response_spec.rb
|
207
207
|
- spec/mushikago/http/request_spec.rb
|
data/lib/mushikago/client.rb
DELETED
@@ -1,35 +0,0 @@
|
|
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, request.port) 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
|