purple_ai_wifi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/purple_wifi.rb +115 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2dd07d9513a776bbb200ae8f1fb18c83fda47eca
|
4
|
+
data.tar.gz: a0bb469b59eda2df75dd405a04430123bb2182e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3694e280281b4c84d642e770f25ffd424f47234013700c517df44d8b05e4c5f8184e5667acaf3a7fa4ed9e2ef2025369d90278d832b8ecd4e7940bf876c7bd88
|
7
|
+
data.tar.gz: 9a8d25883fff97114757451be8c1979ffd04b99f6efb3b7b23daf8578c981a337f37aa979c785bf7cfe21b6b30b402748aaac3fa3f6723bc2b0d9e0421eb5886
|
data/lib/purple_wifi.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# Dependencies
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
require 'openssl'
|
5
|
+
require 'time'
|
6
|
+
require 'uri'
|
7
|
+
class PurpleWifi
|
8
|
+
# Class attributes
|
9
|
+
attr_accessor :public_key, :secret_key, :domain, :target, :venue, :date_from, :date_to
|
10
|
+
def initialize(domain, public_key, secret_key, target = :venues)
|
11
|
+
|
12
|
+
@domain = domain
|
13
|
+
@public_key = public_key
|
14
|
+
@secret_key = secret_key
|
15
|
+
@target = target
|
16
|
+
|
17
|
+
@venue = venue
|
18
|
+
@date_from = date_from
|
19
|
+
@date_to = date_to
|
20
|
+
|
21
|
+
@request_time = Time.now.httpdate
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_url
|
25
|
+
if @target == 'venue'
|
26
|
+
"https://#{@domain}/api/company/v1/venues"
|
27
|
+
elsif @target == 'visitor'
|
28
|
+
# If the user HASN'T passed the from/to dates, then the last part of the request url will be missing:
|
29
|
+
url_end = ''
|
30
|
+
|
31
|
+
# If the user HAS passed the from/to dates, then this creates the last part of the request url:
|
32
|
+
if @date_from != '' && @date_to != ''
|
33
|
+
url_end = "?from=#{@date_from}&to=#{@date_to}"
|
34
|
+
end
|
35
|
+
"https://#{@domain}/api/company/v1/venue/#{venue}/visitors#{url_end}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def generate_signature
|
40
|
+
line1 = "application/json"
|
41
|
+
line2 = "#{@domain}"
|
42
|
+
if @target == 'venue'
|
43
|
+
line3 = "/api/company/v1/venues"
|
44
|
+
elsif @target == 'visitor'
|
45
|
+
if @date_from == '' && @date_to == ''
|
46
|
+
line3 = "/api/company/v1/venue/#{@venue}/visitors"
|
47
|
+
elsif @date_from != '' && @date_to != ''
|
48
|
+
line3 = "/api/company/v1/venue/#{@venue}/visitors?from=#{@date_from}&to=#{@date_to}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
line4 = @request_time
|
52
|
+
[line1, line2, line3, line4].join("\n") + "\n\n"
|
53
|
+
#"#{line1}\r\n#{line2}\r\n#{line3}\r\n#{line4}\r\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_hash
|
57
|
+
OpenSSL::HMAC.hexdigest('SHA256', @secret_key, generate_signature)
|
58
|
+
#OpenSSL::HMAC.hexdigest('SHA256', @secret_key, generate_signature)
|
59
|
+
end
|
60
|
+
|
61
|
+
def generate_header
|
62
|
+
{
|
63
|
+
'Content-Type' => 'application/json',
|
64
|
+
'Content-Length' => '0',
|
65
|
+
'Date' => @request_time,
|
66
|
+
'X-API-Authorization' => @public_key + ':' + generate_hash
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def venues_json
|
71
|
+
# Returns a json object containing full details of all venues in the Purple instance.
|
72
|
+
@target = 'venue'
|
73
|
+
uri = URI.parse(generate_url)
|
74
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
75
|
+
http.use_ssl = true
|
76
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
77
|
+
request.initialize_http_header(generate_header)
|
78
|
+
response = http.request(request)
|
79
|
+
JSON.parse(response.body)
|
80
|
+
end
|
81
|
+
|
82
|
+
def venues
|
83
|
+
if venues_json['success']
|
84
|
+
venue_json = venues_json
|
85
|
+
end
|
86
|
+
|
87
|
+
for venue in 0...venue_json['data']['venues'].size
|
88
|
+
names = [venue_json['data']['venues'][venue]['name']]
|
89
|
+
end
|
90
|
+
for venue in 0...venue_json['data']['venues'].size
|
91
|
+
ids = [venue_json['data']['venues'][venue]['id']]
|
92
|
+
end
|
93
|
+
|
94
|
+
venue_dict = {}
|
95
|
+
for venue in 0...venue_json['data']['venues'].size
|
96
|
+
venue_dict[venue] = names[venue], ids[venue]
|
97
|
+
end
|
98
|
+
venue_dict
|
99
|
+
end
|
100
|
+
|
101
|
+
def visitor_json(venue, date_from = '', date_to = '')
|
102
|
+
@target = 'visitor'
|
103
|
+
@venue = venue
|
104
|
+
@date_from = date_from
|
105
|
+
@date_to = date_to
|
106
|
+
uri = URI.parse(generate_url)
|
107
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
108
|
+
http.use_ssl = true
|
109
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
110
|
+
request.initialize_http_header(generate_header)
|
111
|
+
response = http.request(request)
|
112
|
+
JSON.parse(response.body)
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: purple_ai_wifi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renzo Diaz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem for retrieve data from purple.ai API
|
14
|
+
email: dev.renzo.diaz@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/purple_wifi.rb
|
20
|
+
homepage: https://rubygems.org/gem/purple_ai_wifi
|
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.6.13
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Get visits, venues, visitors
|
44
|
+
test_files: []
|