userstack 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +28 -0
- data/.travis.yml +12 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/validate-target-ruby-version.rb +10 -0
- data/lib/userstack.rb +10 -0
- data/lib/userstack/client.rb +57 -0
- data/lib/userstack/uri_builder.rb +55 -0
- data/lib/userstack/version.rb +5 -0
- data/userstack.gemspec +40 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d3f64ad1781e50ef9bd02f20b4f6f439186f84aa50d85cd37881b52dfc834e89
|
4
|
+
data.tar.gz: 1c0c1cc717f625a03f9052e4efda3ffed6dd91482ea2e564e4eac1f1f56e70c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2dff6576f8ba8c57d6224408f253ea6dfd5d04a04e4974d8fc448fb2004eb2d67ce86ed773017f386d85706fda0f3bf5986c1ea8570f42f3fea1caf15681857
|
7
|
+
data.tar.gz: 260402a23be9b0b440ebc9adce20d50148c903f56b95f829c3261aaa0aaf89ee9d4c64e6a920c75e5a7540e14580a7693ac31d1f7797a9b1545b0eee63637618
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- Rakefile
|
4
|
+
- bin/*
|
5
|
+
- vendor/**/*
|
6
|
+
TargetRubyVersion: 2.3
|
7
|
+
|
8
|
+
Metrics/BlockLength:
|
9
|
+
Exclude:
|
10
|
+
- spec/userstack/**/*
|
11
|
+
- userstack.gemspec
|
12
|
+
|
13
|
+
Metrics/LineLength:
|
14
|
+
Max: 128
|
15
|
+
Exclude:
|
16
|
+
- spec/userstack/**/*
|
17
|
+
|
18
|
+
Style/FormatString:
|
19
|
+
EnforcedStyle: percent
|
20
|
+
|
21
|
+
Style/FormatStringToken:
|
22
|
+
EnforcedStyle: unannotated
|
23
|
+
|
24
|
+
Style/TrailingCommaInArrayLiteral:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/TrailingCommaInHashLiteral:
|
28
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 m-maeda-feedforce
|
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,88 @@
|
|
1
|
+
# Userstack
|
2
|
+
|
3
|
+
[![Travis Status](https://img.shields.io/travis/feedforce/userstack.svg?style=flat-square)][travisci]
|
4
|
+
[![License](https://img.shields.io/github/license/feedforce/userstack.svg?style=flat-square)][license]
|
5
|
+
|
6
|
+
[travisci]: https://travis-ci.org/feedforce/userstack
|
7
|
+
[license]: https://github.com/feedforce/userstack/blob/master/LICENSE.txt
|
8
|
+
|
9
|
+
This gem provides an access to [Userstack](https://userstack.com://userstack.com).
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'userstack'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install userstack
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
1. Instantiate an instance of {Userstack::Client} with a valid Access key.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
client = Userstack::Client.new(ACCESS_KEY)
|
33
|
+
```
|
34
|
+
|
35
|
+
2. Parse an useragent.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
result = client.parse('an useragent')
|
39
|
+
```
|
40
|
+
|
41
|
+
### Use http
|
42
|
+
|
43
|
+
If you are Free plan user, you have to use `http` scheme.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
client = Userstack::Client.new(ACCESS_KEY, use_ssl: false)
|
47
|
+
result = client.parse('an useragent')
|
48
|
+
```
|
49
|
+
|
50
|
+
### Legacy response
|
51
|
+
|
52
|
+
Userstack supports Useragent API response.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
client = Userstack::Client.new(ACCESS_KEY, legacy: true)
|
56
|
+
result = client.parse('an useragent')
|
57
|
+
```
|
58
|
+
|
59
|
+
example response
|
60
|
+
```json
|
61
|
+
{
|
62
|
+
"data": {
|
63
|
+
"ua_type": "Desktop",
|
64
|
+
"os_name": "macOS 10.14 Mojave",
|
65
|
+
"os_version": "",
|
66
|
+
"browser_name": "Chrome",
|
67
|
+
"browser_version": "71.0.3578.98",
|
68
|
+
"engine_name": "WebKit/Blink",
|
69
|
+
"engine_version": ""
|
70
|
+
}
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|
74
|
+
refs. https://userstack.com/documentation#enable_legacy_format
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
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.
|
79
|
+
|
80
|
+
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).
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/userstack.
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "userstack"
|
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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
# rubocop's TargetRubyVersion should be the oldest version in .travis.yml
|
6
|
+
|
7
|
+
target = YAML.load_file('.rubocop.yml')['AllCops']['TargetRubyVersion'].to_s
|
8
|
+
oldest = YAML.load_file('.travis.yml')['rvm'].min
|
9
|
+
|
10
|
+
exit 1 unless oldest.start_with?(target)
|
data/lib/userstack.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/https'
|
5
|
+
|
6
|
+
module Userstack
|
7
|
+
# A class which wraps calls to Userstack API
|
8
|
+
class Client
|
9
|
+
USER_AGENT = 'Userstack gem/%s' % VERSION
|
10
|
+
private_constant :USER_AGENT
|
11
|
+
|
12
|
+
# @param access_key [String] Userstack Access key
|
13
|
+
# @param use_ssl [Boolean] Use ssl or not
|
14
|
+
# @param legacy [Boolean] Legacy response
|
15
|
+
# @raise [ArgumentError] when `access_key` is invalid
|
16
|
+
# @see https://userstack.com/documentation
|
17
|
+
def initialize(access_key, use_ssl: true, legacy: false)
|
18
|
+
raise ArgumentError, 'Invalid Access key' if access_key.nil? || access_key.empty?
|
19
|
+
|
20
|
+
@access_key = access_key.freeze
|
21
|
+
@use_ssl = use_ssl
|
22
|
+
@legacy = legacy
|
23
|
+
freeze
|
24
|
+
end
|
25
|
+
|
26
|
+
# Parse an useragent using Userstack
|
27
|
+
#
|
28
|
+
# @param useragent [String] an useragent
|
29
|
+
# @return [Hash] a Hash generated by parsing the JSON returned
|
30
|
+
# from the API call, just `{}` on parsing failure
|
31
|
+
# @raise [ArgumentError] when `useragent` is invalid
|
32
|
+
def parse(useragent)
|
33
|
+
raise ArgumentError, 'Invalid useragent' if useragent.nil? || useragent.empty?
|
34
|
+
|
35
|
+
response = request(useragent)
|
36
|
+
parse_as_json(response.body)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
attr_reader :access_key, :use_ssl, :legacy
|
42
|
+
|
43
|
+
def request(useragent)
|
44
|
+
uri = UriBuilder.execute(access_key, useragent, use_ssl, legacy)
|
45
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
46
|
+
http.get(uri.to_s, 'User-Agent' => USER_AGENT)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def parse_as_json(json_text)
|
51
|
+
json_text ||= '{}'
|
52
|
+
JSON.parse(json_text)
|
53
|
+
rescue JSON::ParserError
|
54
|
+
{}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module Userstack
|
6
|
+
# A class which builds URI of Userstack api
|
7
|
+
class UriBuilder
|
8
|
+
USERSTACK_API_DOMAIN = 'api.userstack.com'
|
9
|
+
private_constant :USERSTACK_API_DOMAIN
|
10
|
+
|
11
|
+
private_class_method :new
|
12
|
+
|
13
|
+
# @param access_key [String] Userstack Access key
|
14
|
+
# @param useragent [String] useragent
|
15
|
+
# @param use_ssl [Boolean] Use ssl or not
|
16
|
+
# @param legacy [Boolean] Legacy response
|
17
|
+
# @return [URI::HTTP, URI::HTTPS]
|
18
|
+
def self.execute(access_key, useragent, use_ssl, legacy)
|
19
|
+
new(access_key, useragent, use_ssl, legacy).send(:execute)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :access_key, :useragent, :use_ssl, :legacy
|
25
|
+
|
26
|
+
def initialize(access_key, useragent, use_ssl, legacy)
|
27
|
+
@access_key = access_key.freeze
|
28
|
+
@useragent = useragent.freeze
|
29
|
+
@use_ssl = use_ssl
|
30
|
+
@legacy = legacy
|
31
|
+
freeze
|
32
|
+
end
|
33
|
+
|
34
|
+
def execute
|
35
|
+
fqdn = URI("#{scheme}://#{USERSTACK_API_DOMAIN}/")
|
36
|
+
fqdn.tap do |uri|
|
37
|
+
uri.path = '/detect'
|
38
|
+
uri.query = query
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def query
|
43
|
+
q = {
|
44
|
+
access_key: access_key,
|
45
|
+
ua: CGI.escape(useragent)
|
46
|
+
}
|
47
|
+
q[:legacy] = 1 if legacy
|
48
|
+
q.map { |k, v| "#{k}=#{v}" }.join('&')
|
49
|
+
end
|
50
|
+
|
51
|
+
def scheme
|
52
|
+
use_ssl ? 'https' : 'http'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/userstack.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'userstack/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'userstack'
|
9
|
+
spec.version = Userstack::VERSION
|
10
|
+
spec.authors = ['Masaki Maeda']
|
11
|
+
spec.email = ['m-maeda@feedforce.jp']
|
12
|
+
|
13
|
+
spec.summary = 'Ruby toolkit for working with the Userstack'
|
14
|
+
spec.description = 'Simple wrapper for the Userstack API https://userstack.com'
|
15
|
+
spec.homepage = 'https://github.com/feedforce/userstack'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/feedforce/userstack'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/feedforce/userstack/Chengelog.md'
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
35
|
+
spec.add_development_dependency 'pry'
|
36
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
37
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
38
|
+
spec.add_development_dependency 'rubocop', '~> 0.52.x'
|
39
|
+
spec.add_development_dependency 'webmock'
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: userstack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masaki Maeda
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-24 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.52.x
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.52.x
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Simple wrapper for the Userstack API https://userstack.com
|
98
|
+
email:
|
99
|
+
- m-maeda@feedforce.jp
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- bin/validate-target-ruby-version.rb
|
115
|
+
- lib/userstack.rb
|
116
|
+
- lib/userstack/client.rb
|
117
|
+
- lib/userstack/uri_builder.rb
|
118
|
+
- lib/userstack/version.rb
|
119
|
+
- userstack.gemspec
|
120
|
+
homepage: https://github.com/feedforce/userstack
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata:
|
124
|
+
homepage_uri: https://github.com/feedforce/userstack
|
125
|
+
source_code_uri: https://github.com/feedforce/userstack
|
126
|
+
changelog_uri: https://github.com/feedforce/userstack/Chengelog.md
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubygems_version: 3.0.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Ruby toolkit for working with the Userstack
|
146
|
+
test_files: []
|