chatspry 0.0.1.pre1 → 0.0.1.pre2
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.
- data/.rspec +2 -0
- data/Gemfile +15 -1
- data/Rakefile +3 -1
- data/chatspry.gemspec +5 -1
- data/lib/chatspry/authentication.rb +30 -0
- data/lib/chatspry/client/user.rb +12 -0
- data/lib/chatspry/client.rb +106 -0
- data/lib/chatspry/configurable.rb +71 -0
- data/lib/chatspry/default.rb +81 -0
- data/lib/chatspry/version.rb +1 -1
- data/lib/chatspry.rb +23 -1
- data/spec/cassettes/Chatspry_Client/_get/handles_query_params.json +1 -0
- data/spec/cassettes/Chatspry_Client/_head/handles_query_params.json +1 -0
- data/spec/cassettes/Chatspry_Client/authentication/when_token_authenticated/fetches_and_memorizes_identifier.json +1 -0
- data/spec/chatspry/client_spec.rb +301 -0
- data/spec/chatspry_spec.rb +48 -0
- data/spec/helper.rb +35 -0
- data/spec/support/environment_defaults.rb +19 -0
- data/spec/support/fixture_configuration.rb +72 -0
- data/spec/support/request_stubs.rb +27 -0
- metadata +81 -3
data/.rspec
ADDED
data/Gemfile
CHANGED
@@ -1,2 +1,16 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
group :development, :test do
|
4
|
+
gem "pry"
|
5
|
+
end
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem "json"
|
9
|
+
gem "multi_json"
|
10
|
+
gem "vcr"
|
11
|
+
gem "webmock"
|
12
|
+
gem "mime-types"
|
13
|
+
gem "rspec"
|
14
|
+
end
|
15
|
+
|
2
16
|
gemspec
|
data/Rakefile
CHANGED
data/chatspry.gemspec
CHANGED
@@ -25,7 +25,11 @@ Gem::Specification.new do |spec|
|
|
25
25
|
|
26
26
|
spec.require_paths = [ "lib" ]
|
27
27
|
|
28
|
-
spec.
|
28
|
+
spec.add_dependency "faraday", "~> 0.9"
|
29
|
+
spec.add_dependency "hashie", ">= 2.0", "< 3.0"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0.0"
|
29
33
|
spec.add_development_dependency "rake"
|
30
34
|
|
31
35
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Chatspry
|
2
|
+
|
3
|
+
module Authentication
|
4
|
+
|
5
|
+
def basic_authenticated?
|
6
|
+
!!(@identifier && @passphrase)
|
7
|
+
end
|
8
|
+
|
9
|
+
def token_authenticated?
|
10
|
+
!!@access_token
|
11
|
+
end
|
12
|
+
|
13
|
+
def user_authenticated?
|
14
|
+
basic_authenticated? || token_authenticated?
|
15
|
+
end
|
16
|
+
|
17
|
+
def application_authenticated?
|
18
|
+
!!application_authentication
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def application_authentication
|
24
|
+
if @client_id && @client_secret
|
25
|
+
{ client_id: @client_id, client_secret: @client_secret }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "faraday_middleware"
|
3
|
+
require "chatspry/configurable"
|
4
|
+
require "chatspry/authentication"
|
5
|
+
require "chatspry/client/user"
|
6
|
+
|
7
|
+
require "pry"
|
8
|
+
|
9
|
+
module Chatspry
|
10
|
+
|
11
|
+
class Client
|
12
|
+
|
13
|
+
include Chatspry::Authentication
|
14
|
+
include Chatspry::Configurable
|
15
|
+
include Chatspry::Client::User
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
Chatspry::Configurable.keys.each do |key|
|
19
|
+
instance_variable_set(:"@#{key}", options[key] || Chatspry.instance_variable_get(:"@#{key}"))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def same_options?(opts)
|
24
|
+
opts.hash == options.hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
string = ["<Chatspry::Client:"]
|
29
|
+
string << "identifier: #{ @identifier }" if @identifier
|
30
|
+
string << "passphrase: #{ "*******" }" if @passphrase
|
31
|
+
string << "access_token: #{'*'*56}#{@access_token[56..-1] }" if @access_token
|
32
|
+
string << "client_secret: #{ '*'*36 }#{@client_secret[36..-1] }" if @client_secret
|
33
|
+
string = string.join " "
|
34
|
+
string.concat ">"
|
35
|
+
end
|
36
|
+
|
37
|
+
def connection
|
38
|
+
@connection ||= Faraday.new(faraday_options) do |conn|
|
39
|
+
|
40
|
+
conn.headers[:accept] = default_media_type
|
41
|
+
conn.headers[:user_agent] = user_agent
|
42
|
+
|
43
|
+
if token_authenticated?
|
44
|
+
conn.authorization "Bearer", @access_token
|
45
|
+
elsif application_authenticated?
|
46
|
+
conn.params = conn.params.merge application_authentication
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def get(url, options = {})
|
54
|
+
request :get, url, options
|
55
|
+
end
|
56
|
+
|
57
|
+
def head(url, options = {})
|
58
|
+
request :head, url, options
|
59
|
+
end
|
60
|
+
|
61
|
+
def post(url, options = {})
|
62
|
+
request :post, url, options
|
63
|
+
end
|
64
|
+
|
65
|
+
def put(url, options = {})
|
66
|
+
request :put, url, options
|
67
|
+
end
|
68
|
+
|
69
|
+
def patch(url, options = {})
|
70
|
+
request :patch, url, options
|
71
|
+
end
|
72
|
+
|
73
|
+
def delete(url, options = {})
|
74
|
+
request :delete, url, options
|
75
|
+
end
|
76
|
+
|
77
|
+
def request(method, path, options = {})
|
78
|
+
headers = options.delete(:headers) || {}
|
79
|
+
if accept = options.delete(:accept)
|
80
|
+
headers[:accept] = accept
|
81
|
+
end
|
82
|
+
|
83
|
+
query = options.delete(:query) || {}
|
84
|
+
query = options.merge(query)
|
85
|
+
|
86
|
+
@last_response = response = connection.send(method, URI::Parser.new.escape(path.to_s), query, headers)
|
87
|
+
response.body
|
88
|
+
end
|
89
|
+
|
90
|
+
def last_response
|
91
|
+
@last_response if defined? @last_response
|
92
|
+
end
|
93
|
+
|
94
|
+
def faraday_options
|
95
|
+
opts = connection_options
|
96
|
+
|
97
|
+
opts[:builder] = middleware if middleware
|
98
|
+
opts[:proxy] = proxy if proxy
|
99
|
+
opts[:url] = api_endpoint
|
100
|
+
|
101
|
+
return opts
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Chatspry
|
2
|
+
|
3
|
+
module Configurable
|
4
|
+
|
5
|
+
attr_accessor :access_token, :client_id, :client_secret,
|
6
|
+
:default_media_type, :connection_options,
|
7
|
+
:middleware, :user_agent, :proxy
|
8
|
+
|
9
|
+
attr_writer :identifier, :passphrase, :web_endpoint, :api_endpoint
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def keys
|
14
|
+
@keys ||= [
|
15
|
+
:access_token,
|
16
|
+
:client_id,
|
17
|
+
:client_secret,
|
18
|
+
:identifier,
|
19
|
+
:passphrase,
|
20
|
+
|
21
|
+
:web_endpoint,
|
22
|
+
:api_endpoint,
|
23
|
+
|
24
|
+
:default_media_type,
|
25
|
+
:connection_options,
|
26
|
+
|
27
|
+
:middleware,
|
28
|
+
:user_agent,
|
29
|
+
:proxy
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def configure
|
35
|
+
yield self
|
36
|
+
end
|
37
|
+
|
38
|
+
def reset!
|
39
|
+
Chatspry::Configurable.keys.each do |key|
|
40
|
+
instance_variable_set(:"@#{key}", Chatspry::Default.options[key])
|
41
|
+
end
|
42
|
+
self
|
43
|
+
end
|
44
|
+
alias setup reset!
|
45
|
+
|
46
|
+
def api_endpoint
|
47
|
+
File.join(@api_endpoint, "")
|
48
|
+
end
|
49
|
+
|
50
|
+
def web_endpoint
|
51
|
+
File.join(@web_endpoint, "")
|
52
|
+
end
|
53
|
+
|
54
|
+
def identifier
|
55
|
+
@identifier ||= begin
|
56
|
+
user.handle if token_authenticated?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def passphrase
|
61
|
+
@passphrase
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def options
|
67
|
+
Hash[Chatspry::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "chatspry/version"
|
2
|
+
|
3
|
+
module Chatspry
|
4
|
+
|
5
|
+
module Default
|
6
|
+
|
7
|
+
API_ENDPOINT = "http://api.chatspry.dev:8080".freeze
|
8
|
+
WEB_ENDPOINT = "http://chatspry.dev:4200".freeze
|
9
|
+
|
10
|
+
MEDIA_TYPE = "application/json".freeze
|
11
|
+
USER_AGENT = "Chatspry Ruby Gem #{ Chatspry::VERSION }".freeze
|
12
|
+
|
13
|
+
RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
|
14
|
+
|
15
|
+
MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
|
16
|
+
builder.adapter Faraday.default_adapter
|
17
|
+
builder.request :json
|
18
|
+
|
19
|
+
builder.response :mashify
|
20
|
+
builder.response :json, content_type: /\bjson$/
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
|
25
|
+
def options
|
26
|
+
Hash[Chatspry::Configurable.keys.map{|key| [key, send(key)]}]
|
27
|
+
end
|
28
|
+
|
29
|
+
def access_token
|
30
|
+
ENV["CHATSPRY_ACCESS_TOKEN"]
|
31
|
+
end
|
32
|
+
|
33
|
+
def api_endpoint
|
34
|
+
ENV["CHATSPRY_API_ENDPOINT"] || API_ENDPOINT
|
35
|
+
end
|
36
|
+
|
37
|
+
def web_endpoint
|
38
|
+
ENV["CHATSPRY_WEB_ENDPOINT"] || WEB_ENDPOINT
|
39
|
+
end
|
40
|
+
|
41
|
+
def client_id
|
42
|
+
ENV["CHATSPRY_CLIENT_ID"]
|
43
|
+
end
|
44
|
+
|
45
|
+
def client_secret
|
46
|
+
ENV["CHATSPRY_SECRET"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def default_media_type
|
50
|
+
ENV["CHATSPRY_DEFAULT_MEDIA_TYPE"] || MEDIA_TYPE
|
51
|
+
end
|
52
|
+
|
53
|
+
def identifier
|
54
|
+
ENV["CHATSPRY_LOGIN"]
|
55
|
+
end
|
56
|
+
|
57
|
+
def passphrase
|
58
|
+
ENV["CHATSPRY_PASSPHRASE"]
|
59
|
+
end
|
60
|
+
|
61
|
+
def user_agent
|
62
|
+
ENV["CHATSPRY_USER_AGENT"] || USER_AGENT
|
63
|
+
end
|
64
|
+
|
65
|
+
def proxy
|
66
|
+
ENV["CHATSPRY_PROXY"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def connection_options
|
70
|
+
{ headers: { accept: default_media_type, user_agent: user_agent } }
|
71
|
+
end
|
72
|
+
|
73
|
+
def middleware
|
74
|
+
MIDDLEWARE
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/lib/chatspry/version.rb
CHANGED
data/lib/chatspry.rb
CHANGED
@@ -1,4 +1,26 @@
|
|
1
|
-
require "chatspry/
|
1
|
+
require "chatspry/client"
|
2
|
+
require "chatspry/default"
|
2
3
|
|
3
4
|
module Chatspry
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
include Chatspry::Configurable
|
9
|
+
|
10
|
+
def client
|
11
|
+
@client = Chatspry::Client.new(options) unless defined?(@client) && @client.same_options?(options)
|
12
|
+
@client
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def method_missing(method_name, *args, &block)
|
18
|
+
return super unless client.respond_to?(method_name)
|
19
|
+
client.send(method_name, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
4
24
|
end
|
25
|
+
|
26
|
+
Chatspry.setup
|
@@ -0,0 +1 @@
|
|
1
|
+
{"http_interactions":[{"request":{"method":"get","uri":"http://api.chatspry.dev:8080/?foo=bar","body":{"encoding":"US-ASCII","base64_string":""},"headers":{"Accept":["application/json"],"User-Agent":["Chatspry Ruby Gem 0.0.1.pre1"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":404,"message":"Not Found"},"headers":{"Date":["Wed, 18 Jun 2014 19:14:31 GMT"],"Status":["404 Not Found"],"Connection":["close"],"X-Frame-Options":["SAMEORIGIN"],"X-Xss-Protection":["1; mode=block"],"X-Content-Type-Options":["nosniff"],"Content-Type":["text/plain"],"Cache-Control":["no-cache"],"X-Request-Id":["b8fd7b27-8285-4f5c-83f2-ee46c7338393"],"X-Runtime":["0.009932"]},"body":{"encoding":"US-ASCII","base64_string":"IA==\n"},"http_version":null},"recorded_at":"Wed, 18 Jun 2014 19:14:31 GMT"}],"recorded_with":"VCR 2.9.2"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"http_interactions":[{"request":{"method":"head","uri":"http://api.chatspry.dev:8080/?foo=bar","body":{"encoding":"US-ASCII","base64_string":""},"headers":{"Accept":["application/json"],"User-Agent":["Chatspry Ruby Gem 0.0.1.pre1"]}},"response":{"status":{"code":404,"message":"Not Found"},"headers":{"Date":["Wed, 18 Jun 2014 19:14:31 GMT"],"Status":["404 Not Found"],"Connection":["close"],"X-Frame-Options":["SAMEORIGIN"],"X-Xss-Protection":["1; mode=block"],"X-Content-Type-Options":["nosniff"],"Content-Type":["text/plain"],"Cache-Control":["no-cache"],"X-Request-Id":["3e2f3155-3f8d-4172-a3a6-794cc8c11e71"],"X-Runtime":["0.009586"]},"body":{"encoding":"US-ASCII","base64_string":""},"http_version":null},"recorded_at":"Wed, 18 Jun 2014 19:14:31 GMT"}],"recorded_with":"VCR 2.9.2"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"http_interactions":[{"request":{"method":"get","uri":"http://api.chatspry.dev:8080/v1/user","body":{"encoding":"US-ASCII","base64_string":""},"headers":{"Accept":["application/json"],"User-Agent":["Chatspry Ruby Gem 0.0.1.pre1"],"Authorization":["Bearer <ACCESS_TOKEN>"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":200,"message":"OK"},"headers":{"Date":["Wed, 18 Jun 2014 19:14:31 GMT"],"Status":["200 OK"],"Connection":["close"],"X-Frame-Options":["SAMEORIGIN"],"X-Xss-Protection":["1; mode=block"],"X-Content-Type-Options":["nosniff"],"Access-Control-Allow-Credentials":["true"],"Access-Control-Allow-Methods":["GET,POST,PUT,DELETE,PATCH,OPTIONS"],"Access-Control-Allow-Headers":["*"],"Access-Control-Max-Age":["1728000"],"Content-Type":["application/json; charset=utf-8"],"Etag":["\"0a2327f04d9202b40e890296b3055c0f\""],"Cache-Control":["max-age=0, private, must-revalidate"],"X-Request-Id":["20e88c1c-e11b-496d-ae32-5344b27682a9"],"X-Runtime":["0.019083"]},"body":{"encoding":"US-ASCII","base64_string":"eyJ1c2VyIjp7ImlkIjoiN2EyYjI0MTktYzU5NC00MzU5LWFhMzUtMTVhNWJk\nNjMxOGRjIiwiaGFuZGxlIjoiPENIQVRTUFJZX0lERU5USUZJRVI+IiwibmFt\nZSI6IlBoaWxpcCBWaWVpcmEiLCJ1cGRhdGVkX2F0IjoxNDAzMDM2MjQ5LCJj\ncmVhdGVkX2F0IjoxNDAzMDM2MjQ5fX0=\n"},"http_version":null},"recorded_at":"Wed, 18 Jun 2014 19:14:31 GMT"}],"recorded_with":"VCR 2.9.2"}
|
@@ -0,0 +1,301 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe Chatspry::Client do
|
4
|
+
|
5
|
+
before { Chatspry.reset! }
|
6
|
+
after { Chatspry.reset! }
|
7
|
+
|
8
|
+
describe "module configuration" do
|
9
|
+
|
10
|
+
before do
|
11
|
+
Chatspry.reset!
|
12
|
+
Chatspry.configure do |config|
|
13
|
+
Chatspry::Configurable.keys.each do |key|
|
14
|
+
config.send("#{key}=", "Some #{key}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
after { Chatspry.reset! }
|
20
|
+
|
21
|
+
it "inherits the module configuration" do
|
22
|
+
client = Chatspry::Client.new
|
23
|
+
Chatspry::Configurable.keys.each do |key|
|
24
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq("Some #{key}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "with class level configuration" do
|
29
|
+
|
30
|
+
let!(:opts) do
|
31
|
+
@opts = {
|
32
|
+
connection_options: {
|
33
|
+
ssl: { verify: false }
|
34
|
+
},
|
35
|
+
identifier: "zeeraw",
|
36
|
+
passphrase: "SecretPass1234!"
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
it "overrides module configuration" do
|
41
|
+
|
42
|
+
client = Chatspry::Client.new(opts)
|
43
|
+
|
44
|
+
expect(client.identifier).to eq("zeeraw")
|
45
|
+
expect(client.instance_variable_get(:"@passphrase")).to eq("SecretPass1234!")
|
46
|
+
expect(client.client_id).to eq(Chatspry.client_id)
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
it "can set configuration after initialization" do
|
51
|
+
|
52
|
+
client = Chatspry::Client.new
|
53
|
+
client.configure do |config|
|
54
|
+
@opts.each do |key, value|
|
55
|
+
config.send("#{key}=", value)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
expect(client.identifier).to eq("zeeraw")
|
60
|
+
expect(client.instance_variable_get(:"@passphrase")).to eq("SecretPass1234!")
|
61
|
+
expect(client.client_id).to eq(Chatspry.client_id)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
it "masks passphrases on inspect" do
|
66
|
+
client = Chatspry::Client.new(@opts)
|
67
|
+
inspected = client.inspect
|
68
|
+
expect(inspected).not_to include("SecretPass1234!")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "masks tokens on inspect" do
|
72
|
+
client = Chatspry::Client.new(:access_token => '87614b09dd141c22800f96f11737ade5226d7ba8')
|
73
|
+
inspected = client.inspect
|
74
|
+
expect(inspected).not_to eq("87614b09dd141c22800f96f11737ade5226d7ba8")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "masks client secrets on inspect" do
|
78
|
+
client = Chatspry::Client.new(:client_secret => '87614b09dd141c22800f96f11737ade5226d7ba8')
|
79
|
+
inspected = client.inspect
|
80
|
+
expect(inspected).not_to eq("87614b09dd141c22800f96f11737ade5226d7ba8")
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "authentication" do
|
87
|
+
|
88
|
+
let!(:client) { Chatspry.client }
|
89
|
+
before { Chatspry.reset! }
|
90
|
+
|
91
|
+
describe "with module level config" do
|
92
|
+
|
93
|
+
before { Chatspry.reset! }
|
94
|
+
|
95
|
+
it "sets basic auth creds with .configure" do
|
96
|
+
|
97
|
+
Chatspry.configure do |config|
|
98
|
+
config.identifier = "berwyn"
|
99
|
+
config.passphrase = "canIhazCheezburgs"
|
100
|
+
end
|
101
|
+
|
102
|
+
expect(Chatspry.client).to be_basic_authenticated
|
103
|
+
end
|
104
|
+
|
105
|
+
it "sets basic auth creds with module methods" do
|
106
|
+
|
107
|
+
Chatspry.identifier = "berwyn"
|
108
|
+
Chatspry.passphrase = "canIhazCheezburgs"
|
109
|
+
|
110
|
+
expect(Chatspry.client).to be_basic_authenticated
|
111
|
+
end
|
112
|
+
|
113
|
+
it "sets oauth token with .configure" do
|
114
|
+
|
115
|
+
Chatspry.configure do |config|
|
116
|
+
config.access_token = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
117
|
+
end
|
118
|
+
|
119
|
+
expect(Chatspry.client).not_to be_basic_authenticated
|
120
|
+
expect(Chatspry.client).to be_token_authenticated
|
121
|
+
end
|
122
|
+
|
123
|
+
it "sets oauth token with module methods" do
|
124
|
+
Chatspry.access_token = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
125
|
+
|
126
|
+
expect(Chatspry.client).not_to be_basic_authenticated
|
127
|
+
expect(Chatspry.client).to be_token_authenticated
|
128
|
+
end
|
129
|
+
|
130
|
+
it "sets oauth application creds with .configure" do
|
131
|
+
|
132
|
+
Chatspry.configure do |config|
|
133
|
+
config.client_id = '97b4937b385eb63d1f46'
|
134
|
+
config.client_secret = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
|
135
|
+
end
|
136
|
+
|
137
|
+
expect(Chatspry.client).not_to be_basic_authenticated
|
138
|
+
expect(Chatspry.client).not_to be_token_authenticated
|
139
|
+
expect(Chatspry.client).to be_application_authenticated
|
140
|
+
end
|
141
|
+
|
142
|
+
it "sets oauth token with module methods" do
|
143
|
+
|
144
|
+
Chatspry.client_id = "97b4937b385eb63d1f46"
|
145
|
+
Chatspry.client_secret = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
146
|
+
|
147
|
+
expect(Chatspry.client).not_to be_basic_authenticated
|
148
|
+
expect(Chatspry.client).not_to be_token_authenticated
|
149
|
+
expect(Chatspry.client).to be_application_authenticated
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "with class level config" do
|
155
|
+
|
156
|
+
it "sets basic auth creds with .configure" do
|
157
|
+
|
158
|
+
client.configure do |config|
|
159
|
+
config.identifier = "aethe"
|
160
|
+
config.passphrase = "WhatIsWithThesePonies?"
|
161
|
+
end
|
162
|
+
|
163
|
+
expect(client).to be_basic_authenticated
|
164
|
+
end
|
165
|
+
|
166
|
+
it "sets basic auth creds with instance methods" do
|
167
|
+
client.identifier = "aethe"
|
168
|
+
client.passphrase = "WhatIsWithThesePonies?"
|
169
|
+
expect(client).to be_basic_authenticated
|
170
|
+
end
|
171
|
+
|
172
|
+
it "sets oauth token with .configure" do
|
173
|
+
client.access_token = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
174
|
+
expect(client).not_to be_basic_authenticated
|
175
|
+
expect(client).to be_token_authenticated
|
176
|
+
end
|
177
|
+
|
178
|
+
it "sets oauth token with instance methods" do
|
179
|
+
client.access_token = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
180
|
+
expect(client).not_to be_basic_authenticated
|
181
|
+
expect(client).to be_token_authenticated
|
182
|
+
end
|
183
|
+
|
184
|
+
it "sets oauth application creds with .configure" do
|
185
|
+
|
186
|
+
client.configure do |config|
|
187
|
+
config.client_id = "97b4937b385eb63d1f46"
|
188
|
+
config.client_secret = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
189
|
+
end
|
190
|
+
|
191
|
+
expect(client).not_to be_basic_authenticated
|
192
|
+
expect(client).not_to be_token_authenticated
|
193
|
+
expect(client).to be_application_authenticated
|
194
|
+
end
|
195
|
+
|
196
|
+
it "sets oauth token with module methods" do
|
197
|
+
client.client_id = "97b4937b385eb63d1f46"
|
198
|
+
client.client_secret = "d255197b4937b385eb63d1f4677e3ffee61fbaea"
|
199
|
+
|
200
|
+
expect(client).not_to be_basic_authenticated
|
201
|
+
expect(client).not_to be_token_authenticated
|
202
|
+
expect(client).to be_application_authenticated
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
describe "when token authenticated", :vcr do
|
208
|
+
|
209
|
+
it "makes authenticated calls" do
|
210
|
+
client = oauth_client
|
211
|
+
|
212
|
+
root_request = stub_get("/").with(
|
213
|
+
headers: { authorization: "Bearer #{test_chatspry_token}" }
|
214
|
+
)
|
215
|
+
|
216
|
+
client.get("/")
|
217
|
+
assert_requested root_request
|
218
|
+
end
|
219
|
+
|
220
|
+
it "fetches and memorizes identifier" do
|
221
|
+
client = oauth_client
|
222
|
+
|
223
|
+
expect(client.identifier).to eq(test_chatspry_identifier)
|
224
|
+
assert_requested :get, chatspry_url("/v1/user")
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
describe "when application authenticated" do
|
230
|
+
it "makes authenticated calls" do
|
231
|
+
client = Chatspry.client
|
232
|
+
client.client_id = '97b4937b385eb63d1f46'
|
233
|
+
client.client_secret = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
|
234
|
+
|
235
|
+
root_request = stub_get("/?client_id=97b4937b385eb63d1f46&client_secret=d255197b4937b385eb63d1f4677e3ffee61fbaea")
|
236
|
+
client.get("/")
|
237
|
+
assert_requested root_request
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
describe ".connection" do
|
244
|
+
|
245
|
+
before { Chatspry.reset! }
|
246
|
+
|
247
|
+
it "acts like a Faraday connection" do
|
248
|
+
expect(Chatspry.client.connection).to be_kind_of Faraday::Connection
|
249
|
+
end
|
250
|
+
|
251
|
+
it "caches the connection" do
|
252
|
+
connection = Chatspry.client.connection
|
253
|
+
expect(connection.object_id).to eq(Chatspry.client.connection.object_id)
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
describe ".get", :vcr do
|
259
|
+
|
260
|
+
before(:each) { Chatspry.reset! }
|
261
|
+
|
262
|
+
it "handles query params" do
|
263
|
+
Chatspry.get "/", foo: "bar"
|
264
|
+
assert_requested :get, "http://api.chatspry.dev:8080?foo=bar", accept: "text/html"
|
265
|
+
end
|
266
|
+
|
267
|
+
it "handles headers" do
|
268
|
+
request = stub_get("/zen").with(
|
269
|
+
query: { foo: "bar" }, headers: {
|
270
|
+
accept: "text/plain"
|
271
|
+
}
|
272
|
+
)
|
273
|
+
|
274
|
+
Chatspry.get "/zen", foo: "bar", accept: "text/plain"
|
275
|
+
assert_requested request
|
276
|
+
end
|
277
|
+
|
278
|
+
end
|
279
|
+
|
280
|
+
describe ".head", :vcr do
|
281
|
+
|
282
|
+
before(:each) { Chatspry.reset! }
|
283
|
+
|
284
|
+
it "handles query params" do
|
285
|
+
Chatspry.head "/", foo: "bar"
|
286
|
+
assert_requested :head, "http://api.chatspry.dev:8080?foo=bar"
|
287
|
+
end
|
288
|
+
|
289
|
+
it "handles headers" do
|
290
|
+
request = stub_head("/zen").with(
|
291
|
+
query: { foo: "bar" }, headers: {
|
292
|
+
accept: "text/plain"
|
293
|
+
}
|
294
|
+
)
|
295
|
+
|
296
|
+
Chatspry.head "/zen", foo: "bar", accept: "text/plain"
|
297
|
+
assert_requested request
|
298
|
+
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Chatspry do
|
4
|
+
|
5
|
+
before { Chatspry.reset! }
|
6
|
+
after { Chatspry.reset! }
|
7
|
+
|
8
|
+
it "sets defaults" do
|
9
|
+
Chatspry::Configurable.keys.each do |key|
|
10
|
+
expect(Chatspry.instance_variable_get(:"@#{key}")).to eq( Chatspry::Default.send(key) )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".client" do
|
15
|
+
|
16
|
+
it "creates an Chatspry::Client" do
|
17
|
+
expect(Chatspry.client).to be_kind_of Chatspry::Client
|
18
|
+
end
|
19
|
+
|
20
|
+
it "caches the client when the same options are passed" do
|
21
|
+
expect(Chatspry.client).to eq(Chatspry.client)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns a fresh client when options are not the same" do
|
25
|
+
client = Chatspry.client
|
26
|
+
Chatspry.access_token = "87614b09dd141c22800f96f11737ade5226d7ba8"
|
27
|
+
client_two = Chatspry.client
|
28
|
+
client_three = Chatspry.client
|
29
|
+
expect(client).not_to eq(client_two)
|
30
|
+
expect(client_three).to eq(client_two)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".configure" do
|
36
|
+
|
37
|
+
Chatspry::Configurable.keys.each do |key|
|
38
|
+
it "sets the #{ key.to_s.gsub('_', ' ') }" do
|
39
|
+
Chatspry.configure do |config|
|
40
|
+
config.send("#{key}=", key)
|
41
|
+
end
|
42
|
+
expect(Chatspry.instance_variable_get(:"@#{key}")).to eq(key)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "json"
|
2
|
+
require "chatspry"
|
3
|
+
require "rspec"
|
4
|
+
require "webmock/rspec"
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
|
8
|
+
config.filter_run :focus
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
|
11
|
+
config.default_formatter = "doc" if config.files_to_run.one?
|
12
|
+
|
13
|
+
config.order = :random
|
14
|
+
|
15
|
+
Kernel.srand config.seed
|
16
|
+
|
17
|
+
config.expect_with :rspec do |expectations|
|
18
|
+
expectations.syntax = :expect
|
19
|
+
end
|
20
|
+
|
21
|
+
config.mock_with :rspec do |mocks|
|
22
|
+
mocks.syntax = :expect
|
23
|
+
mocks.verify_partial_doubles = true
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
require "support/fixture_configuration"
|
29
|
+
require "support/environment_defaults"
|
30
|
+
require "support/request_stubs"
|
31
|
+
|
32
|
+
def oauth_client
|
33
|
+
Chatspry::Client.new(:access_token => test_chatspry_token)
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
def test_chatspry_identifier
|
2
|
+
ENV.fetch "CHATSPRY_TEST_IDENTIFIER"
|
3
|
+
end
|
4
|
+
|
5
|
+
def test_chatspry_passphrase
|
6
|
+
ENV.fetch "CHATSPRY_TEST_PASSPHRASE"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_chatspry_token
|
10
|
+
ENV.fetch "CHATSPRY_TEST_TOKEN", "x" * 21
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_chatspry_client_id
|
14
|
+
ENV.fetch "CHATSPRY_TEST_CLIENT_ID", "x" * 26
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_chatspry_client_secret
|
18
|
+
ENV.fetch "CHATSPRY_TEST_CLIENT_SECRET", "x" * 32
|
19
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "vcr"
|
2
|
+
|
3
|
+
VCR.configure do |c|
|
4
|
+
|
5
|
+
c.configure_rspec_metadata!
|
6
|
+
c.filter_sensitive_data("<CHATSPRY_IDENTIFIER>") do
|
7
|
+
test_chatspry_identifier
|
8
|
+
end
|
9
|
+
|
10
|
+
c.filter_sensitive_data("<CHATSPRY_PASSPHRASE>") do
|
11
|
+
test_chatspry_passphrase
|
12
|
+
end
|
13
|
+
|
14
|
+
c.filter_sensitive_data("<ACCESS_TOKEN>") do
|
15
|
+
test_chatspry_token
|
16
|
+
end
|
17
|
+
|
18
|
+
c.filter_sensitive_data("<CHATSPRY_CLIENT_ID>") do
|
19
|
+
test_chatspry_client_id
|
20
|
+
end
|
21
|
+
|
22
|
+
c.filter_sensitive_data("<CHATSPRY_CLIENT_SECRET>") do
|
23
|
+
test_chatspry_client_secret
|
24
|
+
end
|
25
|
+
|
26
|
+
# c.before_http_request(:real?) do |request|
|
27
|
+
|
28
|
+
# next if request.headers['X-Vcr-Test-Repo-Setup']
|
29
|
+
# next unless request.uri.include? test_github_repository
|
30
|
+
|
31
|
+
# options = {
|
32
|
+
# :headers => {'X-Vcr-Test-Repo-Setup' => 'true'},
|
33
|
+
# :auto_init => true
|
34
|
+
# }
|
35
|
+
|
36
|
+
# test_repo = "#{test_github_login}/#{test_github_repository}"
|
37
|
+
# if !oauth_client.repository?(test_repo, options)
|
38
|
+
# Octokit.octokit_warn "NOTICE: Creating #{test_repo} test repository."
|
39
|
+
# oauth_client.create_repository(test_github_repository, options)
|
40
|
+
# end
|
41
|
+
|
42
|
+
# test_org_repo = "#{test_github_org}/#{test_github_repository}"
|
43
|
+
# if !oauth_client.repository?(test_org_repo, options)
|
44
|
+
# Octokit.octokit_warn "NOTICE: Creating #{test_org_repo} test repository."
|
45
|
+
# options[:organization] = test_github_org
|
46
|
+
# oauth_client.create_repository(test_github_repository, options)
|
47
|
+
# end
|
48
|
+
|
49
|
+
# end
|
50
|
+
|
51
|
+
# c.ignore_request do |request|
|
52
|
+
# !!request.headers['X-Vcr-Test-Repo-Setup']
|
53
|
+
# end
|
54
|
+
|
55
|
+
c.default_cassette_options = {
|
56
|
+
serialize_with: :json,
|
57
|
+
preserve_exact_body_bytes: true,
|
58
|
+
decode_compressed_response: true,
|
59
|
+
record: ENV['TRAVIS'] ? :none : :once
|
60
|
+
}
|
61
|
+
|
62
|
+
c.cassette_library_dir = File.expand_path("../../cassettes", __FILE__)
|
63
|
+
c.hook_into :webmock
|
64
|
+
end
|
65
|
+
|
66
|
+
def fixture_path
|
67
|
+
File.expand_path("../../fixtures", __FILE__)
|
68
|
+
end
|
69
|
+
|
70
|
+
def fixture(file)
|
71
|
+
File.new(fixture_path + "/" + file)
|
72
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
def stub_delete(url)
|
2
|
+
stub_request(:delete, chatspry_url(url))
|
3
|
+
end
|
4
|
+
|
5
|
+
def stub_get(url)
|
6
|
+
stub_request(:get, chatspry_url(url))
|
7
|
+
end
|
8
|
+
|
9
|
+
def stub_head(url)
|
10
|
+
stub_request(:head, chatspry_url(url))
|
11
|
+
end
|
12
|
+
|
13
|
+
def stub_patch(url)
|
14
|
+
stub_request(:patch, chatspry_url(url))
|
15
|
+
end
|
16
|
+
|
17
|
+
def stub_post(url)
|
18
|
+
stub_request(:post, chatspry_url(url))
|
19
|
+
end
|
20
|
+
|
21
|
+
def stub_put(url)
|
22
|
+
stub_request(:put, chatspry_url(url))
|
23
|
+
end
|
24
|
+
|
25
|
+
def chatspry_url(url)
|
26
|
+
url =~ /^http/ ? url : "http://api.chatspry.dev:8080#{url}"
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatspry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.pre2
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,46 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.9'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.9'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: hashie
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
- - <
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '2.0'
|
49
|
+
- - <
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '3.0'
|
14
52
|
- !ruby/object:Gem::Dependency
|
15
53
|
name: bundler
|
16
54
|
requirement: !ruby/object:Gem::Requirement
|
@@ -27,6 +65,22 @@ dependencies:
|
|
27
65
|
- - ~>
|
28
66
|
- !ruby/object:Gem::Version
|
29
67
|
version: '1.6'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rspec
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ~>
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 3.0.0
|
30
84
|
- !ruby/object:Gem::Dependency
|
31
85
|
name: rake
|
32
86
|
requirement: !ruby/object:Gem::Requirement
|
@@ -51,6 +105,7 @@ extensions: []
|
|
51
105
|
extra_rdoc_files: []
|
52
106
|
files:
|
53
107
|
- .gitignore
|
108
|
+
- .rspec
|
54
109
|
- .ruby-version
|
55
110
|
- Gemfile
|
56
111
|
- LICENSE.txt
|
@@ -58,7 +113,21 @@ files:
|
|
58
113
|
- Rakefile
|
59
114
|
- chatspry.gemspec
|
60
115
|
- lib/chatspry.rb
|
116
|
+
- lib/chatspry/authentication.rb
|
117
|
+
- lib/chatspry/client.rb
|
118
|
+
- lib/chatspry/client/user.rb
|
119
|
+
- lib/chatspry/configurable.rb
|
120
|
+
- lib/chatspry/default.rb
|
61
121
|
- lib/chatspry/version.rb
|
122
|
+
- spec/cassettes/Chatspry_Client/_get/handles_query_params.json
|
123
|
+
- spec/cassettes/Chatspry_Client/_head/handles_query_params.json
|
124
|
+
- spec/cassettes/Chatspry_Client/authentication/when_token_authenticated/fetches_and_memorizes_identifier.json
|
125
|
+
- spec/chatspry/client_spec.rb
|
126
|
+
- spec/chatspry_spec.rb
|
127
|
+
- spec/helper.rb
|
128
|
+
- spec/support/environment_defaults.rb
|
129
|
+
- spec/support/fixture_configuration.rb
|
130
|
+
- spec/support/request_stubs.rb
|
62
131
|
homepage: http://developer.chatspry.com/
|
63
132
|
licenses:
|
64
133
|
- MIT
|
@@ -84,4 +153,13 @@ rubygems_version: 1.8.23.2
|
|
84
153
|
signing_key:
|
85
154
|
specification_version: 3
|
86
155
|
summary: API wrapper for chatspry in ruby
|
87
|
-
test_files:
|
156
|
+
test_files:
|
157
|
+
- spec/cassettes/Chatspry_Client/_get/handles_query_params.json
|
158
|
+
- spec/cassettes/Chatspry_Client/_head/handles_query_params.json
|
159
|
+
- spec/cassettes/Chatspry_Client/authentication/when_token_authenticated/fetches_and_memorizes_identifier.json
|
160
|
+
- spec/chatspry/client_spec.rb
|
161
|
+
- spec/chatspry_spec.rb
|
162
|
+
- spec/helper.rb
|
163
|
+
- spec/support/environment_defaults.rb
|
164
|
+
- spec/support/fixture_configuration.rb
|
165
|
+
- spec/support/request_stubs.rb
|