comagic_client 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/comagic_client.rb +198 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b4d277c89a61b78bb520d4bd8d0d6d452c6acc7
4
+ data.tar.gz: 8a7b33a639dcbcc8b4796f237ce9fe5c6ef3d90e
5
+ SHA512:
6
+ metadata.gz: 39013b8ae3aa648f72aa5f6abcd75fdc42accf09644ce6f3ef022d142b96de6066467e1e2297ed3606013ada515a8133df04c2a7933c2b28723411ad31ead076
7
+ data.tar.gz: 6270f2084b8196b9a0ff3dc043e5d3290ad2f93781a6ddb0228dcf5123ab7e7a712c0450e238aa4384875ad95bb130d7e37ea11dca32339a4b4f0612513e9987
@@ -0,0 +1,198 @@
1
+ %w{rest_client json}.each { |lib| require lib }
2
+
3
+ class Hash
4
+ def not_nils!
5
+ reject! { |k,v| v.nil? }
6
+ end
7
+ end
8
+
9
+ # This class creates a new comagic connector instanse
10
+ class ComagicClient
11
+
12
+ # Comagic API error class
13
+ class ApiError < StandardError
14
+ end
15
+
16
+ # Set up a new connector instanse
17
+ def initialize(login, password)
18
+ @session_key = nil
19
+ @api_url = 'http://api.comagic.ru/api'
20
+ @login, @password = login, password
21
+ end
22
+
23
+ # Perform login with login and password specified in the initialize method
24
+ def login
25
+ response = post "login", login: @login, password: @password
26
+ @session_key = response['session_key']
27
+ end
28
+
29
+
30
+ # Perform logout
31
+ def logout
32
+ require_auth!
33
+ get "logout", session_key: @session_key
34
+ end
35
+
36
+ # Get sit(es)
37
+ def site(options = {})
38
+ require_auth!
39
+ options.merge!({ session_key: @session_key }).not_nils!
40
+ get 'v1/site', options
41
+ end
42
+
43
+ # Get ad campaigns
44
+ def ac(options = {})
45
+ require_auth!
46
+ options.merge!({ session_key: @session_key }).not_nils!
47
+ get 'v1/ac', options
48
+ end
49
+
50
+ # Get tags
51
+ def tag(options = {})
52
+ require_auth!
53
+ options.merge!({ session_key: @session_key }).not_nils!
54
+ get 'v1/tag', options
55
+ end
56
+
57
+ # Get communications
58
+ def communication(options = {})
59
+ require_auth!
60
+ options.merge!({ session_key: @session_key }).not_nils!
61
+ get 'v1/communication', options
62
+ end
63
+
64
+ # Get stat
65
+ def stat(options = {})
66
+ require_auth!
67
+ options.merge!({ session_key: @session_key }).not_nils!
68
+ get 'v1/stat', options
69
+ end
70
+
71
+ # Get goal
72
+ def goal(options = {})
73
+ require_auth!
74
+ options.merge!({ session_key: @session_key }).not_nils!
75
+ get 'v1/goal', options
76
+ end
77
+
78
+ # Get call
79
+ def call(options = {})
80
+ require_auth!
81
+ options.merge!({ session_key: @session_key }).not_nils!
82
+ get 'v1/call', options
83
+ end
84
+
85
+ # Get stat
86
+ def stat(options = {})
87
+ require_auth!
88
+ options.merge!({ session_key: @session_key }).not_nils!
89
+ get 'v1/stat', options
90
+ end
91
+
92
+ # Get chat
93
+ def chat(options = {})
94
+ require_auth!
95
+ options.merge!({ session_key: @session_key }).not_nils!
96
+ get 'v1/chat', options
97
+ end
98
+
99
+ # Get chat_message
100
+ def chat_message(options = {})
101
+ require_auth!
102
+ options.merge!({ session_key: @session_key }).not_nils!
103
+ get 'v1/chat_message', options
104
+ end
105
+
106
+ # Get offline_message
107
+ def offline_message(options = {})
108
+ require_auth!
109
+ options.merge!({ session_key: @session_key }).not_nils!
110
+ get 'v1/offline_message', options
111
+ end
112
+
113
+ # Get person_by_visitor
114
+ def person_by_visitor(options = {})
115
+ require_auth!
116
+ options.merge!({ session_key: @session_key }).not_nils!
117
+ get 'v1/person_by_visitor', options
118
+ end
119
+
120
+ # Get visitors_by_person
121
+ def visitors_by_person(options = {})
122
+ require_auth!
123
+ options.merge!({ session_key: @session_key }).not_nils!
124
+ get 'v1/visitors_by_person', options
125
+ end
126
+
127
+ # Get visitor
128
+ def visitor(options = {})
129
+ require_auth!
130
+ options.merge!({ session_key: @session_key }).not_nils!
131
+ get 'v1/visitor', options
132
+ end
133
+
134
+ # Get get_cdr_in
135
+ def get_cdr_in(options = {})
136
+ require_auth!
137
+ options.merge!({ session_key: @session_key }).not_nils!
138
+ get 'v1/get_cdr_in', options
139
+ end
140
+
141
+ # Get get_cdr_out
142
+ def get_cdr_out(options = {})
143
+ require_auth!
144
+ options.merge!({ session_key: @session_key }).not_nils!
145
+ get 'v1/get_cdr_out', options
146
+ end
147
+
148
+ def create_site(options = {})
149
+ require_auth!
150
+ options.merge!({ session_key: @session_key }).not_nils!
151
+ post 'v1/create_site', options
152
+ end
153
+
154
+ # Pass post, get, delete, put and patch to the request method
155
+ def method_missing(method_name, *arguments, &block)
156
+ if method_name.to_s =~ /(post|get|put|patch|delete)/
157
+ request($1.to_sym, *arguments, &block)
158
+ else
159
+ super
160
+ end
161
+ end
162
+
163
+ # Let class instance respond to post, get, delete, put and patch
164
+ def respond_to?(method_name, *arguments, &block)
165
+ !!method_name.to_s.match(/(post|get|put|patch|delete)/) || super
166
+ end
167
+
168
+ def to_s
169
+ "#{@login}"
170
+ end
171
+
172
+ private
173
+
174
+ # Generic request method
175
+ def request(strategy, uri, data)
176
+ if strategy == :get
177
+ response = RestClient.get "#{@api_url}/#{uri}/", params: data, accept: 'application/json'
178
+ else
179
+ response = RestClient.send strategy, "#{@api_url}/#{uri}/", data, accept: 'application/json'
180
+ end
181
+ response = JSON.parse response
182
+ require_success! response
183
+ response['data']
184
+ end
185
+
186
+ def session_created?
187
+ @session_key != nil
188
+ end
189
+
190
+ def require_auth!
191
+ raise ApiError, "Must be logged in to log out" unless session_created?
192
+ end
193
+
194
+ def require_success!(response)
195
+ raise ApiError, response['message'] unless response['success']
196
+ response
197
+ end
198
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comagic_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Suvorov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Comagic API client
14
+ email: bluurn@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/comagic_client.rb
20
+ homepage: http://rubygems.org/gems/comagic_client
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Comagic API client
44
+ test_files: []