lacuna 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af46cbe3601f8f84bc8c08ad470e15909b918c4b
4
- data.tar.gz: 1b8ccfe2dd303a96235dbe014cf9d6822a047649
3
+ metadata.gz: cfcb24ca00be1e677b7416e35e97a471c8d9977b
4
+ data.tar.gz: 0dc4970c21acbdfa6de03a368d4756f96a93142c
5
5
  SHA512:
6
- metadata.gz: eccfd26de7a5eb756957d74a13851303cd872264fe4f50a84ebaa9d1ccc1bd260a1c1ae2deef894d53ca64d6745e7c4b029482631a17c048380b64f649b6b37f
7
- data.tar.gz: 3e1d1e3a2fcd71b174470234369f3b24ac91772b9f1083adf9156b90b3911616660b0a2965e9ab905d7f5ea0aa5f21738e5a2eb225841a4b879264e1feacd128
6
+ metadata.gz: b5aba662d10e3ffb5589193e55445c927901fd23504914df8a5578142a184e6a6af393c3fe06110ee5feb984ecc3fe7de36dd404ab316f2fcf5a354c7e983635
7
+ data.tar.gz: 74986f5ff81314826c9e94336d1d0e52098317bb9821b4455495f36a16c37e8449c3b0f1bf4981d8938dab4a680de3d0f3f09942ee11d3f701285bb2c6e53a10
@@ -1,134 +1,57 @@
1
1
  # encoding: utf-8
2
2
 
3
3
 
4
+ $LOAD_PATH.unshift File.dirname __FILE__
5
+
4
6
  require 'net/http'
5
7
  require 'openssl'
6
8
  require 'json'
9
+ require 'require_all'
7
10
 
8
- class Session
9
- @@session = nil
10
-
11
- def self.set(val)
12
- @@session = val
13
- end
14
-
15
- def self.end
16
- self.set nil
17
- end
18
-
19
- def self.get
20
- @@session
21
- end
22
-
23
- def self.valid?
24
- # TODO: take into account the two hour session timeout.
25
- !@@session.nil?
26
- end
27
- end
28
-
29
- class Lacuna
30
- VERSION = '0.0.2'
11
+ require 'lacuna/version'
12
+ require 'lacuna/session'
13
+ require 'lacuna/constants'
14
+ require 'lacuna/module'
31
15
 
32
- API_KEYS = {
33
- # Private key : 66090c68-2d51-47fa-b406-44dc98e6f6d3
34
- 'us1' => 'bbd9b648-6e45-419d-bdaf-5726919c4a64',
35
- # Private key : 3a9c5121-0939-4ef9-82c2-7c1aa4f7d1bc
36
- 'pt' => '3746d4a2-0f44-44db-9308-22a85c234aab',
37
- }
16
+ require_all File.join(File.dirname(__FILE__), 'lacuna', 'extras')
38
17
 
39
- LACUNA_DOMAIN = 'lacunaexpanse.com'
18
+ module Lacuna
40
19
 
41
- def initialize(args)
20
+ def self.connect(args)
42
21
  # Allow a custom server url, be specifying the API key used there but
43
22
  # allow the name of the server (eg: 'us1') to be used, which selects
44
23
  # the API key corresponding to US1.
45
- @api_key =
24
+ @@api_key =
46
25
  if args[:api_key]
47
26
  args[:api_key]
48
27
  elsif args[:server_name] && !args[:server_url]
49
28
  API_KEYS[args[:server_name]]
50
29
  end
51
30
 
52
- @url =
31
+ @@url =
53
32
  if args[:server_url]
54
33
  args[:server_url]
55
34
  elsif args[:server_name]
56
35
  File.join('https://', args[:server_name] + '.' + LACUNA_DOMAIN)
57
36
  end
58
37
 
59
- @args = args
38
+ @@args = args
60
39
 
61
40
  # TODO: allow a sleep-period for each request
62
41
  end
63
42
 
64
- def method_missing(name, *args)
65
- args = @args.merge :api_key => @api_key
66
- LacunaModule.new(@url, name.id2name, args)
67
- end
68
- end
69
-
70
- class LacunaModule
71
-
72
- def initialize(base_url, module_name, args)
73
- @base_url = base_url
74
- @url = base_url
75
- @module_name = module_name
76
- @method_name = 'replace me!'
77
- @args = args
78
- end
79
-
80
- def send(url, post_module, post_method, data)
81
- uri = URI.parse url
82
- http = Net::HTTP.new(uri.host, uri.port)
83
- http.use_ssl = true
84
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
85
-
86
- request = Net::HTTP::Post.new '/' + post_module
87
- request.add_field('Content-Type', 'application/json')
88
-
89
- body = {
90
- :id => 1,
91
- :jsonrpc => '2.0',
92
- :method => post_method,
93
- :params => data
94
- }
95
-
96
- # Include session id in requests that need it
97
- if "#{post_module}/#{post_method}" != 'empire/login'
98
- body[:params].unshift Session.get
99
- end
100
-
101
- request.body = JSON.generate body
102
-
103
- response = http.request request
104
- rv = JSON.parse response.read_body
105
-
106
- # TODO: if !rv['error'].nil? throw an exception
107
- if rv['result']
108
- rv['result']
109
- else
110
- rv
111
- end
112
- end
43
+ @@url = ''
44
+ @@args = {}
45
+ @@api_key = ''
46
+ def self.url; @@url; end
47
+ def self.args; @@args; end
48
+ def self.api_key; @@api_key; end
113
49
 
114
- # Check that we're logged in, if not, get session a set up.
115
- def session_stuff
116
- if !Session.valid?
117
- res = self.send(@base_url, 'empire', 'login', [
118
- @args[:name],
119
- @args[:password],
120
- @args[:api_key],
121
- ])
122
- Session.set res['session_id']
123
- else
124
- # check time
125
- return
126
- end
50
+ class Empire < Lacuna::Extras::Empire
51
+ @module_name = 'empire'
127
52
  end
128
53
 
129
- def method_missing(name, *args)
130
- @method_name = name.id2name
131
- self.session_stuff
132
- self.send(@url, @module_name, @method_name, args)
54
+ class Body < Lacuna::Extras::Body
55
+ @module_name = 'body'
133
56
  end
134
57
  end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ module Lacuna
4
+ API_KEYS = {
5
+ # Private key : 66090c68-2d51-47fa-b406-44dc98e6f6d3
6
+ 'us1' => 'bbd9b648-6e45-419d-bdaf-5726919c4a64',
7
+ # Private key : 3a9c5121-0939-4ef9-82c2-7c1aa4f7d1bc
8
+ 'pt' => '3746d4a2-0f44-44db-9308-22a85c234aab',
9
+ }
10
+
11
+ LACUNA_DOMAIN = 'lacunaexpanse.com'
12
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module Lacuna
4
+ class Extras
5
+ class Body < Lacuna::Module
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module Lacuna
4
+ class Extras
5
+ class Empire < Lacuna::Module
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ module Lacuna
4
+ class Module
5
+ def self.send(url, post_module, post_method, data)
6
+ uri = URI.parse url
7
+ http = Net::HTTP.new(uri.host, uri.port)
8
+ http.use_ssl = true
9
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
10
+
11
+ request = Net::HTTP::Post.new '/' + post_module
12
+ request.add_field('Content-Type', 'application/json')
13
+
14
+ body = {
15
+ :id => 1,
16
+ :jsonrpc => '2.0',
17
+ :method => post_method,
18
+ :params => data
19
+ }
20
+
21
+ # Include session id in requests that need it
22
+ if "#{post_module}/#{post_method}" != 'empire/login'
23
+ body[:params].unshift Session.get
24
+ end
25
+
26
+ request.body = JSON.generate body
27
+
28
+ response = http.request request
29
+ rv = JSON.parse response.read_body
30
+
31
+ if rv['result']
32
+ rv['result']
33
+ elsif rv['error']
34
+ # TODO: throw error?
35
+ p rv['error']
36
+ rv['error']
37
+ else
38
+ rv
39
+ end
40
+ end
41
+
42
+ # Check that we're logged in, if not, get session a set up.
43
+ def self.session_stuff
44
+ if !Session.valid?
45
+ res = self.send(Lacuna.url, 'empire', 'login', [
46
+ Lacuna.args[:name],
47
+ Lacuna.args[:password],
48
+ Lacuna.api_key,
49
+ ])
50
+ Session.set res['session_id']
51
+ else
52
+ # check time
53
+ return
54
+ end
55
+ end
56
+
57
+ def self.method_missing(name, *args)
58
+ if !@module_name.nil?
59
+ self.session_stuff
60
+ self.send(Lacuna.url, @module_name, name.id2name, args)
61
+ else
62
+ puts "#{self} isn't working!"
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ class Session
4
+ @@session = nil
5
+
6
+ def self.set(val)
7
+ @@session = val
8
+ end
9
+
10
+ def self.end
11
+ self.set nil
12
+ end
13
+
14
+ def self.get
15
+ @@session
16
+ end
17
+
18
+ def self.valid?
19
+ # TODO: take into account the two hour session timeout.
20
+ !@@session.nil?
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module Lacuna
4
+ VERSION = '0.0.3'
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lacuna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan McCallum
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2010-05-30 00:00:00.000000000 Z
11
+ date: 2010-06-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Lacuna is a Ruby library for using the API of The Lacuna Expanse.
14
14
  email: nmccallum7@gmail.com
@@ -17,6 +17,12 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/lacuna.rb
20
+ - lib/lacuna/constants.rb
21
+ - lib/lacuna/extras/body.rb
22
+ - lib/lacuna/extras/empire.rb
23
+ - lib/lacuna/module.rb
24
+ - lib/lacuna/session.rb
25
+ - lib/lacuna/version.rb
20
26
  homepage: http://rubygems.org/gems/lacuna
21
27
  licenses:
22
28
  - MIT