yotpo_api_smart_map 1.0.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.
- checksums.yaml +7 -0
- data/README.md +89 -0
- data/docs/SmartMapsApi.md +53 -0
- data/docs/SmartmapAccountStats.md +8 -0
- data/docs/SmartmapArgs.md +8 -0
- data/docs/SmartmapExcludedDay.md +9 -0
- data/git_push.sh +52 -0
- data/lib/yotpo_api_smart_map.rb +41 -0
- data/lib/yotpo_api_smart_map/api/smart_maps_api.rb +81 -0
- data/lib/yotpo_api_smart_map/api_client.rb +330 -0
- data/lib/yotpo_api_smart_map/api_error.rb +34 -0
- data/lib/yotpo_api_smart_map/configuration.rb +163 -0
- data/lib/yotpo_api_smart_map/models/smartmap_account_stats.rb +161 -0
- data/lib/yotpo_api_smart_map/models/smartmap_args.rb +159 -0
- data/lib/yotpo_api_smart_map/models/smartmap_excluded_day.rb +169 -0
- data/lib/yotpo_api_smart_map/version.rb +13 -0
- data/spec/api/smart_maps_api_spec.rb +49 -0
- data/spec/models/smartmap_account_stats_spec.rb +44 -0
- data/spec/models/smartmap_args_spec.rb +44 -0
- data/spec/models/smartmap_excluded_day_spec.rb +54 -0
- data/yotpo_api_smart_map.gemspec +32 -0
- metadata +248 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ce8eb8190ab59870557280b9427d588616d7470d
|
4
|
+
data.tar.gz: 215087f0d22e54c61ef0c1368a9aa61168783119
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cdf11259d7a3938ba93c50d04298271433e571f9e05707b3382fdcebeeca41a2bbb6c443ba16c0394fa13cf30bba8caa246eb9e9b9a5056886c98eea959db022
|
7
|
+
data.tar.gz: b2c0445b8da1117cf5d4525d8cd95901154236df7ad1173e3961b3a20fea0e9f0eaf6480ecba3aea302e7d5f4d021fa5d9175040200306bdbc84a3a74cbb1402
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# yotpo_api_smart_map
|
2
|
+
|
3
|
+
YotpoApiSmartMap - the Ruby gem for the smartmap.proto
|
4
|
+
|
5
|
+
|
6
|
+
This SDK is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
|
7
|
+
|
8
|
+
- API version: version not set
|
9
|
+
- Package version: 1.0.0
|
10
|
+
- Build date: 2016-08-01T15:17:32.211+03:00
|
11
|
+
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
### Build a gem
|
16
|
+
|
17
|
+
To build the Ruby code into a gem:
|
18
|
+
|
19
|
+
```shell
|
20
|
+
gem build yotpo_api_smart_map.gemspec
|
21
|
+
```
|
22
|
+
|
23
|
+
Then either install the gem locally:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
gem install ./yotpo_api_smart_map-1.0.0.gem
|
27
|
+
```
|
28
|
+
|
29
|
+
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
|
30
|
+
|
31
|
+
Finally add this to the Gemfile:
|
32
|
+
|
33
|
+
gem 'yotpo_api_smart_map', '~> 1.0.0'
|
34
|
+
|
35
|
+
### Install from Git
|
36
|
+
|
37
|
+
If the Ruby gem is hosted at a git repository: https://github.com/YOUR_GIT_USR_ID/YOUR_GIT_REPO_ID, then add the following in the Gemfile:
|
38
|
+
|
39
|
+
gem 'yotpo_api_smart_map', :git => 'https://github.com/YOUR_GIT_USR_ID/YOUR_GIT_REPO_ID.git'
|
40
|
+
|
41
|
+
### Include the Ruby code directly
|
42
|
+
|
43
|
+
Include the Ruby code directly using `-I` as follows:
|
44
|
+
|
45
|
+
```shell
|
46
|
+
ruby -Ilib script.rb
|
47
|
+
```
|
48
|
+
|
49
|
+
## Getting Started
|
50
|
+
|
51
|
+
Please follow the [installation](#installation) procedure and then run the following code:
|
52
|
+
```ruby
|
53
|
+
# Load the gem
|
54
|
+
require 'yotpo_api_smart_map'
|
55
|
+
|
56
|
+
api_instance = YotpoApiSmartMap::SmartMapsApi.new
|
57
|
+
|
58
|
+
account_id = "account_id_example" # String |
|
59
|
+
|
60
|
+
|
61
|
+
begin
|
62
|
+
result = api_instance.get(account_id)
|
63
|
+
p result
|
64
|
+
rescue YotpoApiSmartMap::ApiError => e
|
65
|
+
puts "Exception when calling SmartMapsApi->get: #{e}"
|
66
|
+
end
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
## Documentation for API Endpoints
|
71
|
+
|
72
|
+
All URIs are relative to *http://localhost*
|
73
|
+
|
74
|
+
Class | Method | HTTP request | Description
|
75
|
+
------------ | ------------- | ------------- | -------------
|
76
|
+
*YotpoApiSmartMap::SmartMapsApi* | [**get**](docs/SmartMapsApi.md#get) | **GET** /v1/{accountId} |
|
77
|
+
|
78
|
+
|
79
|
+
## Documentation for Models
|
80
|
+
|
81
|
+
- [YotpoApiSmartMap::SmartmapAccountStats](docs/SmartmapAccountStats.md)
|
82
|
+
- [YotpoApiSmartMap::SmartmapArgs](docs/SmartmapArgs.md)
|
83
|
+
- [YotpoApiSmartMap::SmartmapExcludedDay](docs/SmartmapExcludedDay.md)
|
84
|
+
|
85
|
+
|
86
|
+
## Documentation for Authorization
|
87
|
+
|
88
|
+
All endpoints do not require authorization.
|
89
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# YotpoApiSmartMap::SmartMapsApi
|
2
|
+
|
3
|
+
All URIs are relative to *http://localhost*
|
4
|
+
|
5
|
+
Method | HTTP request | Description
|
6
|
+
------------- | ------------- | -------------
|
7
|
+
[**get**](SmartMapsApi.md#get) | **GET** /v1/{accountId} |
|
8
|
+
|
9
|
+
|
10
|
+
# **get**
|
11
|
+
> SmartmapAccountStats get(account_id)
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
### Example
|
16
|
+
```ruby
|
17
|
+
# load the gem
|
18
|
+
require 'yotpo_api_smart_map'
|
19
|
+
|
20
|
+
api_instance = YotpoApiSmartMap::SmartMapsApi.new
|
21
|
+
|
22
|
+
account_id = "account_id_example" # String |
|
23
|
+
|
24
|
+
|
25
|
+
begin
|
26
|
+
result = api_instance.get(account_id)
|
27
|
+
p result
|
28
|
+
rescue YotpoApiSmartMap::ApiError => e
|
29
|
+
puts "Exception when calling SmartMapsApi->get: #{e}"
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
### Parameters
|
34
|
+
|
35
|
+
Name | Type | Description | Notes
|
36
|
+
------------- | ------------- | ------------- | -------------
|
37
|
+
**account_id** | **String**| |
|
38
|
+
|
39
|
+
### Return type
|
40
|
+
|
41
|
+
[**SmartmapAccountStats**](SmartmapAccountStats.md)
|
42
|
+
|
43
|
+
### Authorization
|
44
|
+
|
45
|
+
No authorization required
|
46
|
+
|
47
|
+
### HTTP reuqest headers
|
48
|
+
|
49
|
+
- **Content-Type**: application/json
|
50
|
+
- **Accept**: application/json
|
51
|
+
|
52
|
+
|
53
|
+
|
data/git_push.sh
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
|
3
|
+
#
|
4
|
+
# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update"
|
5
|
+
|
6
|
+
git_user_id=$1
|
7
|
+
git_repo_id=$2
|
8
|
+
release_note=$3
|
9
|
+
|
10
|
+
if [ "$git_user_id" = "" ]; then
|
11
|
+
git_user_id="YOUR_GIT_USR_ID"
|
12
|
+
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
|
13
|
+
fi
|
14
|
+
|
15
|
+
if [ "$git_repo_id" = "" ]; then
|
16
|
+
git_repo_id="YOUR_GIT_REPO_ID"
|
17
|
+
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
|
18
|
+
fi
|
19
|
+
|
20
|
+
if [ "$release_note" = "" ]; then
|
21
|
+
release_note="Minor update"
|
22
|
+
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
|
23
|
+
fi
|
24
|
+
|
25
|
+
# Initialize the local directory as a Git repository
|
26
|
+
git init
|
27
|
+
|
28
|
+
# Adds the files in the local repository and stages them for commit.
|
29
|
+
git add .
|
30
|
+
|
31
|
+
# Commits the tracked changes and prepares them to be pushed to a remote repository.
|
32
|
+
git commit -m "$release_note"
|
33
|
+
|
34
|
+
# Sets the new remote
|
35
|
+
git_remote=`git remote`
|
36
|
+
if [ "$git_remote" = "" ]; then # git remote not defined
|
37
|
+
|
38
|
+
if [ "$GIT_TOKEN" = "" ]; then
|
39
|
+
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment."
|
40
|
+
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
|
41
|
+
else
|
42
|
+
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
|
43
|
+
fi
|
44
|
+
|
45
|
+
fi
|
46
|
+
|
47
|
+
git pull origin master
|
48
|
+
|
49
|
+
# Pushes (Forces) the changes in the local repository up to the remote repository
|
50
|
+
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
|
51
|
+
git push origin master 2>&1 | grep -v 'To https'
|
52
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
=begin
|
2
|
+
smartmap.proto
|
3
|
+
|
4
|
+
OpenAPI spec version: version not set
|
5
|
+
|
6
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
7
|
+
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
# Common files
|
12
|
+
require 'yotpo_api_smart_map/api_client'
|
13
|
+
require 'yotpo_api_smart_map/api_error'
|
14
|
+
require 'yotpo_api_smart_map/version'
|
15
|
+
require 'yotpo_api_smart_map/configuration'
|
16
|
+
|
17
|
+
# Models
|
18
|
+
require 'yotpo_api_smart_map/models/smartmap_account_stats'
|
19
|
+
require 'yotpo_api_smart_map/models/smartmap_args'
|
20
|
+
require 'yotpo_api_smart_map/models/smartmap_excluded_day'
|
21
|
+
|
22
|
+
# APIs
|
23
|
+
require 'yotpo_api_smart_map/api/smart_maps_api'
|
24
|
+
|
25
|
+
module YotpoApiSmartMap
|
26
|
+
class << self
|
27
|
+
# Customize default settings for the SDK using block.
|
28
|
+
# YotpoApiSmartMap.configure do |config|
|
29
|
+
# config.username = "xxx"
|
30
|
+
# config.password = "xxx"
|
31
|
+
# end
|
32
|
+
# If no block given, return the default Configuration object.
|
33
|
+
def configure
|
34
|
+
if block_given?
|
35
|
+
yield(Configuration.default)
|
36
|
+
else
|
37
|
+
Configuration.default
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
=begin
|
2
|
+
smartmap.proto
|
3
|
+
|
4
|
+
OpenAPI spec version: version not set
|
5
|
+
|
6
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
7
|
+
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
require "uri"
|
12
|
+
|
13
|
+
module YotpoApiSmartMap
|
14
|
+
class SmartMapsApi
|
15
|
+
attr_accessor :api_client
|
16
|
+
|
17
|
+
def initialize(api_client = ApiClient.default)
|
18
|
+
@api_client = api_client
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
#
|
23
|
+
# @param account_id
|
24
|
+
# @param [Hash] opts the optional parameters
|
25
|
+
# @return [SmartmapAccountStats]
|
26
|
+
def get(account_id, opts = {})
|
27
|
+
data, status_code, headers = get_with_http_info(account_id, opts)
|
28
|
+
return data
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# @param account_id
|
34
|
+
# @param [Hash] opts the optional parameters
|
35
|
+
# @return [Array<(SmartmapAccountStats, Fixnum, Hash)>] SmartmapAccountStats data, response status code and response headers
|
36
|
+
def get_with_http_info(account_id, opts = {})
|
37
|
+
if @api_client.config.debugging
|
38
|
+
@api_client.config.logger.debug "Calling API: SmartMapsApi#get ..."
|
39
|
+
end
|
40
|
+
|
41
|
+
# verify the required parameter 'account_id' is set
|
42
|
+
fail "Missing the required parameter 'account_id' when calling get" if account_id.nil?
|
43
|
+
|
44
|
+
# resource path
|
45
|
+
local_var_path = "/v1/{accountId}".sub('{format}','json').sub('{' + 'accountId' + '}', account_id.to_s)
|
46
|
+
|
47
|
+
# query parameters
|
48
|
+
query_params = {}
|
49
|
+
|
50
|
+
# header parameters
|
51
|
+
header_params = {}
|
52
|
+
|
53
|
+
# HTTP header 'Accept' (if needed)
|
54
|
+
_header_accept = ['application/json']
|
55
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
56
|
+
|
57
|
+
# HTTP header 'Content-Type'
|
58
|
+
_header_content_type = ['application/json']
|
59
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
60
|
+
|
61
|
+
# form parameters
|
62
|
+
form_params = {}
|
63
|
+
|
64
|
+
# http body (model)
|
65
|
+
post_body = nil
|
66
|
+
|
67
|
+
auth_names = []
|
68
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path,
|
69
|
+
:header_params => header_params,
|
70
|
+
:query_params => query_params,
|
71
|
+
:form_params => form_params,
|
72
|
+
:body => post_body,
|
73
|
+
:auth_names => auth_names,
|
74
|
+
:return_type => 'SmartmapAccountStats')
|
75
|
+
if @api_client.config.debugging
|
76
|
+
@api_client.config.logger.debug "API called: SmartMapsApi#get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
77
|
+
end
|
78
|
+
return data, status_code, headers
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,330 @@
|
|
1
|
+
=begin
|
2
|
+
smartmap.proto
|
3
|
+
|
4
|
+
OpenAPI spec version: version not set
|
5
|
+
|
6
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
7
|
+
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
require 'date'
|
12
|
+
require 'json'
|
13
|
+
require 'logger'
|
14
|
+
require 'tempfile'
|
15
|
+
require 'typhoeus'
|
16
|
+
require 'uri'
|
17
|
+
|
18
|
+
module YotpoApiSmartMap
|
19
|
+
class ApiClient
|
20
|
+
# The Configuration object holding settings to be used in the API client.
|
21
|
+
attr_accessor :config
|
22
|
+
|
23
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
24
|
+
#
|
25
|
+
# @return [Hash]
|
26
|
+
attr_accessor :default_headers
|
27
|
+
|
28
|
+
def initialize(config = Configuration.default)
|
29
|
+
@config = config
|
30
|
+
@user_agent = "Swagger-Codegen/#{VERSION}/ruby"
|
31
|
+
@default_headers = {
|
32
|
+
'Content-Type' => "application/json",
|
33
|
+
'User-Agent' => @user_agent
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.default
|
38
|
+
@@default ||= ApiClient.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# Call an API with given options.
|
42
|
+
#
|
43
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
44
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
45
|
+
def call_api(http_method, path, opts = {})
|
46
|
+
request = build_request(http_method, path, opts)
|
47
|
+
response = request.run
|
48
|
+
|
49
|
+
if @config.debugging
|
50
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
51
|
+
end
|
52
|
+
|
53
|
+
unless response.success?
|
54
|
+
fail ApiError.new(:code => response.code,
|
55
|
+
:response_headers => response.headers,
|
56
|
+
:response_body => response.body),
|
57
|
+
response.status_message
|
58
|
+
end
|
59
|
+
|
60
|
+
if opts[:return_type]
|
61
|
+
data = deserialize(response, opts[:return_type])
|
62
|
+
else
|
63
|
+
data = nil
|
64
|
+
end
|
65
|
+
return data, response.code, response.headers
|
66
|
+
end
|
67
|
+
|
68
|
+
def build_request(http_method, path, opts = {})
|
69
|
+
url = build_request_url(path)
|
70
|
+
http_method = http_method.to_sym.downcase
|
71
|
+
|
72
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
73
|
+
query_params = opts[:query_params] || {}
|
74
|
+
form_params = opts[:form_params] || {}
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
req_opts = {
|
79
|
+
:method => http_method,
|
80
|
+
:headers => header_params,
|
81
|
+
:params => query_params,
|
82
|
+
:timeout => @config.timeout,
|
83
|
+
:ssl_verifypeer => @config.verify_ssl,
|
84
|
+
:sslcert => @config.cert_file,
|
85
|
+
:sslkey => @config.key_file,
|
86
|
+
:verbose => @config.debugging
|
87
|
+
}
|
88
|
+
|
89
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
90
|
+
|
91
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
92
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
93
|
+
req_opts.update :body => req_body
|
94
|
+
if @config.debugging
|
95
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
Typhoeus::Request.new(url, req_opts)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Check if the given MIME is a JSON MIME.
|
103
|
+
# JSON MIME examples:
|
104
|
+
# application/json
|
105
|
+
# application/json; charset=UTF8
|
106
|
+
# APPLICATION/JSON
|
107
|
+
def json_mime?(mime)
|
108
|
+
!!(mime =~ /\Aapplication\/json(;.*)?\z/i)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Deserialize the response to the given return type.
|
112
|
+
#
|
113
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
114
|
+
def deserialize(response, return_type)
|
115
|
+
body = response.body
|
116
|
+
return nil if body.nil? || body.empty?
|
117
|
+
|
118
|
+
# return response body directly for String return type
|
119
|
+
return body if return_type == 'String'
|
120
|
+
|
121
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
122
|
+
return download_file(response) if return_type == 'File'
|
123
|
+
|
124
|
+
# ensuring a default content type
|
125
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
126
|
+
|
127
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
128
|
+
|
129
|
+
begin
|
130
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
131
|
+
rescue JSON::ParserError => e
|
132
|
+
if %w(String Date DateTime).include?(return_type)
|
133
|
+
data = body
|
134
|
+
else
|
135
|
+
raise e
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
convert_to_type data, return_type
|
140
|
+
end
|
141
|
+
|
142
|
+
# Convert data to the given return type.
|
143
|
+
def convert_to_type(data, return_type)
|
144
|
+
return nil if data.nil?
|
145
|
+
case return_type
|
146
|
+
when 'String'
|
147
|
+
data.to_s
|
148
|
+
when 'Integer'
|
149
|
+
data.to_i
|
150
|
+
when 'Float'
|
151
|
+
data.to_f
|
152
|
+
when 'BOOLEAN'
|
153
|
+
data == true
|
154
|
+
when 'DateTime'
|
155
|
+
# parse date time (expecting ISO 8601 format)
|
156
|
+
DateTime.parse data
|
157
|
+
when 'Date'
|
158
|
+
# parse date time (expecting ISO 8601 format)
|
159
|
+
Date.parse data
|
160
|
+
when 'Object'
|
161
|
+
# generic object (usually a Hash), return directly
|
162
|
+
data
|
163
|
+
when /\AArray<(.+)>\z/
|
164
|
+
# e.g. Array<Pet>
|
165
|
+
sub_type = $1
|
166
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
167
|
+
when /\AHash\<String, (.+)\>\z/
|
168
|
+
# e.g. Hash<String, Integer>
|
169
|
+
sub_type = $1
|
170
|
+
{}.tap do |hash|
|
171
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
172
|
+
end
|
173
|
+
else
|
174
|
+
# models, e.g. Pet
|
175
|
+
YotpoApiSmartMap.const_get(return_type).new.tap do |model|
|
176
|
+
model.build_from_hash data
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
182
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
183
|
+
#
|
184
|
+
# @see Configuration#temp_folder_path
|
185
|
+
# @return [Tempfile] the file downloaded
|
186
|
+
def download_file(response)
|
187
|
+
content_disposition = response.headers['Content-Disposition']
|
188
|
+
if content_disposition
|
189
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
190
|
+
prefix = sanitize_filename(filename)
|
191
|
+
else
|
192
|
+
prefix = 'download-'
|
193
|
+
end
|
194
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
195
|
+
|
196
|
+
tempfile = nil
|
197
|
+
encoding = response.body.encoding
|
198
|
+
Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
|
199
|
+
file.write(response.body)
|
200
|
+
tempfile = file
|
201
|
+
end
|
202
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
203
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
204
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
205
|
+
"explicitly with `tempfile.delete`"
|
206
|
+
tempfile
|
207
|
+
end
|
208
|
+
|
209
|
+
# Sanitize filename by removing path.
|
210
|
+
# e.g. ../../sun.gif becomes sun.gif
|
211
|
+
#
|
212
|
+
# @param [String] filename the filename to be sanitized
|
213
|
+
# @return [String] the sanitized filename
|
214
|
+
def sanitize_filename(filename)
|
215
|
+
filename.gsub /.*[\/\\]/, ''
|
216
|
+
end
|
217
|
+
|
218
|
+
def build_request_url(path)
|
219
|
+
# Add leading and trailing slashes to path
|
220
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
221
|
+
URI.encode(@config.base_url + path)
|
222
|
+
end
|
223
|
+
|
224
|
+
def build_request_body(header_params, form_params, body)
|
225
|
+
# http form
|
226
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
227
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
228
|
+
data = {}
|
229
|
+
form_params.each do |key, value|
|
230
|
+
case value
|
231
|
+
when File, Array, nil
|
232
|
+
# let typhoeus handle File, Array and nil parameters
|
233
|
+
data[key] = value
|
234
|
+
else
|
235
|
+
data[key] = value.to_s
|
236
|
+
end
|
237
|
+
end
|
238
|
+
elsif body
|
239
|
+
data = body.is_a?(String) ? body : body.to_json
|
240
|
+
else
|
241
|
+
data = nil
|
242
|
+
end
|
243
|
+
data
|
244
|
+
end
|
245
|
+
|
246
|
+
# Update hearder and query params based on authentication settings.
|
247
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
248
|
+
Array(auth_names).each do |auth_name|
|
249
|
+
auth_setting = @config.auth_settings[auth_name]
|
250
|
+
next unless auth_setting
|
251
|
+
case auth_setting[:in]
|
252
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
253
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
254
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def user_agent=(user_agent)
|
260
|
+
@user_agent = user_agent
|
261
|
+
@default_headers['User-Agent'] = @user_agent
|
262
|
+
end
|
263
|
+
|
264
|
+
# Return Accept header based on an array of accepts provided.
|
265
|
+
# @param [Array] accepts array for Accept
|
266
|
+
# @return [String] the Accept header (e.g. application/json)
|
267
|
+
def select_header_accept(accepts)
|
268
|
+
return nil if accepts.nil? || accepts.empty?
|
269
|
+
# use JSON when present, otherwise use all of the provided
|
270
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
271
|
+
return json_accept || accepts.join(',')
|
272
|
+
end
|
273
|
+
|
274
|
+
# Return Content-Type header based on an array of content types provided.
|
275
|
+
# @param [Array] content_types array for Content-Type
|
276
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
277
|
+
def select_header_content_type(content_types)
|
278
|
+
# use application/json by default
|
279
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
280
|
+
# use JSON when present, otherwise use the first one
|
281
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
282
|
+
return json_content_type || content_types.first
|
283
|
+
end
|
284
|
+
|
285
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
286
|
+
# @param [Object] model object to be converted into JSON string
|
287
|
+
# @return [String] JSON string representation of the object
|
288
|
+
def object_to_http_body(model)
|
289
|
+
return model if model.nil? || model.is_a?(String)
|
290
|
+
_body = nil
|
291
|
+
if model.is_a?(Array)
|
292
|
+
_body = model.map{|m| object_to_hash(m) }
|
293
|
+
else
|
294
|
+
_body = object_to_hash(model)
|
295
|
+
end
|
296
|
+
_body.to_json
|
297
|
+
end
|
298
|
+
|
299
|
+
# Convert object(non-array) to hash.
|
300
|
+
# @param [Object] obj object to be converted into JSON string
|
301
|
+
# @return [String] JSON string representation of the object
|
302
|
+
def object_to_hash(obj)
|
303
|
+
if obj.respond_to?(:to_hash)
|
304
|
+
obj.to_hash
|
305
|
+
else
|
306
|
+
obj
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
# Build parameter value according to the given collection format.
|
311
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
312
|
+
def build_collection_param(param, collection_format)
|
313
|
+
case collection_format
|
314
|
+
when :csv
|
315
|
+
param.join(',')
|
316
|
+
when :ssv
|
317
|
+
param.join(' ')
|
318
|
+
when :tsv
|
319
|
+
param.join("\t")
|
320
|
+
when :pipes
|
321
|
+
param.join('|')
|
322
|
+
when :multi
|
323
|
+
# return the array directly as typhoeus will handle it as expected
|
324
|
+
param
|
325
|
+
else
|
326
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|