aliyun_open_search 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1af1bfa1c15f6ad116e77ae04f9bbc7bf1553d2
4
+ data.tar.gz: 16e720c3f586cf6d9d8e12e1baca48f08cf2aebc
5
+ SHA512:
6
+ metadata.gz: 7d0561e8d82215f6f4c6a52b3c07d9ece97662d932e964ff4352df8a88bf925a40b4a0757d4f024358849fccdba6ed0067d041972c693df246539f46866e4841
7
+ data.tar.gz: efd1e943353afabdbff8a35286a98329c195115de5701f5f6f232df3acc6467b7914787896f89821ac89f6b0cfc31e88f5fc62af4e37cac5fcf261db5cda6aab
data/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # Ignore application configuration
12
+ /config/application.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ Style/StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+
4
+ Style/StringLiteralsInInterpolation:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/AsciiComments:
8
+ Enabled: false
9
+
10
+ Style/ClassAndModuleChildren:
11
+ Enabled: false
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Metrics/MethodLength:
17
+ Max: 30
18
+
19
+ Metrics/LineLength:
20
+ Max: 90
21
+
22
+ Metrics/AbcSize:
23
+ Max: 30
24
+
25
+ Metrics/ClassLength:
26
+ Max: 200
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in aliyun_open_search.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 lcp
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.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # AliyunOpenSearch
2
+
3
+ 对Aliyun Open Search API 进行封装.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'aliyun_open_search', github: "LcpMarvel/aliyun_open_search", require: false
11
+
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install aliyun_open_search
21
+
22
+ ## Usage
23
+
24
+ ### 准备工作
25
+ #### 定义 3 个环境变量
26
+
27
+ ```ruby
28
+
29
+ ACCESS_KEY_ID: "your access_key_id"
30
+ ACCESS_KEY_SECRET: "your access_key_secret"
31
+
32
+ OPEN_SEARCH_HOST: "http://opensearch-cn-hangzhou.aliyuncs.com"
33
+
34
+ ```
35
+
36
+ ### 数据处理API
37
+
38
+ ```
39
+ # app_name 是阿里云 应用名称
40
+ # params 的内容按照阿里云文档中的约定组织
41
+ require "aliyun_open_search"
42
+
43
+ params = {
44
+ action: :push,
45
+ table_name: :cars,
46
+ items: [
47
+ {
48
+ "cmd": "update",
49
+ "timestamp": 1_420_070_400_010,
50
+ "fields": {
51
+ "id": "121139313135",
52
+ "styl_name": "aodi",
53
+ "acquirer_id": "1"
54
+ }
55
+ },
56
+ {
57
+ "cmd": "update",
58
+ "timestamp": 1_420_070_400_020,
59
+ "fields": {
60
+ "id": "1211391233136",
61
+ "styl_name": "baoma",
62
+ "acquirer_id": "1"
63
+ }
64
+ }
65
+ ]
66
+ }
67
+
68
+ response = AliyunOpenSearch::Syncs.new(app_name).execute(params)
69
+ response_body = JSON.load(response.body)
70
+
71
+ ```
72
+
73
+ ### 搜索
74
+
75
+ ```ruby
76
+ require "aliyun_open_search"
77
+
78
+ # index_names 是阿里云 应用名称, 可以多个
79
+ # params 的内容按照阿里云文档中的约定组织
80
+
81
+ params = {
82
+ query: "config=start:0,hit:10,format=fulljson&&query=default:'的'&&filter=create_timestamp>1423000000&&sort=+type;-RANK&fetch_fields=id;title;body;url;type;create_timestamp",
83
+ first_formula_name: "first_bbs",
84
+ formula: "second_bbs",
85
+ summary: "summary_snipped:1,summary_field:title,summary_element:high,summary_len:32,summary_ellipsis:...;summary_snipped:2,summary_field:body,summary_element:high,summary_len:60,summary_ellipsis:..."
86
+ }
87
+
88
+ AliyunOpenSearch::Search.new("test", "test2", "test3").execute(params)
89
+
90
+ ```
91
+
92
+
93
+ ## Development
94
+
95
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
+
97
+ 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).
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aliyun_open_search. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
102
+
103
+
104
+ ## License
105
+
106
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
107
+
data/Rakefile ADDED
@@ -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,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "aliyun_open_search/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aliyun_open_search"
8
+ spec.version = AliyunOpenSearch::VERSION
9
+ spec.authors = ["lcp"]
10
+ spec.email = ["lcpmarvel@gmail.com"]
11
+
12
+ spec.summary = "Aliyun OpenSearch"
13
+ spec.description = "Aliyun OpenSearch Syncs and Search"
14
+ spec.homepage = "https://github.com/LcpMarvel/aliyun_open_search"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ fail "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "figaro"
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aliyun_open_search"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ ACCESS_KEY_ID: "your access_key_id"
2
+ ACCESS_KEY_SECRET: "your access_key_secret"
3
+
4
+ OPEN_SEARCH_HOST: http://opensearch-cn-hangzhou.aliyuncs.com
@@ -0,0 +1,7 @@
1
+ require "aliyun_open_search/version"
2
+ require "aliyun_open_search/base"
3
+ require "aliyun_open_search/syncs"
4
+ require "aliyun_open_search/search"
5
+
6
+ module AliyunOpenSearch
7
+ end
@@ -0,0 +1,71 @@
1
+ require "base64"
2
+ require "json"
3
+
4
+ module AliyunOpenSearch
5
+ class Base
6
+ attr_reader :basic_params, :base_url
7
+
8
+ def initialize
9
+ @basic_params = {
10
+ "Version" => "v2",
11
+ "AccessKeyId" => ENV.fetch("ACCESS_KEY_ID"),
12
+ "SignatureMethod" => "HMAC-SHA1",
13
+ "Timestamp" => Time.now.utc.iso8601,
14
+ "SignatureVersion" => "1.0",
15
+ "SignatureNonce" => signature_nonce
16
+ }
17
+ end
18
+
19
+ def uri(special_base_url = nil, params)
20
+ encoded_params = params.inject([]) do |arr, (k, v)|
21
+ arr << "#{k}=#{CGI.escape(v)}"
22
+ end.join("&")
23
+
24
+ URI((special_base_url || base_url) + "?" + encoded_params)
25
+ end
26
+
27
+ def self.request_method
28
+ "GET"
29
+ end
30
+
31
+ def self.signature(params)
32
+ params = params.sort_by { |k, _v| k.to_s }
33
+ .map do |arr|
34
+ str = if arr[1].is_a?(String) || arr[1].is_a?(Fixnum)
35
+ arr[1].to_s
36
+ else
37
+ arr[1].to_json
38
+ end
39
+
40
+ "#{arr[0]}=#{CGI.escape(str)}"
41
+ end.join("&")
42
+
43
+ Base64.encode64(
44
+ OpenSSL::HMAC.digest(
45
+ OpenSSL::Digest.new("sha1"),
46
+ "#{ENV["ACCESS_KEY_SECRET"]}&",
47
+ request_method + "&" + CGI.escape("/") + "&" + CGI.escape(params)
48
+ )
49
+ ).strip
50
+ end
51
+
52
+ private
53
+
54
+ def signature_nonce
55
+ # 用户在不同请求间要使用不同的随机数值,建议使用13位毫秒时间戳+4位随机数
56
+ (Time.now.to_f.round(3) * 1000).to_i.to_s + (1000..9999).to_a.sample.to_s
57
+ end
58
+
59
+ def self.format_params(params)
60
+ {}.tap do |hash|
61
+ params.map do |key, value|
62
+ hash[key.to_s] = if value.is_a?(Array)
63
+ JSON.generate(value)
64
+ else
65
+ value.to_s
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ module AliyunOpenSearch
2
+ class Search < Base
3
+ attr_reader :index_names
4
+
5
+ def initialize(*index_names)
6
+ super()
7
+ @index_names = index_names.join(";")
8
+ @base_url = "#{ENV["OPEN_SEARCH_HOST"]}/search"
9
+ end
10
+
11
+ def execute(custom_params)
12
+ params = basic_params.merge(
13
+ self.class.format_params(custom_params.merge("index_name" => index_names))
14
+ )
15
+
16
+ Net::HTTP.get(
17
+ uri(params.merge("Signature" => self.class.signature(params)))
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module AliyunOpenSearch
2
+ class Syncs < Base
3
+ def initialize(app_name)
4
+ super()
5
+
6
+ @base_url = "#{ENV["OPEN_SEARCH_HOST"]}/index/doc/#{app_name}"
7
+ end
8
+
9
+ def self.request_method
10
+ "POST"
11
+ end
12
+
13
+ def execute(custom_params)
14
+ formatted_custom_params = {}.tap do |hash|
15
+ custom_params.map do |key, value|
16
+ hash[key.to_s] = if value.is_a?(Array)
17
+ JSON.generate(value)
18
+ else
19
+ value.to_s
20
+ end
21
+ end
22
+ end
23
+
24
+ params_with_signature = basic_params.merge(
25
+ "Signature" => self.class
26
+ .signature(basic_params.merge(formatted_custom_params))
27
+ )
28
+
29
+ Net::HTTP.post_form(uri(params_with_signature), formatted_custom_params)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module AliyunOpenSearch
2
+ VERSION = "0.2.5"
3
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyun_open_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
+ platform: ruby
6
+ authors:
7
+ - lcp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: figaro
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Aliyun OpenSearch Syncs and Search
70
+ email:
71
+ - lcpmarvel@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".editorconfig"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - aliyun_open_search.gemspec
87
+ - bin/console
88
+ - bin/setup
89
+ - config/application.example.yml
90
+ - lib/aliyun_open_search.rb
91
+ - lib/aliyun_open_search/base.rb
92
+ - lib/aliyun_open_search/search.rb
93
+ - lib/aliyun_open_search/syncs.rb
94
+ - lib/aliyun_open_search/version.rb
95
+ homepage: https://github.com/LcpMarvel/aliyun_open_search
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Aliyun OpenSearch
120
+ test_files: []