gateway-sdk 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/.yardopts +6 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/gateway-sdk.gemspec +30 -0
- data/lib/gateway/client.rb +74 -0
- data/lib/gateway/config.yml +10 -0
- data/lib/gateway/errors.rb +35 -0
- data/lib/gateway/requests/common_request.rb +25 -0
- data/lib/gateway/requests/send_message_request.rb +33 -0
- data/lib/gateway/requests/set_profile_request.rb +32 -0
- data/lib/gateway/version.rb +3 -0
- data/lib/gateway.rb +16 -0
- data/tasks/yard.rake +2 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d4c55497b0328920759cd6e77eebb9ba0eca4394
|
4
|
+
data.tar.gz: fd2c4a381b1b4a6924543c09d19240886d75a8fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7e38b00bee0270cce9a135095ae2459d4df5ab1d60d668c1b6b838f85f9386b9b4706999a825b656f4412126601927a921272878910cef4c35ed896adbf72bd
|
7
|
+
data.tar.gz: ad154e70fa058c3ea197076f9454ba84652fe517459884a107ab8f0cca91f9464234c146aed031345451434c26948a4903e0b0c185fabf89104d7118ea0cb1af
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Gateway::Sdk
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gateway/sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'gateway-sdk'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install gateway-sdk
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/gateway-sdk/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gateway/gateway"
|
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
data/gateway-sdk.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gateway/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gateway-sdk"
|
8
|
+
spec.version = Gateway::VERSION
|
9
|
+
spec.authors = ["atanych"]
|
10
|
+
spec.email = ["atanych@gmail.com"]
|
11
|
+
|
12
|
+
# if spec.respond_to?(:metadata)
|
13
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
14
|
+
# end
|
15
|
+
|
16
|
+
spec.summary = 'gateway SDK'
|
17
|
+
spec.description = 'SDK for interaction with gateway'
|
18
|
+
spec.homepage = 'https://github.com/atanych/gateway_sdk.git'
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "redcarpet"
|
29
|
+
spec.add_development_dependency "yard"
|
30
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'yaml'
|
3
|
+
require 'uri'
|
4
|
+
require 'rest-client'
|
5
|
+
module Gateway
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :config
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Client
|
13
|
+
#
|
14
|
+
class Client
|
15
|
+
|
16
|
+
METHOD_GET = 'get' # http method GET
|
17
|
+
METHOD_POST = 'post' # http method POST
|
18
|
+
|
19
|
+
attr_accessor :request
|
20
|
+
attr_reader :config_data, :test
|
21
|
+
|
22
|
+
#
|
23
|
+
# Initialize
|
24
|
+
#
|
25
|
+
# @param [Gateway::Requests::CommonRequest] request
|
26
|
+
# @param [TrueClass|FalseClass] test test or production modes
|
27
|
+
#
|
28
|
+
def initialize(request, test = false)
|
29
|
+
Gateway.config = JSON.parse(YAML.load_file("#{Gateway.root}/gateway/config.yml").to_json, object_class: OpenStruct)
|
30
|
+
@request = request
|
31
|
+
@test = test
|
32
|
+
request_name = request.class.name.split('::').last
|
33
|
+
@config_data = Gateway.config.api_methods[request_name]
|
34
|
+
raise Gateway::StandardError.new("fill data in config.yml for method: #{request_name}") unless @config_data
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Send request to gateway and take response from it
|
39
|
+
#
|
40
|
+
# @return [Hash] raw response from gateway
|
41
|
+
#
|
42
|
+
def response
|
43
|
+
# Rails.logger.debug('gateway request: ' + YAML::dump(request))
|
44
|
+
query_params = {r: config_data.route}
|
45
|
+
case config_data.http_method
|
46
|
+
when METHOD_GET
|
47
|
+
query_params.merge! request.attributes
|
48
|
+
response = RestClient.get(url(query_params))
|
49
|
+
when METHOD_POST
|
50
|
+
response = RestClient.post(url(query_params), request.attributes)
|
51
|
+
else
|
52
|
+
raise Gateway::ArgumentError.new("http method #{config_data.http_method} is not supported")
|
53
|
+
end
|
54
|
+
return JSON.parse(response)
|
55
|
+
rescue RestClient::Exception => e
|
56
|
+
response = JSON.parse e.response
|
57
|
+
raise Gateway::StandardError.new(response['message'])
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
#
|
62
|
+
# Generate url
|
63
|
+
#
|
64
|
+
# @param [Hash] query_params
|
65
|
+
#
|
66
|
+
# @return [String] url with query params
|
67
|
+
#
|
68
|
+
def url(query_params)
|
69
|
+
url = test ? Gateway.config.urls.test : Gateway.config.urls.production
|
70
|
+
url += '?' + URI.encode_www_form(query_params) unless query_params.empty?
|
71
|
+
url
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
urls:
|
2
|
+
production: "http://gateway.sms-vote.ru"
|
3
|
+
test: "http://staging.gateway.sms-vote.ru"
|
4
|
+
api_methods:
|
5
|
+
SendMessageRequest:
|
6
|
+
route: "scheduler/by-phone-and-transport"
|
7
|
+
http_method: "post"
|
8
|
+
SetProfileRequest:
|
9
|
+
route: "scheduler/profile"
|
10
|
+
http_method: "post"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Gateway
|
2
|
+
#
|
3
|
+
# Standard error
|
4
|
+
#
|
5
|
+
class StandardError < StandardError
|
6
|
+
attr_accessor :message
|
7
|
+
|
8
|
+
#
|
9
|
+
# Initialize
|
10
|
+
#
|
11
|
+
# @param [String] message
|
12
|
+
#
|
13
|
+
def initialize(message)
|
14
|
+
@message = message
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Argument error
|
21
|
+
#
|
22
|
+
class ArgumentError < ArgumentError
|
23
|
+
attr_accessor :message
|
24
|
+
|
25
|
+
#
|
26
|
+
# Initialize
|
27
|
+
#
|
28
|
+
# @param [String] message
|
29
|
+
#
|
30
|
+
def initialize(message)
|
31
|
+
@message = message
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Requests
|
3
|
+
|
4
|
+
#
|
5
|
+
# Common request
|
6
|
+
# All request is needed to inheritance from it
|
7
|
+
#
|
8
|
+
class CommonRequest
|
9
|
+
|
10
|
+
#
|
11
|
+
# Convert instance variables to hash
|
12
|
+
#
|
13
|
+
# @return [Hash]
|
14
|
+
#
|
15
|
+
def attributes
|
16
|
+
hash = {}
|
17
|
+
instance_variables.each do |attr|
|
18
|
+
hash[attr[1..-1]] = instance_variable_get attr
|
19
|
+
end
|
20
|
+
hash
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path('../common_request', __FILE__)
|
2
|
+
module Gateway
|
3
|
+
module Requests
|
4
|
+
#
|
5
|
+
# Request to send message via gateway
|
6
|
+
#
|
7
|
+
class SendMessageRequest < Gateway::Requests::CommonRequest
|
8
|
+
|
9
|
+
TRANSPORT_WA = 'whatsapp' # transport whatsapp
|
10
|
+
TRANSPORT_VIBER = 'viber' # transport viber
|
11
|
+
TRANSPORT_TG = 'telegram' # transport telegram
|
12
|
+
TRANSPORT_SMS = 'modem' # transport modem
|
13
|
+
|
14
|
+
attr_accessor :phone, :devicePhone, :body, :image, :transport
|
15
|
+
|
16
|
+
#
|
17
|
+
# Possible transports
|
18
|
+
#
|
19
|
+
def self.possible_transports
|
20
|
+
[TRANSPORT_WA, TRANSPORT_VIBER, TRANSPORT_TG, TRANSPORT_SMS]
|
21
|
+
end
|
22
|
+
|
23
|
+
def transport=(transport)
|
24
|
+
unless self.class.possible_transports.include? transport
|
25
|
+
raise Gateway::ArgumentError.new("transport #{transport} is not supported, use: #{self.class.possible_transports.join(', ')}")
|
26
|
+
end
|
27
|
+
@transport = transport
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../common_request', __FILE__)
|
2
|
+
module Gateway
|
3
|
+
module Requests
|
4
|
+
#
|
5
|
+
# Request to change settings for whatsapp account via gateway
|
6
|
+
#
|
7
|
+
class SetProfileRequest < CommonRequest
|
8
|
+
|
9
|
+
TYPE_NICKNAME = 1 # nickname
|
10
|
+
TYPE_AVATAR = 2 # path to avatar
|
11
|
+
TYPE_STATUS = 3 # status
|
12
|
+
|
13
|
+
attr_accessor :type, :devicePhone, :value
|
14
|
+
|
15
|
+
#
|
16
|
+
# Possible types
|
17
|
+
#
|
18
|
+
def self.possible_types
|
19
|
+
[TYPE_NICKNAME, TYPE_AVATAR, TYPE_STATUS]
|
20
|
+
end
|
21
|
+
|
22
|
+
def type=(type)
|
23
|
+
unless self.class.possible_types.include? type
|
24
|
+
raise Gateway::ArgumentError.new("type #{type} is not supported, use: #{self.class.possible_types.join(', ')}")
|
25
|
+
end
|
26
|
+
@type = type
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/gateway.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/gateway/**/*.rb"].each {|file| require file }
|
2
|
+
|
3
|
+
#
|
4
|
+
# SDK for gateway (sending messages and setting params)
|
5
|
+
#
|
6
|
+
module Gateway
|
7
|
+
|
8
|
+
#
|
9
|
+
# Return root path
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
#
|
13
|
+
def self.root
|
14
|
+
File.dirname __FILE__
|
15
|
+
end
|
16
|
+
end
|
data/tasks/yard.rake
ADDED
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gateway-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- atanych
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-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: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
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: redcarpet
|
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: yard
|
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: SDK for interaction with gateway
|
70
|
+
email:
|
71
|
+
- atanych@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- ".yardopts"
|
80
|
+
- Gemfile
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- gateway-sdk.gemspec
|
86
|
+
- lib/gateway.rb
|
87
|
+
- lib/gateway/client.rb
|
88
|
+
- lib/gateway/config.yml
|
89
|
+
- lib/gateway/errors.rb
|
90
|
+
- lib/gateway/requests/common_request.rb
|
91
|
+
- lib/gateway/requests/send_message_request.rb
|
92
|
+
- lib/gateway/requests/set_profile_request.rb
|
93
|
+
- lib/gateway/version.rb
|
94
|
+
- tasks/yard.rake
|
95
|
+
homepage: https://github.com/atanych/gateway_sdk.git
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: gateway SDK
|
119
|
+
test_files: []
|
120
|
+
has_rdoc:
|