gateway_sdk 0.2.0

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: deefdd9540b059b0be2d1a956a67386ec500e27d
4
+ data.tar.gz: d1440e2e2db5a06aa86a5b7adf2e862df1fdf7cb
5
+ SHA512:
6
+ metadata.gz: 571121e662cecfe7de1c76d0226ae3f05f966c9092257df8e87346a807370314770c61c4177c9db6efc9f71c3bbb53dcc4da75428aa7090762ee3caa40726c33
7
+ data.tar.gz: 47a7ad1a985d1d30000b04539844f404024a30923838f0eda7a6973bda6254f0e9323943a4a3c8cc5b31d1948c6a09560a03d0243c365dd94d014fafd71a1fec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --markup markdown
2
+ --markup-provider redcarpet
3
+ --charset utf-8
4
+ --readme README.md
5
+ -
6
+ README.md
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gateway.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+
2
+ ## Installation
3
+
4
+ Add this line to your application's Gemfile:
5
+
6
+ ```ruby
7
+ gem 'gateway'
8
+ ```
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install gateway
17
+
18
+ ## Usage
19
+
20
+ Use for sending messages from popular transports: whatsapp, viber, telegram, sms
21
+ Read more: http://chat2desk.com/
22
+
23
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ Dir.glob('tasks/*.rake').each {|r| import r}
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "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
@@ -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,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
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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Gateway
2
+ VERSION = '0.2.0' # gem version
3
+ end
data/tasks/yard.rake ADDED
@@ -0,0 +1,2 @@
1
+ require 'yard'
2
+ YARD::Rake::YardocTask.new
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: