cistern 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +168 -11
  2. data/lib/cistern/version.rb +1 -1
  3. data/lib/cistern.rb +9 -1
  4. metadata +5 -6
data/README.md CHANGED
@@ -1,26 +1,183 @@
1
1
  # Cistern
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/lanej/cistern.png)](http://travis-ci.org/lanej/cistern)
4
+ [![Dependencies](https://gemnasium.com/lanej/cistern.png)](https://gemnasium.com/lanej/cistern.png)
4
5
 
5
- TODO: Write a gem description
6
+ Cistern helps you consistenly build your API clients and faciliates building mock support.
6
7
 
7
- ## Installation
8
+ ## Usage
8
9
 
9
- Add this line to your application's Gemfile:
10
+ ### Service
10
11
 
11
- gem 'cistern'
12
+ This represents the remote service that you are wrapping. If the service name is 'foo' then a good name is 'Foo::Client'.
12
13
 
13
- And then execute:
14
+ Service initialization will only accept parameters enumerated by ```requires``` and ```recognizes```. ```model```, ```collection```, and ```request``` enumerate supported features and require them directly within the context of the ```model_path``` and ```request_path```.
14
15
 
15
- $ bundle
16
+ ```Mock.data``` is commonly used to store mock data. It is often easiest to use identity to raw response mappings within the ```Mock.data``` hash.
16
17
 
17
- Or install it yourself as:
18
+ ```ruby
18
19
 
19
- $ gem install cistern
20
+ class Foo::Client < Cistern::Service
20
21
 
21
- ## Usage
22
+ model_path "foo/models"
23
+ request_path "foo/requests"
24
+
25
+ model :bar
26
+ collection :bars
27
+ request :create_bar
28
+ request :get_bar
29
+ request :get_bars
30
+
31
+ requires :hmac_id, :hmac_secret
32
+ recognizes :host
33
+
34
+ class Real
35
+ def initialize(options={})
36
+ # setup connection
37
+ end
38
+ end
39
+
40
+ class Mock
41
+ def self.data
42
+ @data ||= {
43
+ :bars => {},
44
+ }
45
+ end
46
+
47
+ def self.reset!
48
+ @data = nil
49
+ end
50
+
51
+ def data
52
+ self.class.data
53
+ end
54
+ def initialize(options={})
55
+ # setup mock data
56
+ end
57
+ end
58
+ end
59
+
60
+ ```
61
+
62
+ ### Model
63
+
64
+ ```connection``` represents the associated ```Foo::Client``` instance.
65
+
66
+ ```ruby
67
+
68
+ class Foo::Client::Bar < Cistern::Model
69
+ identity :id
70
+
71
+ attribute :flavor
72
+ attribute :keypair_id, aliases: "keypair", squash: "id"
73
+ attribute :private_ips, type: :array
74
+
75
+ def destroy
76
+ params = {
77
+ "id" => self.identity
78
+ }
79
+ self.connection.destroy_bar(params).body["request"]
80
+ end
81
+
82
+ def save
83
+ requires :keypair_id
84
+
85
+ params = {
86
+ "keypair" => self.keypair_id,
87
+ "bar" => {
88
+ "flavor" => self.flavor,
89
+ },
90
+ }
91
+
92
+ if new_record?
93
+ request_attributes = connection.create_bar(params).body["request"]
94
+ merge_attributes(new_attributes)
95
+ end
96
+ end
97
+ end
98
+
99
+ ```
100
+
101
+ ### Collection
102
+
103
+ ```model``` tells Cistern which class is contained within the collection. ```Cistern::Collection``` inherits from ```Array``` and lazy loads where applicable.
104
+
105
+ ```ruby
106
+
107
+ class Foo::Client::Bars < Cistern::Collection
108
+
109
+ model Foo::Client::Bar
110
+
111
+ def all(params = {})
112
+ response = connection.get_bars(params)
113
+
114
+ data = self.clone.load(response.body["bars"])
115
+
116
+ collection.attributes.clear
117
+ collection.merge_attributes(data)
118
+ end
119
+
120
+ def discover(provisioned_id, options={})
121
+ params = {
122
+ "provisioned_id" => provisioned_id,
123
+ }
124
+ params.merge!("location" => options[:location]) if options.key?(:location)
125
+
126
+ connection.requests.new(connection.discover_bar(params).body["request"])
127
+ end
128
+
129
+ def get(id)
130
+ if data = connection.get_bar("id" => id).body["bar"]
131
+ new(data)
132
+ else
133
+ nil
134
+ end
135
+ end
136
+ end
137
+
138
+ ```
139
+
140
+ ### Request
141
+
142
+ ```ruby
143
+
144
+ module Foo
145
+ class Client
146
+ class Real
147
+ def create_bar(options={})
148
+ request(
149
+ :body => {"bar" => options},
150
+ :method => :post,
151
+ :path => '/bar'
152
+ )
153
+ end
154
+ end # Real
155
+
156
+ class Mock
157
+ def create_bar(options={})
158
+ id = Foo.random_hex(6)
159
+
160
+ bar = {
161
+ "id" => id
162
+ }.merge(options)
163
+
164
+ self.data[:bars][id]= bar
165
+
166
+ response(
167
+ :body => {"bar" => bar},
168
+ :status => 201,
169
+ :path => '/bar',
170
+ )
171
+ end
172
+ end # Mock
173
+ end # Client
174
+ end # Foo
175
+
176
+ ```
177
+
178
+ ## Examples
22
179
 
23
- TODO: Write usage instructions here
180
+ * [zendesk2](https://github.com/lanej/zendesk2)
24
181
 
25
182
  ## Releasing
26
183
 
@@ -32,4 +189,4 @@ TODO: Write usage instructions here
32
189
  2. Create your feature branch (`git checkout -b my-new-feature`)
33
190
  3. Commit your changes (`git commit -am 'Added some feature'`)
34
191
  4. Push to the branch (`git push origin my-new-feature`)
35
- . Create new Pull Request
192
+ 5. Create new Pull Request
@@ -1,3 +1,3 @@
1
1
  module Cistern
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/cistern.rb CHANGED
@@ -16,6 +16,14 @@ module Cistern
16
16
 
17
17
  def self.timeout=(timeout); @timeout= timeout; end
18
18
  def self.timeout; @timeout || 0; end
19
- def self.formatter; @formatter ||= Cistern::Formatter::Default; end
20
19
  def self.formatter=(formatter); @formatter = formatter; end
20
+
21
+ def self.formatter
22
+ @formatter ||= if defined?(AwesomePrint)
23
+ Cistern::Formatter::AwesomePrint
24
+ elsif defined?(Formatador)
25
+ Cistern::Formatter::Formatador
26
+ else Cistern::Formatter::Default
27
+ end
28
+ end
21
29
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cistern
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
4
  prerelease:
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Josh Lane
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: API client framework extracted from Fog
15
15
  email:
@@ -50,20 +50,20 @@ rdoc_options: []
50
50
  require_paths:
51
51
  - lib
52
52
  required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
53
54
  requirements:
54
55
  - - ! '>='
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
- none: false
58
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
59
60
  requirements:
60
61
  - - ! '>='
61
62
  - !ruby/object:Gem::Version
62
63
  version: '0'
63
- none: false
64
64
  requirements: []
65
65
  rubyforge_project:
66
- rubygems_version: 1.8.24
66
+ rubygems_version: 1.8.25
67
67
  signing_key:
68
68
  specification_version: 3
69
69
  summary: API client framework
@@ -72,4 +72,3 @@ test_files:
72
72
  - spec/collection_spec.rb
73
73
  - spec/model_spec.rb
74
74
  - spec/spec_helper.rb
75
- has_rdoc: