base_api 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2f6ba9e27bf46cf201d84629e1aecb05c88bf1d99a91239e17efa30eaf031573
4
+ data.tar.gz: '08bb79a098243ac94bf58c5078c9b13dbb4ad0215b18bfb427e7e00ec8b00411'
5
+ SHA512:
6
+ metadata.gz: fd214b36ca3748cbcffae1c724c94798bb8e9bee8b27238fd5f262199d373c6a01b71429961c9a6b1eecbdd672ee33726932a2585d5c03a379ac7e150b7b18c1
7
+ data.tar.gz: 1b6894d07fd8febae5a146566679d3a9c4ca87ec8e2ad958eff924cf604a6cca2c6ab368bbfd8d1d17d4358a47dde34757ab7e838ab19ffade9b3771aee9d2bb
@@ -0,0 +1,27 @@
1
+ name: Rspec
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby: [ '2.6', '2.7' ]
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - name: Set up Ruby
14
+ uses: actions/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ - name: Build and test with RSpec
18
+ env:
19
+ BASE_API_SHOP_URL: ${{ secrets.BASE_API_SHOP_URL }}
20
+ BASE_API_ACCESS_TOKEN: ${{ secrets.BASE_API_ACCESS_TOKEN }}
21
+ BASE_API_CLIENT_ID: ${{ secrets.BASE_API_CLIENT_ID }}
22
+ BASE_API_REDIRECT_URI: ${{ secrets.BASE_API_REDIRECT_URI }}
23
+ BASE_API_CLIENT_SECRET: ${{ secrets.BASE_API_CLIENT_SECRET }}
24
+ run: |
25
+ gem install bundler
26
+ bundle install --jobs 4 --retry 3
27
+ bundle exec rspec
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ env_vars
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yshunske@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in base_api.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'pry-byebug'
9
+
10
+ group :test do
11
+ gem 'vcr'
12
+ gem 'webmock'
13
+ end
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ base_api (0.1.0)
5
+ httparty (>= 0.17.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ byebug (11.0.1)
13
+ coderay (1.1.2)
14
+ crack (0.4.3)
15
+ safe_yaml (~> 1.0.0)
16
+ diff-lcs (1.3)
17
+ hashdiff (1.0.0)
18
+ httparty (0.17.3)
19
+ mime-types (~> 3.0)
20
+ multi_xml (>= 0.5.2)
21
+ method_source (0.9.2)
22
+ mime-types (3.3.1)
23
+ mime-types-data (~> 3.2015)
24
+ mime-types-data (3.2019.1009)
25
+ multi_xml (0.6.0)
26
+ pry (0.12.2)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ pry-byebug (3.7.0)
30
+ byebug (~> 11.0)
31
+ pry (~> 0.10)
32
+ public_suffix (4.0.3)
33
+ rake (12.3.3)
34
+ rspec (3.9.0)
35
+ rspec-core (~> 3.9.0)
36
+ rspec-expectations (~> 3.9.0)
37
+ rspec-mocks (~> 3.9.0)
38
+ rspec-core (3.9.1)
39
+ rspec-support (~> 3.9.1)
40
+ rspec-expectations (3.9.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-mocks (3.9.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-support (3.9.2)
47
+ safe_yaml (1.0.5)
48
+ vcr (5.0.0)
49
+ webmock (3.8.0)
50
+ addressable (>= 2.3.6)
51
+ crack (>= 0.3.2)
52
+ hashdiff (>= 0.4.0, < 2.0.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ base_api!
59
+ pry-byebug
60
+ rake (~> 12.0)
61
+ rspec (~> 3.0)
62
+ vcr
63
+ webmock
64
+
65
+ BUNDLED WITH
66
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 yamashun
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # BaseApi
2
+ ![](https://github.com/yamashun/base_api/workflows/Rspec/badge.svg)
3
+
4
+ A Ruby interface to the BASE API. (This library is unofficial.)
5
+
6
+ [BASE API doc](https://docs.thebase.in/docs/api/)
7
+
8
+ BASE is a Japanese service that allows you to easily open an online shop. Please check the [official page](https://thebase.in/) for details.
9
+
10
+ [日本語版のREADME](https://github.com/yamashun/base_api/blob/master/README_JP.md)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'base_api'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install base_api
27
+
28
+ ## Usage
29
+ ### Basic usage
30
+
31
+ ```ruby
32
+ # Initialize Client instance with bearer token issued with OAuth2.0
33
+ client = BaseApi::Client.new(access_token: 'bearer token')
34
+
35
+ # Fetch items
36
+ response = client.items
37
+ response['items']
38
+ ```
39
+
40
+ ### Default results per request
41
+ Default results from the BASE API are 20. If you wish to change per request results number, you can set in three ways:
42
+
43
+ ```bash
44
+ # Environment variable
45
+ export BASE_API_LIMIT=30
46
+ ```
47
+
48
+ or
49
+
50
+ ```ruby
51
+ # Arguments for initialzie a client instance
52
+ client = BaseApi::Client.new(access_token: 'bearer token', limit: 30)
53
+ ```
54
+
55
+ or
56
+
57
+ ```ruby
58
+ # Arguments for call api
59
+ client = BaseApi::Client.new(access_token: 'bearer token')
60
+ client.items({ limit: 30 })
61
+ ```
62
+
63
+ ### Pagination
64
+
65
+
66
+ ### Errors
67
+
68
+
69
+ ## Development
70
+ TODO
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/base_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/base_api/blob/master/CODE_OF_CONDUCT.md).
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the BaseApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/base_api/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,116 @@
1
+ # BaseApi
2
+ ![](https://github.com/yamashun/base_api/workflows/Rspec/badge.svg)
3
+
4
+ BASE APIのためのクライアントを提供するRuby gemです。(非公式です。)
5
+ [BASE API doc](https://docs.thebase.in/docs/api/)
6
+
7
+ BASEは簡単にネットショップを開設できる日本のサービスです。詳しくは公式ページ[公式ページ](https://thebase.in/)を確認してください。
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'base_api'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install base_api
24
+
25
+ ## Usage
26
+ ### Basic usage
27
+
28
+ ```ruby
29
+ # OAuth2.0で払い出されたBearerトークンを使ってクライアントのインスタンスを生成します。
30
+ client = BaseApi::Client.new(access_token: 'bearer token')
31
+
32
+ # 商品の取得
33
+ response = client.items
34
+ response['items']
35
+ ```
36
+
37
+ ### リスト返却APIで取得する要素数
38
+ リストを返すBASE APIで一度に返却される要素の個数はデフォルトで20となっています。変更したい場合は以下のいずれかの方法で変更できます。
39
+
40
+ ```bash
41
+ # 環境変数に設定する
42
+ export BASE_API_LIMIT=30
43
+ ```
44
+
45
+ or
46
+
47
+ ```ruby
48
+ # クライアントを生成時に引数に渡す
49
+ client = BaseApi::Client.new(access_token: 'bearer token', limit: 30)
50
+ ```
51
+
52
+ or
53
+
54
+ ```ruby
55
+ # API呼び出し時に引数を渡す
56
+ client = BaseApi::Client.new(access_token: 'bearer token')
57
+ client.items({ limit: 30 })
58
+ ```
59
+
60
+ ### ページネーション
61
+ ページネーション用に `fetch_next_page` を用意しています。
62
+
63
+ ```ruby
64
+ client = BaseApi::Client.new(access_token: 'bearer token')
65
+ # 1番目から20番目の要素を取得
66
+ client.items
67
+
68
+ # 21番目から40番目の要素を取得
69
+ client.fetch_next_page
70
+ ```
71
+
72
+ ### コールバック
73
+ ブロックを渡すことでAPIの結果に応じた処理が可能です。ブロックはレスポンス結果とエラーオブジェクトを引数にとります。
74
+
75
+ ```ruby
76
+ client.items do |res, err|
77
+ if err
78
+ # エラーに応じた処理
79
+ else
80
+ # レスポンスに応じた処理
81
+ end
82
+ end
83
+ ```
84
+
85
+ ## 未対応API
86
+ 現在以下のAPIは未対応
87
+ - [GET /1/oauth/authorize](https://docs.thebase.in/docs/api/oauth/authorize)
88
+ - [POST /1/orders/edit_status](https://docs.thebase.in/docs/api/orders/edit_status)
89
+ - [GET /1/savings](https://docs.thebase.in/docs/api/savings/)
90
+ - [GET /1/delivery_companies](https://docs.thebase.in/docs/api/delivery_companies/)
91
+ - [GET /1/search](https://docs.thebase.in/docs/api/search/)
92
+ - [GET /1/search/refresh](https://docs.thebase.in/docs/api/search/refresh)
93
+
94
+
95
+ ## TODO
96
+ - 未対応APIの対応
97
+ - Railsアプリ用の認可コード取得のためのMVCテンプレートとジェネレータ
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/base_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/base_api/blob/master/CODE_OF_CONDUCT.md).
108
+
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
113
+
114
+ ## Code of Conduct
115
+
116
+ Everyone interacting in the BaseApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/base_api/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/base_api/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "base_api"
5
+ spec.version = BaseApi::VERSION
6
+ spec.authors = ["yamashun"]
7
+ spec.email = ["yshunske@gmail.com"]
8
+
9
+ spec.summary = %q{A Ruby interface to the BASE API. (This library is unofficial.)}
10
+ spec.description = %q{BASE is a Japanese service that allows you to easily open an online shop. Please check the official page(https://thebase.in/) for details}
11
+ spec.homepage = "https://github.com/yamashun/base_api"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/yamashun/base_api"
19
+ spec.metadata["changelog_uri"] = "https://github.com/yamashun/base_api"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.add_dependency 'httparty', '>= 0.17.3'
30
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "base_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require "base_api/version"
2
+ require "base_api/client"
3
+ require "base_api/configurable"
4
+ require "base_api/default"
5
+
6
+ module BaseApi
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ class << self
11
+ include BaseApi::Configurable
12
+
13
+ def client
14
+ @client = BaseApi::Client.new(options)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,128 @@
1
+ require 'base_api/configurable'
2
+ require 'httparty'
3
+ require 'base_api/error'
4
+ require 'base_api/client/authorizations'
5
+ require 'base_api/client/users'
6
+ require 'base_api/client/items'
7
+ require 'base_api/client/categories'
8
+ require 'base_api/client/item_categories'
9
+ require 'base_api/client/orders'
10
+
11
+ module BaseApi
12
+ # TODO: 責務でクラス分ける
13
+ class Client
14
+ include HTTParty
15
+ include BaseApi::Configurable
16
+ include BaseApi::Client::Authorizations
17
+ include BaseApi::Client::Users
18
+ include BaseApi::Client::Items
19
+ include BaseApi::Client::Categories
20
+ include BaseApi::Client::ItemCategories
21
+ include BaseApi::Client::Orders
22
+
23
+ base_uri 'https://api.thebase.in'
24
+
25
+ attr_accessor :client_id, :client_secret, :code, :access_token, :refresh_token,
26
+ :offset, :response
27
+
28
+ def initialize(options = {})
29
+ BaseApi::Configurable.keys.each do |key|
30
+ value = options.key?(key) ? options[key] : BaseApi.instance_variable_get(:"@#{key}")
31
+ instance_variable_set(:"@#{key}", value)
32
+ end
33
+
34
+ yield(self) if block_given?
35
+ end
36
+
37
+ def inspect
38
+ inspected = super
39
+
40
+ inspected.gsub! @code, '*******' if @code
41
+ inspected.gsub! @access_token, "#{'*'*28}#{@access_token[28..-1]}" if @access_token
42
+ inspected.gsub! @refresh_token, "#{'*'*28}#{@refresh_token[28..-1]}" if @refresh_token
43
+ inspected.gsub! @client_secret, "#{'*'*28}#{@client_secret[28..-1]}" if @client_secret
44
+
45
+ inspected
46
+ end
47
+
48
+ def fetch_next_page(&callback)
49
+ paginate(@last_page_args[:path], next_page_payload, &callback)
50
+ end
51
+
52
+ def reset_response
53
+ @response = nil
54
+ @last_page_args = nil
55
+ end
56
+
57
+ private
58
+
59
+ def call_post_api(path, payload = {}, &callback)
60
+ @response = self.class.post(path, { body: payload, headers: authorization_header })
61
+ handle_response(&callback)
62
+ end
63
+
64
+ def call_get_api(path, payload = {}, &callback)
65
+ @response = self.class.get(path, { query: payload, headers: authorization_header })
66
+ handle_response(&callback)
67
+ end
68
+
69
+ def handle_response(&callback)
70
+ check_status
71
+ success(response, &callback)
72
+ rescue => e
73
+ error(e, &callback)
74
+ end
75
+
76
+ def authorization_header
77
+ { Authorization: "Bearer #{access_token}" }
78
+ end
79
+
80
+ def paginate(path, payload = {}, &callback)
81
+ if different_path_request_called?(path)
82
+ reset_response
83
+ end
84
+
85
+ if payload[:offset].nil?
86
+ payload.merge!(offset: @offset)
87
+ end
88
+ if payload[:limit].nil?
89
+ payload.merge!(limit: @limit)
90
+ end
91
+
92
+ @last_page_args = { path: path, payload: payload }
93
+
94
+ call_get_api(path, payload, &callback)
95
+ end
96
+
97
+ def next_page_payload
98
+ next_offset = @last_page_args[:payload][:offset] + @last_page_args[:payload][:limit]
99
+ @last_page_args[:payload].merge(offset: next_offset)
100
+ end
101
+
102
+ def different_path_request_called?(path)
103
+ @last_page_args.is_a?(Hash) && @last_page_args[:path] != path
104
+ end
105
+
106
+ def check_status
107
+ if response.client_error?
108
+ raise BaseApi::ClientError.new(response)
109
+ elsif response.server_error?
110
+ raise BaseApi::ServerError.new(response)
111
+ end
112
+ end
113
+
114
+ def success(response, &block)
115
+ block.call(response, nil) if block_given?
116
+ response
117
+ end
118
+
119
+ def error(error, &block)
120
+ if block_given?
121
+ block.call(response, error)
122
+ response
123
+ else
124
+ raise error
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,47 @@
1
+ module BaseApi
2
+ class Client
3
+ module Authorizations
4
+ def oauth_access_token(&block)
5
+ call_post_api('/1/oauth/token', access_token_payload, &block)
6
+ if response.success?
7
+ @access_token = response['access_token']
8
+ @refresh_token = response['refresh_token']
9
+ end
10
+
11
+ response
12
+ end
13
+
14
+ def oauth_refresh_token(&block)
15
+ call_post_api('/1/oauth/token', refresh_token_payload, &block)
16
+ if response.success?
17
+ @access_token = response['access_token']
18
+ @refresh_token = response['refresh_token']
19
+ end
20
+
21
+ response
22
+ end
23
+
24
+ private
25
+
26
+ def access_token_payload
27
+ {
28
+ grant_type: 'authorization_code',
29
+ client_id: client_id,
30
+ client_secret: client_secret,
31
+ code: code,
32
+ redirect_uri: redirect_uri
33
+ }
34
+ end
35
+
36
+ def refresh_token_payload
37
+ {
38
+ grant_type: 'refresh_token',
39
+ client_id: client_id,
40
+ client_secret: client_secret,
41
+ refresh_token: refresh_token,
42
+ redirect_uri: redirect_uri
43
+ }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ module BaseApi
2
+ class Client
3
+ module Categories
4
+ # @return [HTTParty::Response]
5
+ # @see https://docs.thebase.in/docs/api/categories/
6
+ def categories(&block)
7
+ call_get_api('/1/categories', &block)
8
+ end
9
+
10
+ # @param [String] name
11
+ # @option param [Hash] option
12
+ # @return [HTTParty::Response]
13
+ # @see https://docs.thebase.in/docs/api/categories/add
14
+ def categories_add(name, option = {}, &block)
15
+ call_post_api('/1/categories/add', { name: name }.merge(option), &block)
16
+ end
17
+
18
+ # @param [Integer|String] category_id
19
+ # @option param [Hash] option
20
+ # @return [HTTParty::Response]
21
+ # @see https://docs.thebase.in/docs/api/categories/edit
22
+ def categories_edit(category_id, option = {}, &block)
23
+ call_post_api('/1/categories/edit', { category_id: category_id }.merge(option), &block)
24
+ end
25
+
26
+ # @param [Integer|String] category_id
27
+ # @return [HTTParty::Response]
28
+ # @see https://docs.thebase.in/docs/api/categories/delete
29
+ def categories_delete(category_id, &block)
30
+ call_post_api('/1/categories/delete', { category_id: category_id }, &block)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ module BaseApi
2
+ class Client
3
+ module ItemCategories
4
+ # @param [Integer|String] item_id
5
+ # @return [HTTParty::Response]
6
+ # @see https://docs.thebase.in/docs/api/item_categories/detail
7
+ def item_categories_detail(item_id, &block)
8
+ call_get_api("/1/item_categories/detail/#{item_id}", &block)
9
+ end
10
+
11
+ # @param [Integer|String] item_id
12
+ # @param [Integer|String] category_id
13
+ # @return [HTTParty::Response]
14
+ # @see https://docs.thebase.in/docs/api/item_categories/add
15
+ def item_categories_add(item_id, category_id, &block)
16
+ call_post_api('/1/item_categories/add', { item_id: item_id, category_id: category_id }, &block)
17
+ end
18
+
19
+ # @param [Integer|String] item_category_id
20
+ # @return [HTTParty::Response]
21
+ # @see https://docs.thebase.in/docs/api/item_categories/delete
22
+ def item_categories_delete(item_category_id, &block)
23
+ call_post_api('/1/item_categories/delete', { item_category_id: item_category_id }, &block)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,78 @@
1
+ module BaseApi
2
+ class Client
3
+ module Items
4
+ # @option params [Hash] option
5
+ # @return [HTTParty::Response]
6
+ # @see https://docs.thebase.in/docs/api/items/
7
+ def items(option = {}, &block)
8
+ paginate('/1/items', option, &block)
9
+ end
10
+
11
+ # @param [String] q: search word
12
+ # @option params [Hash] option
13
+ # @return [HTTParty::Response]
14
+ # @see https://docs.thebase.in/docs/api/items/search
15
+ def items_search(q, option = {}, &block)
16
+ paginate('/1/items/search', { q: q }.merge(option), &block)
17
+ end
18
+
19
+ # @param [Integer | String] item_id
20
+ # @return [HTTParty::Response]
21
+ # @see https://docs.thebase.in/docs/api/items/detail
22
+ def items_detail(item_id, &block)
23
+ call_get_api("/1/items/detail/#{item_id}", &block)
24
+ end
25
+
26
+ # @param [Hash] item
27
+ # @return [HTTParty::Response]
28
+ # @see https://docs.thebase.in/docs/api/items/add
29
+ def items_add(item, &block)
30
+ call_post_api('/1/items/add', item, &block)
31
+ end
32
+
33
+ # @param [Hash] item
34
+ # @return [HTTParty::Response]
35
+ # @see https://docs.thebase.in/docs/api/items/edit
36
+ def items_edit(item, &block)
37
+ call_post_api('/1/items/edit', item, &block)
38
+ end
39
+
40
+ # @param [Integer|String] item_id
41
+ # @return [HTTParty::Response]
42
+ # @see https://docs.thebase.in/docs/api/items/delete
43
+ def items_delete(item_id, &block)
44
+ call_post_api('/1/items/delete', { item_id: item_id }, &block)
45
+ end
46
+
47
+ # @param [Hash] item_image
48
+ # @return [HTTParty::Response]
49
+ # @see https://docs.thebase.in/docs/api/items/add_image
50
+ def items_add_image(item_image, &block)
51
+ call_post_api('/1/items/add_image', item_image, &block)
52
+ end
53
+
54
+ # @param [Integer|String] item_id
55
+ # @param [Integer|String] image_no: from 1 to 20
56
+ # @return [HTTParty::Response]
57
+ # @see https://docs.thebase.in/docs/api/items/delete_image
58
+ def items_delete_image(item_id, image_no, &block)
59
+ call_post_api('/1/items/delete_image', { item_id: item_id, image_no: image_no }, &block)
60
+ end
61
+
62
+ # @param [Hash] params
63
+ # @return [HTTParty::Response]
64
+ # @see https://docs.thebase.in/docs/api/items/edit_stock
65
+ def items_edit_stock(params, &block)
66
+ call_post_api('/1/items/edit_stock', params, &block)
67
+ end
68
+
69
+ # @param [Integer|String] item_id
70
+ # @param [Integer|String] variation_id
71
+ # @return [HTTParty::Response]
72
+ # @see https://docs.thebase.in/docs/api/items/delete_variation
73
+ def items_delete_variation(item_id, variation_id, &block)
74
+ call_post_api('/1/items/delete_variation', { item_id: item_id, variation_id: variation_id }, &block)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,24 @@
1
+ module BaseApi
2
+ class Client
3
+ module Orders
4
+ # Get orders
5
+ #
6
+ # @option params [Hash] option
7
+ # ex) { limit: 20, offset: 50 }
8
+ # @return [HTTParty::Response]
9
+ # @see https://docs.thebase.in/docs/api/orders/
10
+ def orders(option = {}, &block)
11
+ paginate('/1/orders', option, &block)
12
+ end
13
+
14
+ # Get a single order
15
+ #
16
+ # @param [String] unique_key
17
+ # @return [HTTParty::Response]
18
+ # @see https://docs.thebase.in/docs/api/orders/detail
19
+ def orders_detail(unique_key, &block)
20
+ call_get_api("/1/orders/detail/#{unique_key}", {}, &block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module BaseApi
2
+ class Client
3
+ module Users
4
+ def users_me(&block)
5
+ call_get_api('/1/users/me', {}, &block)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module BaseApi
2
+ module Configurable
3
+ attr_accessor :client_id, :client_secret, :code, :access_token, :refresh_token,
4
+ :limit, :offset, :redirect_uri
5
+
6
+ class << self
7
+ def keys
8
+ @keys ||= [
9
+ :client_id,
10
+ :client_secret,
11
+ :code,
12
+ :access_token,
13
+ :refresh_token,
14
+ :limit,
15
+ :offset,
16
+ :redirect_uri
17
+ ]
18
+ end
19
+ end
20
+
21
+ def configure
22
+ yield self
23
+ end
24
+
25
+ # Reset configuration options to default values
26
+ def reset!
27
+ BaseApi::Configurable.keys.each do |key|
28
+ instance_variable_set(:"@#{key}", BaseApi::Default.options[key])
29
+ end
30
+ self
31
+ end
32
+
33
+ private
34
+
35
+ def options
36
+ Hash[BaseApi::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
1
+ require 'base_api/configurable'
2
+
3
+ module BaseApi
4
+ class Default
5
+ class << self
6
+ # Configuration options
7
+ # @return [Hash]
8
+ def options
9
+ Hash[BaseApi::Configurable.keys.map{|key| [key, send(key)]}]
10
+ end
11
+
12
+ def client_id
13
+ ENV['BASE_API_CLIENT_ID']
14
+ end
15
+
16
+ def client_secret
17
+ ENV['BASE_API_CLIENT_SECRET']
18
+ end
19
+
20
+ def code
21
+ ENV['BASE_API_CODE']
22
+ end
23
+
24
+ def access_token
25
+ ENV['BASE_API_ACCESS_TOKEN']
26
+ end
27
+
28
+ def refresh_token
29
+ ENV['BASE_API_REFRESH_TOKEN']
30
+ end
31
+
32
+ def limit
33
+ ENV.fetch("BASE_API_LIMIT") { 20 }
34
+ end
35
+
36
+ def offset
37
+ ENV.fetch("BASE_API_OFFSET") { 0 }
38
+ end
39
+
40
+ def redirect_uri
41
+ ENV['BASE_API_REDIRECT_URI']
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module BaseApi
2
+ class Error < StandardError
3
+ def initialize(response)
4
+ @response = response
5
+ super(build_error_message)
6
+ end
7
+
8
+ def response_status
9
+ @response.code
10
+ end
11
+
12
+ def response_headers
13
+ @response.headers
14
+ end
15
+
16
+ def response_body
17
+ @response.parsed_response
18
+ end
19
+
20
+ private
21
+
22
+ def build_error_message
23
+ message = "#{response_status}: "
24
+ message << "#{@response['error']} " if @response['error']
25
+ message << "#{@response['error_description']}" if @response['error_description']
26
+ message << " uri: #{@response.request.uri}"
27
+ message
28
+ end
29
+ end
30
+
31
+ class ClientError < Error; end
32
+
33
+ class ServerError < Error; end
34
+ end
@@ -0,0 +1,3 @@
1
+ module BaseApi
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yamashun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.17.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.17.3
27
+ description: BASE is a Japanese service that allows you to easily open an online shop.
28
+ Please check the official page(https://thebase.in/) for details
29
+ email:
30
+ - yshunske@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".github/workflows/ruby.yml"
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".travis.yml"
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - README_JP.md
45
+ - Rakefile
46
+ - base_api.gemspec
47
+ - bin/console
48
+ - bin/setup
49
+ - lib/base_api.rb
50
+ - lib/base_api/client.rb
51
+ - lib/base_api/client/authorizations.rb
52
+ - lib/base_api/client/categories.rb
53
+ - lib/base_api/client/item_categories.rb
54
+ - lib/base_api/client/items.rb
55
+ - lib/base_api/client/orders.rb
56
+ - lib/base_api/client/users.rb
57
+ - lib/base_api/configurable.rb
58
+ - lib/base_api/default.rb
59
+ - lib/base_api/error.rb
60
+ - lib/base_api/version.rb
61
+ homepage: https://github.com/yamashun/base_api
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://github.com/yamashun/base_api
66
+ source_code_uri: https://github.com/yamashun/base_api
67
+ changelog_uri: https://github.com/yamashun/base_api
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A Ruby interface to the BASE API. (This library is unofficial.)
87
+ test_files: []