haruna 0.0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p392
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in haruna.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 kakipo
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,54 @@
1
+ # Haruna
2
+
3
+ A Ruby interface to the 艦これ API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'haruna'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install haruna
18
+
19
+ ## Usage
20
+
21
+ client = Haruna::Client.new(token)
22
+
23
+ # to start a mission
24
+ client.req_mission_start(deck_id, mission_id)
25
+
26
+ # to obtain mission result
27
+ client.req_mission_result(deck_id)
28
+
29
+ # to refill fuel & bullets
30
+ client.req_hokyu_charge(kind, ship_id_arr)
31
+
32
+ ### Parameter
33
+
34
+ | Param | Description | Constraint |
35
+ | ------------- | -------------- | ---------- |
36
+ | token | Your API token | |
37
+ | ship_id | Ship's id | 1 ~ |
38
+ | deck_id | Deck's id | 1 ~ 4 |
39
+ | kind | Refill mode - 1: oil, 2: bullets, 3: oil + bullets | 1 ~ 3 |
40
+
41
+
42
+ ## Disclaimer
43
+
44
+ This software library ("gem") is provided by kakipo "as is" and "with all faults." kakipo makes no representations or warranties of any kind concerning the safety, suitability, inaccuracies, typographical errors, or other harmful components of this gem. There are inherent dangers in the use of any software, and you are solely responsible for determining whether this gem is compatible with your equipment and other software installed on your equipment. You are also solely responsible for the protection of your equipment and backup of your data, and kakipo will not be liable for any damages you may suffer in connection with using, modifying, or distributing this gem.
45
+
46
+ Plus, please read and respect [the official terms of use](http://www.dmm.co.jp/rule/=/category=onlinegame_service/).
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/haruna.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 'haruna/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "haruna"
8
+ spec.version = Haruna::VERSION
9
+ spec.authors = ["kakipo"]
10
+ spec.email = ["kakipo@gmail.com"]
11
+ spec.description = %q{Write a gem description}
12
+ spec.summary = %q{Write a gem summary}
13
+ spec.homepage = "https://github.com/kakipo/haruna"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency "faraday"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rake"
25
+ end
data/lib/haruna/api.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Haruna
2
+
3
+ class API
4
+ # config req header
5
+ API_VER = 1
6
+ END_POINT = 'http://125.6.189.215'
7
+ USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'
8
+ REFERER = 'http://125.6.189.215/kcs/port.swf?version=1.5.5'
9
+ ACCEPT_ENCODING = 'gzip,deflate,sdch'
10
+ ACCEPT_LANGUAGE = 'ja,en-US;q=0.8,en;q=0.6'
11
+ CONTENT_TYPE = 'application/x-www-form-urlencoded'
12
+
13
+ def initialize(token, proxy=nil)
14
+ require 'faraday'
15
+ @token = token
16
+
17
+ @conn = Faraday.new(:url => END_POINT, :proxy => proxy) do |faraday|
18
+ faraday.request :url_encoded
19
+ # faraday.response :logger
20
+ faraday.adapter Faraday.default_adapter
21
+ end
22
+ end
23
+
24
+ attr_writer :conn # for testing, not really need to rewrite
25
+
26
+ def call(action, target, param={})
27
+ @conn.post do |req|
28
+ req.url "/kcsapi/#{action}/#{target}"
29
+ req.headers['User-Agent'] = USER_AGENT
30
+ req.headers['Referer'] = REFERER
31
+ req.headers['Accept-Encoding'] = ACCEPT_ENCODING
32
+ req.headers['Accept-Language'] = ACCEPT_LANGUAGE
33
+ req.headers['Content-Type'] = CONTENT_TYPE
34
+ req.body = setup_param(param)
35
+ end
36
+ end
37
+
38
+ def setup_param(param)
39
+ param.merge(api_verno: API_VER, api_token: @token)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,55 @@
1
+ module Haruna
2
+
3
+ class Client < API
4
+
5
+ def initialize(token, proxy=nil)
6
+ super(token, proxy)
7
+ end
8
+
9
+ def req_mission_start(deck_id, mission_id)
10
+ validate_deck_id(deck_id)
11
+ validate_mission_id(mission_id)
12
+ call("api_req_mission", "start", api_deck_id: deck_id, api_mission_id: mission_id)
13
+ end
14
+
15
+ def req_mission_result(deck_id)
16
+ validate_deck_id(deck_id)
17
+ call("api_req_mission", "result", api_deck_id: deck_id)
18
+ end
19
+
20
+ def get_member_deck_port
21
+ call("api_get_member", "deck_port")
22
+ end
23
+
24
+ # kind
25
+ # 1: oil?
26
+ # 2: bullet?
27
+ # 3: oil + bullet?
28
+ def req_hokyu_charge(kind, ship_id_arr)
29
+ validate_supply_kind(kind)
30
+ validate_ship_ids(ship_id_arr)
31
+ call("api_req_hokyu", "charge", api_kind: kind, api_id_items: ship_id_arr.join(","))
32
+ end
33
+
34
+ # validators
35
+ def validate_deck_id(deck_id)
36
+ raise ArgumentError, "deck id must be between 1~4" unless deck_id.between?(1, 4)
37
+ end
38
+
39
+ def validate_mission_id(mission_id)
40
+ raise ArgumentError, "mission_id must be positive" unless 0 < mission_id
41
+ end
42
+
43
+ def validate_supply_kind(kind)
44
+ raise ArgumentError, "kind must be between 1~3" unless kind.between?(1, 3)
45
+ end
46
+
47
+ def validate_ship_ids(ship_id_arr)
48
+ raise ArgumentError, "at least 1 ship id is required" if ship_id_arr.empty?
49
+ raise ArgumentError, "at least 1 ship id is required" unless ship_id_arr.select{|id| id <= 0 }.empty?
50
+ end
51
+
52
+
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Haruna
2
+ VERSION = "0.0.1.2"
3
+ end
data/lib/haruna.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "haruna/version"
2
+ require "haruna/api"
3
+ require "haruna/client"
4
+
5
+ module Haruna
6
+
7
+ end
data/spec/api_spec.rb ADDED
@@ -0,0 +1,67 @@
1
+ # -*- encoding: UTF-8 -*-
2
+ require File.expand_path(File.join('./', 'spec_helper'), File.dirname(__FILE__))
3
+
4
+ describe Haruna::API do
5
+ describe "#new" do
6
+
7
+ context "when token is empty" do
8
+ it "should raise error" do
9
+ expect{ Haruna::API.new }.to raise_error(ArgumentError)
10
+ end
11
+ end
12
+
13
+ context "when token is NOT empty" do
14
+ subject { Haruna::API.new("abcdefg") }
15
+ it { should be_an_instance_of Haruna::API }
16
+ end
17
+ end
18
+
19
+ describe "#setup_param" do
20
+ before do
21
+ @token = "abcdefg"
22
+ @api = Haruna::API.new(@token)
23
+ end
24
+
25
+ subject { @api.setup_param(param) }
26
+
27
+ context "when passed param has NO elements" do
28
+ let(:param) { {} }
29
+ its(:size) { should eq 2 }
30
+ its([:api_verno]) { should eq 1 }
31
+ its([:api_token]) { should eq @token }
32
+ end
33
+
34
+ context "when passed param has AN element" do
35
+ let(:param) { {hoge: :foo} }
36
+ its(:size) { should eq 3 }
37
+ its([:hoge]) { should eq :foo }
38
+ its([:api_verno]) { should eq 1 }
39
+ its([:api_token]) { should eq @token }
40
+ end
41
+ end
42
+
43
+ describe "#call" do
44
+ before do
45
+ @api = Haruna::API.new("abcdefg")
46
+ @api.conn = create_stub_connection # overwrite faraday adapter
47
+ end
48
+
49
+ subject { @api.call("get_seafood", target, {}) }
50
+
51
+ context "when target is tamago" do
52
+ let(:target) { "tamago" }
53
+ its(:body) { should eq 'egg' }
54
+ end
55
+
56
+ context "when target is ebi" do
57
+ let(:target) { "ebi" }
58
+ its(:body) { should eq 'shrimp' }
59
+ end
60
+
61
+ context "when target is uni" do
62
+ let(:target) { "uni" }
63
+ its(:body) { should eq 'urchin' }
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,178 @@
1
+ # coding: utf-8
2
+ require File.expand_path(File.join('./', 'spec_helper'), File.dirname(__FILE__))
3
+
4
+ describe Haruna::Client do
5
+
6
+ describe "#new" do
7
+ context "when token is empty" do
8
+ it "should raise error" do
9
+ expect{ Haruna::Client.new }.to raise_error(ArgumentError)
10
+ end
11
+ end
12
+
13
+ context "when token is NOT empty" do
14
+ subject { Haruna::Client.new("abcdefg") }
15
+ it { should be_an_instance_of Haruna::Client }
16
+ end
17
+ end
18
+
19
+ describe "client commands" do
20
+ before do
21
+ @client = Haruna::Client.new("abcdefg")
22
+ @client.conn = create_stub_connection # overwrite faraday adapter
23
+ end
24
+
25
+ describe "#req_mission_start" do
26
+ context "w/o parameters" do
27
+ it "should raise an error" do
28
+ expect{ @client.req_mission_start }.to raise_error(ArgumentError)
29
+ end
30
+ end
31
+ context "w/ two parameters" do
32
+ subject { @client.req_mission_start(1, 1) }
33
+ its(:body) { should eq "ok" }
34
+ end
35
+ end
36
+
37
+ describe "#req_mission_result" do
38
+ context "w/o parameters" do
39
+ it "should raise an error" do
40
+ expect{ @client.req_mission_result }.to raise_error(ArgumentError)
41
+ end
42
+ end
43
+ context "w/ two parameters" do
44
+ subject { @client.req_mission_result(1) }
45
+ its(:body) { should eq "ok" }
46
+ end
47
+ end
48
+
49
+ describe "#req_hokyu_charge" do
50
+ context "w/o parameters" do
51
+ it "should raise an error" do
52
+ expect{ @client.req_hokyu_charge }.to raise_error(ArgumentError)
53
+ end
54
+ end
55
+ context "w/ two parameters" do
56
+ subject { @client.req_hokyu_charge(1, [1,2,3]) }
57
+ its(:body) { should eq "ok" }
58
+ end
59
+ end
60
+
61
+ describe "#get_member_deck_port" do
62
+ context "w/o parameters" do
63
+ subject { @client.get_member_deck_port }
64
+ its(:body) { should eq "ok" }
65
+ end
66
+ context "w/ a parameter" do
67
+ it "should raise an error" do
68
+ expect{ @client.get_member_deck_port(1) }.to raise_error(ArgumentError)
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ describe "validators" do
76
+ before do
77
+ @client = Haruna::Client.new("abcdefg")
78
+ end
79
+
80
+ describe "#validate_deck_id" do
81
+ subject{ @client.validate_deck_id(deck_id) }
82
+ context "when deck_id is TOO SMALL" do
83
+ let(:deck_id) { 0 }
84
+ it "should raise error" do
85
+ expect{ subject }.to raise_error(ArgumentError)
86
+ end
87
+ end
88
+ context "when deck_id is MIN" do
89
+ let(:deck_id) { 1 }
90
+ it "should not raise error" do
91
+ expect{ subject }.not_to raise_error
92
+ end
93
+ end
94
+ context "when deck_id is TOO BIG" do
95
+ let(:deck_id) { 5 }
96
+ it "should raise error" do
97
+ expect{ subject }.to raise_error(ArgumentError)
98
+ end
99
+ end
100
+ context "when deck_id is MAX" do
101
+ let(:deck_id) { 4 }
102
+ it "should not raise error" do
103
+ expect{ subject }.not_to raise_error
104
+ end
105
+ end
106
+ end
107
+
108
+ describe "#validate_mission_id" do
109
+ subject{ @client.validate_mission_id(mission_id) }
110
+ context "when mission_id is TOO SMALL" do
111
+ let(:mission_id) { 0 }
112
+ it "should raise error" do
113
+ expect{ subject }.to raise_error(ArgumentError)
114
+ end
115
+ end
116
+ context "when mission_id is MIN" do
117
+ let(:mission_id) { 1 }
118
+ it "should not raise error" do
119
+ expect{ subject }.not_to raise_error
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "#validate_kind" do
125
+ subject{ @client.validate_supply_kind(kind) }
126
+ context "when kind is TOO SMALL" do
127
+ let(:kind) { 0 }
128
+ it "should raise error" do
129
+ expect{ subject }.to raise_error(ArgumentError)
130
+ end
131
+ end
132
+ context "when kind is MIN" do
133
+ let(:kind) { 1 }
134
+ it "should not raise error" do
135
+ expect{ subject }.not_to raise_error
136
+ end
137
+ end
138
+ context "when kind is TOO BIG" do
139
+ let(:kind) { 4 }
140
+ it "should raise error" do
141
+ expect{ subject }.to raise_error(ArgumentError)
142
+ end
143
+ end
144
+ context "when kind is MAX" do
145
+ let(:kind) { 3 }
146
+ it "should not raise error" do
147
+ expect{ subject }.not_to raise_error
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "#validate_ship_ids" do
153
+ subject{ @client.validate_ship_ids(id_arr) }
154
+ context "when id_arr is empty" do
155
+ let(:id_arr) { [] }
156
+ it "should raise error" do
157
+ expect{ subject }.to raise_error(ArgumentError)
158
+ end
159
+ end
160
+ context "when id_arr is VALID" do
161
+ let(:id_arr) { [1, 2, 3] }
162
+ it "should not raise error" do
163
+ expect{ subject }.not_to raise_error
164
+ end
165
+ end
166
+ context "when id_arr contains INVALID id" do
167
+ let(:id_arr) { [1, 2, 0, 3] }
168
+ it "should raise error" do
169
+ expect{ subject }.to raise_error(ArgumentError)
170
+ end
171
+ end
172
+ end
173
+
174
+ end
175
+
176
+
177
+
178
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'haruna'
4
+
5
+ def create_stub_connection
6
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
7
+ # stub.post(url) { [status_code, response_header, response_body] }
8
+ stub.post('/kcsapi/get_seafood/tamago') {[ 200, {}, 'egg' ]}
9
+ stub.post('/kcsapi/get_seafood/ebi') {[ 200, {}, 'shrimp' ]}
10
+ stub.post('/kcsapi/get_seafood/uni') {[ 200, {}, 'urchin' ]}
11
+
12
+ stub.post('/kcsapi/api_req_mission/start') {[ 200, {}, 'ok' ]}
13
+ stub.post('/kcsapi/api_req_mission/result') {[ 200, {}, 'ok' ]}
14
+ stub.post('/kcsapi/api_req_hokyu/charge') {[ 200, {}, 'ok' ]}
15
+ stub.post('/kcsapi/api_get_member/deck_port') {[ 200, {}, 'ok' ]}
16
+
17
+ end
18
+
19
+ Faraday.new do |builder|
20
+ builder.adapter :test, stubs
21
+ end
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haruna
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kakipo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Write a gem description
79
+ email:
80
+ - kakipo@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .ruby-version
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - haruna.gemspec
93
+ - lib/haruna.rb
94
+ - lib/haruna/api.rb
95
+ - lib/haruna/client.rb
96
+ - lib/haruna/version.rb
97
+ - spec/api_spec.rb
98
+ - spec/client_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/kakipo/haruna
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Write a gem summary
125
+ test_files:
126
+ - spec/api_spec.rb
127
+ - spec/client_spec.rb
128
+ - spec/spec_helper.rb