convey_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +50 -0
- data/lib/convey_client/item.rb +19 -0
- data/lib/convey_client/items.rb +100 -0
- data/lib/convey_client/request.rb +44 -0
- data/lib/convey_client/request_error.rb +4 -0
- data/lib/convey_client.rb +66 -0
- data/spec/convey_client/item_spec.rb +45 -0
- data/spec/convey_client/items_spec.rb +103 -0
- data/spec/convey_client/request_spec.rb +117 -0
- data/spec/convey_client_spec.rb +97 -0
- data/spec/spec_helper.rb +8 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Convey Client
|
2
|
+
|
3
|
+
The convey_client gem is an API client for convey.io
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install convey_client
|
8
|
+
|
9
|
+
## Configuration
|
10
|
+
|
11
|
+
Add the below to a file and require it at boot up:
|
12
|
+
|
13
|
+
ConveyClient.setup do |config|
|
14
|
+
config.subdomain = "convey"
|
15
|
+
config.use_cache(:basic_file, 300, :path => "tmp/")
|
16
|
+
end
|
17
|
+
|
18
|
+
You can specify any Moneta cache store you like, e.g. basic_file, memory, memcache etc and the gem will then cache every request you perform. The (optional) second argument is cache expiry in seconds, you can provide any initialize options required by your cache store as the 3rd arg.
|
19
|
+
|
20
|
+
There is an optional third config item, config.raise_request_errors which defaults to false. If you set this to true and the client encounters errors, ConveyClient::RequestErrors will be raised, otherwise you silently get an empty result set (this will be improved in the future).
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Get a specific item
|
25
|
+
|
26
|
+
ConveyClient::Items.find("convey-item-id")
|
27
|
+
|
28
|
+
Get all of your items from convey.io
|
29
|
+
|
30
|
+
ConveyClient::Items.all
|
31
|
+
|
32
|
+
Get all items from a specific convey.io bucket
|
33
|
+
|
34
|
+
ConveyClient::Items.in_bucket("convey-bucket-id")
|
35
|
+
|
36
|
+
Get all items made with a specific convey.io template
|
37
|
+
|
38
|
+
ConveyClient::Items.in_bucket("convey-template-id")
|
39
|
+
|
40
|
+
Get all items from a specific bucket, made with the given template
|
41
|
+
|
42
|
+
ConveyClient::Items.in_bucket_and_using_template("convey-bucket-id", "convey-template-id")
|
43
|
+
|
44
|
+
For any request returning a collection, you can search and sort (limiting coming soon), for example:
|
45
|
+
|
46
|
+
ConveyClient::Items.all.sorted("published_on", "desc") # get all items, sorted by the published_on attribute
|
47
|
+
ConveyClient::Items.all.where("published_on" ">", "2012-05-30") # get all items published after May 30, 2012
|
48
|
+
|
49
|
+
The available searching operators are: >, <, >=, <=, =, !=, contains, does_not_contain
|
50
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ConveyClient
|
2
|
+
class Item
|
3
|
+
attr_accessor :id, :name, :bucket, :template, :attributes
|
4
|
+
|
5
|
+
def initialize(raw_item)
|
6
|
+
self.id = raw_item["id"]
|
7
|
+
self.name = raw_item["name"]
|
8
|
+
self.bucket = raw_item["bucket"]
|
9
|
+
self.template = raw_item["template"]
|
10
|
+
self.attributes = raw_item["attributes"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method, *args, &block)
|
14
|
+
super unless attributes.keys.include?(method.to_s)
|
15
|
+
|
16
|
+
attributes[method.to_s]["value"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ConveyClient
|
4
|
+
class Items
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_accessor :path, :sort_key, :sort_dir, :items, :search_params
|
8
|
+
|
9
|
+
def initialize(path)
|
10
|
+
self.path = path
|
11
|
+
self.items = nil
|
12
|
+
self.search_params = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def sorted(key, dir)
|
16
|
+
self.sort_key = key
|
17
|
+
self.sort_dir = dir
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def where(key, operator, val)
|
22
|
+
search_params << [ key, operator, val ]
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def each
|
27
|
+
to_a
|
28
|
+
items.each { |item| yield item }
|
29
|
+
end
|
30
|
+
|
31
|
+
def length
|
32
|
+
to_a
|
33
|
+
items.length
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_a
|
37
|
+
return items if items
|
38
|
+
|
39
|
+
parse(ConveyClient::Request.execute(url))
|
40
|
+
items
|
41
|
+
rescue RequestError
|
42
|
+
self.items = []
|
43
|
+
end
|
44
|
+
|
45
|
+
def [](index)
|
46
|
+
to_a[index]
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.find(item_id)
|
50
|
+
new("items/#{item_id}").to_a.first
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.all
|
54
|
+
new("items")
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.in_bucket(bucket_id)
|
58
|
+
new("buckets/#{bucket_id}/items")
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.using_template(template_id)
|
62
|
+
new("templates/#{template_id}/items")
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.in_bucket_and_using_template(bucket_id, template_id)
|
66
|
+
new("buckets/#{bucket_id}/templates/#{template_id}/items")
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def parse(raw_items)
|
71
|
+
parsed_items = JSON.parse(raw_items)
|
72
|
+
if parsed_items.is_a?(Array)
|
73
|
+
self.items = parsed_items.collect do |raw_item|
|
74
|
+
Item.new(raw_item)
|
75
|
+
end
|
76
|
+
else
|
77
|
+
self.items = [ Item.new(parsed_items) ]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def url
|
82
|
+
params = []
|
83
|
+
|
84
|
+
if sort_key && sort_dir
|
85
|
+
params << URI.encode("sort[key]=#{sort_key}")
|
86
|
+
params << URI.encode("sort[dir]=#{sort_dir}")
|
87
|
+
end
|
88
|
+
|
89
|
+
search_params.each do |(key, operator, value)|
|
90
|
+
params << URI.encode("search[#{key}][operator]=#{operator}&search[#{key}][value]=#{value}")
|
91
|
+
end
|
92
|
+
|
93
|
+
if params.any?
|
94
|
+
"#{path}?#{params.join("&")}"
|
95
|
+
else
|
96
|
+
path
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module ConveyClient
|
4
|
+
class Request
|
5
|
+
attr_accessor :path
|
6
|
+
|
7
|
+
def initialize(path)
|
8
|
+
self.path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.execute(path)
|
12
|
+
new(path).execute
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute
|
16
|
+
return ConveyClient.cached_response(path) if ConveyClient.cached?(path)
|
17
|
+
|
18
|
+
response = Net::HTTP.get_response(url)
|
19
|
+
if response.code.to_i == 200
|
20
|
+
ConveyClient.cache(path, response.body)
|
21
|
+
response.body
|
22
|
+
else
|
23
|
+
if ConveyClient.raise_request_errors
|
24
|
+
raise ConveyClient::RequestError, "#{url} - #{response.code}"
|
25
|
+
else
|
26
|
+
"[]"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def url
|
32
|
+
return @url unless @url.nil?
|
33
|
+
|
34
|
+
full_url = ConveyClient.base_url + path
|
35
|
+
|
36
|
+
if (auth_token = ConveyClient.auth_token)
|
37
|
+
full_url += full_url.include?("?") ? "&" : "?"
|
38
|
+
full_url += "token=#{auth_token}"
|
39
|
+
end
|
40
|
+
|
41
|
+
@url = URI.parse(full_url)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'moneta'
|
2
|
+
require 'convey_client/item'
|
3
|
+
require 'convey_client/items'
|
4
|
+
require 'convey_client/request'
|
5
|
+
require 'convey_client/request_error'
|
6
|
+
|
7
|
+
module ConveyClient
|
8
|
+
class << self
|
9
|
+
attr_accessor :config
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.setup
|
13
|
+
self.config = Configuration.new
|
14
|
+
yield(config)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.base_url
|
18
|
+
"http://#{config.subdomain}.convey.io/api/"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.cached?(key)
|
22
|
+
return false unless config.cache
|
23
|
+
|
24
|
+
!config.cache[key].nil? && !config.cache[key].empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.cached_response(key)
|
28
|
+
return unless cached?(key)
|
29
|
+
|
30
|
+
config.cache[key]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.cache(key, content)
|
34
|
+
return unless config.cache
|
35
|
+
|
36
|
+
options = {}
|
37
|
+
options[:expires_in] = config.cache_timeout if config.cache_timeout
|
38
|
+
config.cache.store(key, content, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.auth_token
|
42
|
+
config.auth_token
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.subdomain
|
46
|
+
config.subdomain
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.raise_request_errors
|
50
|
+
config.raise_request_errors
|
51
|
+
end
|
52
|
+
|
53
|
+
class Configuration
|
54
|
+
attr_accessor :subdomain, :auth_token, :cache, :cache_timeout,
|
55
|
+
:raise_request_errors
|
56
|
+
|
57
|
+
def use_cache(method, timeout = nil, options={})
|
58
|
+
timeout, options = nil, timeout if timeout.is_a?(Hash)
|
59
|
+
|
60
|
+
klass = method.to_s.split('_').collect { |e| e.capitalize }.join
|
61
|
+
Moneta.autoload(klass.to_sym, "moneta/#{method}")
|
62
|
+
self.cache = Moneta.const_get(klass).new(options)
|
63
|
+
self.cache_timeout = timeout
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConveyClient::Item do
|
4
|
+
before do
|
5
|
+
@item = ConveyClient::Item.new({
|
6
|
+
"id" => "id",
|
7
|
+
"name" => "name",
|
8
|
+
"bucket" => "bucket-id",
|
9
|
+
"template" => "template-id",
|
10
|
+
"attributes" => {
|
11
|
+
"age" => {
|
12
|
+
"value" => 28
|
13
|
+
},
|
14
|
+
"level" => {
|
15
|
+
"value" => "blue"
|
16
|
+
},
|
17
|
+
}
|
18
|
+
})
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "core attributes" do
|
22
|
+
it "should store the items id" do
|
23
|
+
@item.id.should == "id"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should store the items name" do
|
27
|
+
@item.name.should == "name"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should store the items bucket" do
|
31
|
+
@item.bucket.should == "bucket-id"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should store the items template" do
|
35
|
+
@item.template.should == "template-id"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "accessing template attributes" do
|
40
|
+
it "should provide methods for each attribute key" do
|
41
|
+
@item.age.should == 28
|
42
|
+
@item.level.should == "blue"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
SINGLE_ITEM_RESPONSE = <<-ITEM
|
4
|
+
{"name":"First Item Name","id":"first-itemg-name","template":"first-item-template","bucket":"first-items","attributes":{"published_on":{"type":"date","value":"2012-04-27"},"content":{"type":"text","value":"<p>This is some content</p>"}}}
|
5
|
+
ITEM
|
6
|
+
MULTIPLE_ITEMS_RESPONSE = <<-ITEMS
|
7
|
+
[#{SINGLE_ITEM_RESPONSE},{"name":"Second Item Name","id":"second-itemg-name","template":"second-item-template","bucket":"second-items","attributes":{"published_on":{"type":"date","value":"2012-05-27"},"content":{"type":"text","value":"<p>This is second items content</p>"}}}]
|
8
|
+
ITEMS
|
9
|
+
|
10
|
+
describe ConveyClient::Items do
|
11
|
+
before do
|
12
|
+
ConveyClient.setup do |config|
|
13
|
+
config.subdomain = "test"
|
14
|
+
config.auth_token = nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".find" do
|
19
|
+
describe "when found" do
|
20
|
+
before do
|
21
|
+
ConveyClient::Request.stub!(:execute).and_return(SINGLE_ITEM_RESPONSE)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return the found item" do
|
25
|
+
item = ConveyClient::Items.find("id")
|
26
|
+
item.name.should == "First Item Name"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "when nothing is found" do
|
31
|
+
before do
|
32
|
+
ConveyClient::Request.stub!(:execute).and_return("[]")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return nil" do
|
36
|
+
ConveyClient::Items.find("id").should be_nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ".all" do
|
42
|
+
describe "when there is one item" do
|
43
|
+
before do
|
44
|
+
ConveyClient::Request.stub!(:execute).and_return(SINGLE_ITEM_RESPONSE)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return an array of one" do
|
48
|
+
ConveyClient::Items.all.length.should == 1
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "when there are multiple items" do
|
53
|
+
before do
|
54
|
+
ConveyClient::Request.stub!(:execute).and_return(MULTIPLE_ITEMS_RESPONSE)
|
55
|
+
@items = ConveyClient::Items.all
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return an array" do
|
59
|
+
@items.length.should == 2
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return the results in order" do
|
63
|
+
@items[0].name.should == "First Item Name"
|
64
|
+
@items[1].name.should == "Second Item Name"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe ".in_bucket" do
|
70
|
+
it "should create the appropriate request path" do
|
71
|
+
ConveyClient::Request.should_receive(:execute).with("buckets/bucket-id/items").and_return("[]")
|
72
|
+
ConveyClient::Items.in_bucket("bucket-id").to_a
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe ".using_template" do
|
77
|
+
it "should create the appropriate request path" do
|
78
|
+
ConveyClient::Request.should_receive(:execute).with("templates/template-id/items").and_return("[]")
|
79
|
+
ConveyClient::Items.using_template("template-id").to_a
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe ".in_bucket_and_using_template" do
|
84
|
+
it "should create the appropriate request path" do
|
85
|
+
ConveyClient::Request.should_receive(:execute).with("buckets/bucket-id/templates/template-id/items").and_return("[]")
|
86
|
+
ConveyClient::Items.in_bucket_and_using_template("bucket-id", "template-id").to_a
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "sorting" do
|
91
|
+
it "should add the appropriate params to the request path" do
|
92
|
+
ConveyClient::Request.should_receive(:execute).with("items?sort[key]=age&sort[dir]=asc").and_return("[]")
|
93
|
+
ConveyClient::Items.all.sorted("age", "asc").to_a
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "querying" do
|
98
|
+
it "should add the appropriate params to the request path" do
|
99
|
+
ConveyClient::Request.should_receive(:execute).with("items?search[age][operator]==&search[age][value]=20").and_return("[]")
|
100
|
+
ConveyClient::Items.all.where("age", "=", 20).to_a
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConveyClient::Request do
|
4
|
+
describe "#url" do
|
5
|
+
before do
|
6
|
+
@request = ConveyClient::Request.new("this/is/a/path")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "with no auth token" do
|
10
|
+
before do
|
11
|
+
ConveyClient.setup do |config|
|
12
|
+
config.subdomain = "test"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return the base url plus the requests path" do
|
17
|
+
@request.url.to_s.should == "http://test.convey.io/api/this/is/a/path"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "with an auth token" do
|
22
|
+
before do
|
23
|
+
ConveyClient.setup do |config|
|
24
|
+
config.subdomain = "test"
|
25
|
+
config.auth_token = "basdfasdfasdf"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return the base url plus the requests path" do
|
30
|
+
@request.url.to_s.should == "http://test.convey.io/api/this/is/a/path?token=basdfasdfasdf"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#execute" do
|
36
|
+
before do
|
37
|
+
ConveyClient.setup do |config|
|
38
|
+
config.subdomain = "test"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "when a response is not a 200" do
|
43
|
+
before do
|
44
|
+
FakeWeb.register_uri(:get, "http://test.convey.io/api/my/path",
|
45
|
+
:body => "404", :status => ["404", "Not Found"])
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "when raise_request_errors is set to false" do
|
49
|
+
before do
|
50
|
+
ConveyClient.setup do |config|
|
51
|
+
config.subdomain = "test"
|
52
|
+
config.raise_request_errors = false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns an empty array" do
|
57
|
+
ConveyClient::Request.execute("my/path").should == "[]"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "when raise_request_errors is set to true" do
|
62
|
+
before do
|
63
|
+
ConveyClient.setup do |config|
|
64
|
+
config.subdomain = "test"
|
65
|
+
config.raise_request_errors = true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "raise a request error" do
|
70
|
+
lambda do
|
71
|
+
ConveyClient::Request.execute("my/path")
|
72
|
+
end.should raise_error(ConveyClient::RequestError)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "without a cache" do
|
78
|
+
before do
|
79
|
+
FakeWeb.register_uri(:get, "http://test.convey.io/api/my/path",
|
80
|
+
:body => "response content")
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should send a http request but not cache the response" do
|
84
|
+
ConveyClient::Request.execute("my/path").should == "response content"
|
85
|
+
ConveyClient.cached_response("my/path").should be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "with a cache" do
|
90
|
+
before do
|
91
|
+
ConveyClient.setup do |config|
|
92
|
+
config.subdomain = "test"
|
93
|
+
config.use_cache(:memory, 86400)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "when the path is cached" do
|
98
|
+
it "should return the cached response" do
|
99
|
+
ConveyClient.cache("my/path", "content")
|
100
|
+
ConveyClient::Request.execute("my/path").should == "content"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "when the path is not cached" do
|
105
|
+
before do
|
106
|
+
FakeWeb.register_uri(:get, "http://test.convey.io/api/my/path",
|
107
|
+
:body => "response content")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should send a http request and cache the response" do
|
111
|
+
ConveyClient::Request.execute("my/path").should == "response content"
|
112
|
+
ConveyClient.cached_response("my/path").should == "response content"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConveyClient do
|
4
|
+
describe "setup" do
|
5
|
+
it "should allow me to set an subdomain" do
|
6
|
+
ConveyClient.setup do |config|
|
7
|
+
config.subdomain = "test"
|
8
|
+
end
|
9
|
+
|
10
|
+
ConveyClient.subdomain.should == "test"
|
11
|
+
ConveyClient.auth_token.should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should allow me to set an auth_token" do
|
15
|
+
ConveyClient.setup do |config|
|
16
|
+
config.auth_token = "asdf"
|
17
|
+
end
|
18
|
+
|
19
|
+
ConveyClient.auth_token.should == "asdf"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".base_url" do
|
24
|
+
it "should use the given subdomain" do
|
25
|
+
ConveyClient.setup do |config|
|
26
|
+
config.subdomain = "test"
|
27
|
+
end
|
28
|
+
ConveyClient.base_url.should == "http://test.convey.io/api/"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "caching" do
|
33
|
+
describe "setup" do
|
34
|
+
it "should allow me to set my cache of choice" do
|
35
|
+
ConveyClient.setup do |config|
|
36
|
+
config.subdomain = "test"
|
37
|
+
config.use_cache(:basic_file, 300, { :path => "tmp/" })
|
38
|
+
end
|
39
|
+
ConveyClient.config.cache.is_a?(Moneta::BasicFile).should be_true
|
40
|
+
ConveyClient.config.cache_timeout.should == 300
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ".cached_response" do
|
45
|
+
before do
|
46
|
+
ConveyClient.setup do |config|
|
47
|
+
config.subdomain = "test"
|
48
|
+
config.use_cache(:memory, 86400)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return the content if key is in cache" do
|
53
|
+
ConveyClient.cache("key", "content")
|
54
|
+
ConveyClient.cached_response("key").should == "content"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should return false if key is not in the cache cache" do
|
58
|
+
ConveyClient.cached_response("asdflasdklf").should be_nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe ".cached?" do
|
63
|
+
it "should be false when there is no cache" do
|
64
|
+
ConveyClient.cached?("key").should be_false
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be true if a key is in the cache" do
|
68
|
+
ConveyClient.setup do |config|
|
69
|
+
config.subdomain = "test"
|
70
|
+
config.use_cache(:memory, 86400)
|
71
|
+
end
|
72
|
+
ConveyClient.cache("key", "content")
|
73
|
+
ConveyClient.cached?("key").should be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should be false if a key is not in the cache" do
|
77
|
+
ConveyClient.setup do |config|
|
78
|
+
config.subdomain = "test"
|
79
|
+
config.use_cache(:memory, 86400)
|
80
|
+
end
|
81
|
+
ConveyClient.cached?("key").should be_false
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should be false if the entry for key is empty" do
|
85
|
+
ConveyClient.setup do |config|
|
86
|
+
config.subdomain = "test"
|
87
|
+
config.use_cache(:memory, 86400)
|
88
|
+
end
|
89
|
+
ConveyClient.cache("key", "")
|
90
|
+
ConveyClient.cached?("key").should be_false
|
91
|
+
|
92
|
+
ConveyClient.cache("key", nil)
|
93
|
+
ConveyClient.cached?("key").should be_false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'convey_client')
|
2
|
+
require 'fakeweb'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: convey_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anthony Langhorne
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-05-30 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: moneta
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: json
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
description:
|
38
|
+
email: conveyisawesome@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
files:
|
47
|
+
- lib/convey_client.rb
|
48
|
+
- lib/convey_client/item.rb
|
49
|
+
- lib/convey_client/items.rb
|
50
|
+
- lib/convey_client/request.rb
|
51
|
+
- lib/convey_client/request_error.rb
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- spec/convey_client_spec.rb
|
55
|
+
- spec/convey_client/item_spec.rb
|
56
|
+
- spec/convey_client/items_spec.rb
|
57
|
+
- spec/convey_client/request_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: http://github.com/convey/convey-client
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.16
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: An API client for convey.io
|
86
|
+
test_files:
|
87
|
+
- spec/convey_client_spec.rb
|
88
|
+
- spec/convey_client/item_spec.rb
|
89
|
+
- spec/convey_client/items_spec.rb
|
90
|
+
- spec/convey_client/request_spec.rb
|
91
|
+
- spec/spec_helper.rb
|