ahub 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ahub/answer.rb +2 -3
- data/lib/ahub/group.rb +1 -2
- data/lib/ahub/modules/api_resource.rb +90 -0
- data/lib/ahub/question.rb +1 -2
- data/lib/ahub/space.rb +1 -2
- data/lib/ahub/topic.rb +1 -2
- data/lib/ahub/user.rb +5 -10
- data/lib/ahub/version.rb +1 -1
- data/lib/ahub.rb +1 -3
- metadata +3 -4
- data/lib/ahub/modules/api_helpers.rb +0 -60
- data/lib/ahub/modules/class_helpers.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f15ac34fc04f78ba2a17031596dfb90dccbbf567
|
4
|
+
data.tar.gz: 98c3444ab17ff20dd792105d1aa8ed58f8b7b1f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3be66373cb10dfeaf82b7adb93387bd174beb8b2a61ff225d58b3d0b854386719641e98d7ca35510141b6ccab95b9f9c0571b5166347a2ef1953ae9a44d981f
|
7
|
+
data.tar.gz: 3762549149f4338b191ad195ff39ac736329482b70b4194ca8e6a4be0fefdc61231a126321c32898542811292e4a1330f05461140a32870a86f35f2f432b2352
|
data/lib/ahub/answer.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Ahub
|
2
2
|
class Answer
|
3
|
-
|
4
|
-
include Ahub::ClassHelpers
|
3
|
+
include Ahub::APIResource
|
5
4
|
|
6
5
|
def self.create(question_id:, body:, username:, password:)
|
7
6
|
url = "#{Ahub::DOMAIN}/services/v2/question/#{question_id}/answer.json"
|
@@ -10,7 +9,7 @@ module Ahub
|
|
10
9
|
end
|
11
10
|
|
12
11
|
def initialize(attrs)
|
13
|
-
super
|
12
|
+
super
|
14
13
|
@author = Ahub::User.new(attrs[:author])
|
15
14
|
end
|
16
15
|
|
data/lib/ahub/group.rb
CHANGED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module Ahub
|
6
|
+
module APIResource
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
end
|
11
|
+
|
12
|
+
def update
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
def destroy
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(attrs)
|
21
|
+
attrs.each_pair do |k,v|
|
22
|
+
self.instance_variable_set("@#{k.to_s.underscore}", v)
|
23
|
+
|
24
|
+
self.class.send(:define_method, k.to_s.underscore.to_sym) do
|
25
|
+
instance_variable_get("@#{__method__}")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class_methods do
|
31
|
+
def headers(username:'answerhub', password:'answerhub')
|
32
|
+
encoded = "Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
|
33
|
+
|
34
|
+
{
|
35
|
+
'Authorization' => encoded,
|
36
|
+
'Accept' => "application/json",
|
37
|
+
'Content-type' => "application/json",
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def admin_headers
|
42
|
+
headers(username: Ahub::ADMIN_USER, password: Ahub::ADMIN_PASS)
|
43
|
+
end
|
44
|
+
|
45
|
+
def find(id)
|
46
|
+
url = "#{base_url}/#{id}.json"
|
47
|
+
|
48
|
+
new get_resource(url: url, headers:admin_headers)
|
49
|
+
rescue RestClient::ResourceNotFound => e
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_all(params: nil, page: 1, pageSize: 30)
|
54
|
+
url = "#{base_url}.json?page=#{page}&pageSize=#{pageSize}"
|
55
|
+
|
56
|
+
if params
|
57
|
+
params.each{|k,v| url << "&#{k}=#{URI.encode(v)}"}
|
58
|
+
end
|
59
|
+
|
60
|
+
get_resources(url: url, headers: admin_headers, klass: self)
|
61
|
+
end
|
62
|
+
|
63
|
+
def base_url
|
64
|
+
class_name = name.gsub(/Ahub::/, '').downcase
|
65
|
+
"#{Ahub::DOMAIN}/services/v2/#{class_name}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def object_id_from_response(response)
|
69
|
+
response.headers[:location].match(/(?<id>\d*)\.json/)[:id].to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_resource(url:, headers:)
|
73
|
+
JSON.parse(RestClient.get(url, admin_headers), symbolize_names:true)
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_resources(url:, headers:, klass:)
|
77
|
+
JSON.parse(RestClient.get(url, admin_headers), symbolize_names:true)[:list].map do |node|
|
78
|
+
klass.new(node)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def create_resource(url:, payload:, headers:)
|
85
|
+
response = RestClient.post(url, payload.to_json, headers)
|
86
|
+
find(object_id_from_response(response))
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/ahub/question.rb
CHANGED
data/lib/ahub/space.rb
CHANGED
data/lib/ahub/topic.rb
CHANGED
data/lib/ahub/user.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Ahub
|
2
2
|
class User
|
3
|
-
|
4
|
-
include Ahub::ClassHelpers
|
3
|
+
include Ahub::APIResource
|
5
4
|
|
6
5
|
def self.create(username:, email:, password:nil)
|
7
6
|
url = "#{base_url}.json"
|
@@ -25,14 +24,10 @@ module Ahub
|
|
25
24
|
@groups = attrs[:groups].map{|group| Ahub::Group.new(group)} if attrs[:groups]
|
26
25
|
end
|
27
26
|
|
28
|
-
def is_complete?
|
29
|
-
!!@complete
|
30
|
-
end
|
31
|
-
|
32
27
|
def questions
|
33
28
|
unless @questions
|
34
|
-
|
35
|
-
@questions =
|
29
|
+
url = "#{self.class.base_url}/#{id}/question.json"
|
30
|
+
@questions = self.class.get_resources(url: url, headers: self.class.admin_headers, klass: Ahub::Question)
|
36
31
|
end
|
37
32
|
|
38
33
|
@questions
|
@@ -40,8 +35,8 @@ module Ahub
|
|
40
35
|
|
41
36
|
def answers
|
42
37
|
unless @answers
|
43
|
-
|
44
|
-
@answers =
|
38
|
+
url = "#{self.class.base_url}/#{id}/answer.json"
|
39
|
+
@answers = self.class.get_resources(url: url, headers: self.class.admin_headers, klass: Ahub::Answer)
|
45
40
|
end
|
46
41
|
|
47
42
|
@answers
|
data/lib/ahub/version.rb
CHANGED
data/lib/ahub.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'dotenv'
|
2
2
|
require 'rest_client'
|
3
|
-
require '
|
4
|
-
require 'ahub/modules/api_helpers'
|
5
|
-
require 'ahub/modules/class_helpers'
|
3
|
+
require 'ahub/modules/api_resource'
|
6
4
|
require 'ahub/version'
|
7
5
|
require 'ahub/user'
|
8
6
|
require 'ahub/question'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ahub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abel Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -133,8 +133,7 @@ files:
|
|
133
133
|
- lib/ahub.rb
|
134
134
|
- lib/ahub/answer.rb
|
135
135
|
- lib/ahub/group.rb
|
136
|
-
- lib/ahub/modules/
|
137
|
-
- lib/ahub/modules/class_helpers.rb
|
136
|
+
- lib/ahub/modules/api_resource.rb
|
138
137
|
- lib/ahub/question.rb
|
139
138
|
- lib/ahub/space.rb
|
140
139
|
- lib/ahub/topic.rb
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module Ahub
|
2
|
-
module APIHelpers
|
3
|
-
require 'base64'
|
4
|
-
|
5
|
-
def headers(username:'answerhub', password:'answerhub')
|
6
|
-
encoded = "Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
|
7
|
-
|
8
|
-
{
|
9
|
-
'Authorization' => encoded,
|
10
|
-
'Accept' => "application/json",
|
11
|
-
'Content-type' => "application/json",
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
|
-
def admin_headers
|
16
|
-
headers(username: Ahub::ADMIN_USER, password: Ahub::ADMIN_PASS)
|
17
|
-
end
|
18
|
-
|
19
|
-
def find(id)
|
20
|
-
url = "#{base_url}/#{id}.json"
|
21
|
-
|
22
|
-
new get_resource(url: url, headers:admin_headers)
|
23
|
-
rescue RestClient::ResourceNotFound => e
|
24
|
-
nil
|
25
|
-
end
|
26
|
-
|
27
|
-
def find_all(params: nil, page: 1, pageSize: 30)
|
28
|
-
url = "#{base_url}.json?page=#{page}&pageSize=#{pageSize}"
|
29
|
-
|
30
|
-
if params
|
31
|
-
params.each{|k,v| url << "&#{k}=#{URI.encode(v)}"}
|
32
|
-
end
|
33
|
-
|
34
|
-
get_resource(url: url, headers: admin_headers)[:list].map do |node|
|
35
|
-
new(node)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def base_url
|
40
|
-
class_name = name.gsub(/Ahub::/, '').downcase
|
41
|
-
"#{Ahub::DOMAIN}/services/v2/#{class_name}"
|
42
|
-
end
|
43
|
-
|
44
|
-
def object_id_from_response(response)
|
45
|
-
response.headers[:location].match(/(?<id>\d*)\.json/)[:id].to_i
|
46
|
-
end
|
47
|
-
|
48
|
-
def get_resource(url:, headers:)
|
49
|
-
JSON.parse(RestClient.get(url, admin_headers), symbolize_names:true)
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def create_resource(url:, payload:, headers:)
|
55
|
-
response = RestClient.post(url, payload.to_json, headers)
|
56
|
-
find(object_id_from_response(response))
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Ahub
|
2
|
-
module ClassHelpers
|
3
|
-
def update
|
4
|
-
raise NotImplementedError
|
5
|
-
end
|
6
|
-
|
7
|
-
def destroy
|
8
|
-
raise NotImplementedError
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize(attrs)
|
12
|
-
attrs.each_pair do |k,v|
|
13
|
-
self.instance_variable_set("@#{k.to_s.underscore}", v)
|
14
|
-
|
15
|
-
self.class.send(:define_method, k.to_s.underscore.to_sym) do
|
16
|
-
instance_variable_get("@#{__method__}")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|