visiquery 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 831931af26d5b4fd59084d25981c1a67dba82d5e
4
+ data.tar.gz: 3136537b1324602808c49fa5125d3acd58246091
5
+ SHA512:
6
+ metadata.gz: 6ad13ed47dfa02c5cc5731cd5d6479e4aed6995b8901429a92069ff9830ebd073958099dc168739be2c2c65ebc6d16fe0f350dd3d4b11e7cba666770b743cc83
7
+ data.tar.gz: 82db6dfdfbb47130547b3086e411bec82dc00a299c10935010af235234e1e268b31690031cb1b9aaf49aa459c08999c1435efeb2a0f5f513de9a6f43af8ff9d4
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'oj'
4
+ require 'uri'
5
+ require 'date'
6
+ require 'bundler'
7
+ require 'net/http'
8
+ require 'multi_json'
9
+
10
+ require 'visiquery/user'
11
+ require 'visiquery/client'
12
+ require 'visiquery/ban-reason'
13
+
14
+ Bundler.setup :default
15
+
16
+ module VisiQuery
17
+ Version = "0.1.0"
18
+
19
+ class VisiBaseError < StandardError; end
20
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module VisiQuery
4
+ class BanReason
5
+ attr_accessor :reason, :comment
6
+ attr_accessor :updated_at, :created_at, :expires_at
7
+
8
+ def self.from_json body
9
+ BanReason.new(body['user_name'], body['profile_id']).tap do |ban|
10
+ ban.reason = body['reason']
11
+ ban.comment = body['comment']
12
+
13
+ ban.created_at = DateTime.parse body['created_at']
14
+ ban.updated_at = DateTime.parse body['updated_at']
15
+
16
+ if body['expires_at']
17
+ ban.expires_at = DateTime.parse body['expires_at']
18
+ end
19
+ end
20
+ end
21
+
22
+ def initialize name, id
23
+ @id = id
24
+ @name = name
25
+ end
26
+
27
+ def to_hash
28
+ Hash.new.tap do |result|
29
+ result['profile_id'] = @id
30
+ result['user_name'] = @name if @name
31
+
32
+ result['reason'] = reason if reason
33
+ result['comment'] = comment if comment
34
+ result['expires_at'] = expires_at if expires_at
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ module VisiQuery
4
+ class Client
5
+ VisiBaseHost = "visibase.uplink.io"
6
+ VisiBasePort = 80
7
+
8
+ def initialize host = VisiBaseHost, port = VisiBasePort
9
+ @http = Net::HTTP.new host, port
10
+ @http.start
11
+ end
12
+
13
+ # Get a user by instance id, profile id, or username.
14
+ def user identifier
15
+ response = @http.get "/users/#{URI.escape identifier}.json"
16
+
17
+ if response.code.to_i == 200
18
+ body = ::MultiJson.load response.body
19
+
20
+ if message = body['error']
21
+ raise VisiBaseError, message
22
+ else
23
+ user = User.from_json body['accumulated']
24
+ end
25
+ end
26
+ end
27
+
28
+ # Get a ban reason by instance id, profile id, or username.
29
+ def ban_reason identifier
30
+ response = @http.get "/bans/#{URI.escape identifier}.json"
31
+
32
+ if response.code.to_i == 200
33
+ body = ::MultiJson.load response.body
34
+
35
+ if message = body['error']
36
+ raise VisiBaseError, message
37
+ else
38
+ user = BanReason.from_json body['accumulated']
39
+ end
40
+ end
41
+ end
42
+
43
+ # Publish changes to an object.
44
+ def publish object
45
+ case object
46
+ when User
47
+ publish_to '/users/create.json', :user, object
48
+ when BanReason
49
+ publish_to '/bans/create.json', :ban, object
50
+ end
51
+ end
52
+
53
+ def close
54
+ @http.finish
55
+ end
56
+
57
+ private
58
+
59
+ def publish_to path, param, object
60
+ response = @http.post path, url_encode(param, object)
61
+
62
+ if response.code.to_i == 200
63
+ body = ::MultiJson.dump response.body
64
+
65
+ unless body['error']
66
+ return object
67
+ else
68
+ raise VisiBaseError, body['error']
69
+ end
70
+ else
71
+ raise VisiBaseError, "Server returned #{response.code} #{response.message}"
72
+ end
73
+ end
74
+
75
+ def url_encode param, object
76
+ URI.encode_www_form object.to_hash.map { |k, v| ["#{param}[#{k}]", v] }
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ module VisiQuery
4
+ class User
5
+ Attributes = %w{real_name profile_text hours age facebook_name skype_name website residence}
6
+
7
+ attr_accessor :id, :name
8
+ attr_accessor :created_at, :updated_at, :first_login, :last_login
9
+ Attributes.each &method(:attr_accessor)
10
+
11
+ def self.from_json body
12
+ User.new(body['name'], body['profile_id']).tap do |user|
13
+ Attributes.each do |attribute|
14
+ user.instance_variable_set :"@#{attribute}", body[attribute]
15
+ end
16
+
17
+ user.created_at = DateTime.parse body['created_at']
18
+ user.updated_at = DateTime.parse body['updated_at']
19
+
20
+ if body['last_login']
21
+ user.last_login = DateTime.parse body['last_login']
22
+ end
23
+
24
+ if body['first_login']
25
+ user.first_login = DateTime.parse body['first_login']
26
+ end
27
+ end
28
+ end
29
+
30
+ def initialize name, id
31
+ @id = id.to_i
32
+ @name = name
33
+ end
34
+
35
+ RejectedVariables = %w{@created_at @updated_at}
36
+
37
+ def to_hash
38
+ variables = instance_variables.dup
39
+ variables.delete_if { |variable| RejectedVariables.include? variable.to_s }
40
+
41
+ Hash.new.tap do |result|
42
+ variables.each do |variable|
43
+ value = instance_variable_get variable
44
+
45
+ result[variable.to_s[1..-1]] = value if value
46
+ end
47
+
48
+ result['name'] = @name if @name
49
+ result['profile_id'] = @id
50
+ end
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visiquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikkel Kroman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2
42
+ No description.
43
+ email: mk@uplink.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - library/visiquery.rb
49
+ - library/visiquery/user.rb
50
+ - library/visiquery/client.rb
51
+ - library/visiquery/ban-reason.rb
52
+ homepage:
53
+ licenses:
54
+ - ISC
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - library
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.0.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: No summary.
76
+ test_files: []