relax 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.2 – June 7, 2012
4
+
5
+ * Move default configuration into `Config`.
6
+ * Add `Configurable` module to encapsulate `#config` and `#configure`.
7
+ * Improve documentation and examples.
8
+
3
9
  ## 0.2.1 – May 30, 2012
4
10
 
5
11
  * Add a timeout configuration option and use it on the resource connection
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Relax
2
2
 
3
+ [![Build Status][travis-image]][travis]
4
+
5
+ [travis]: http://travis-ci.org/tylerhunt/relax
6
+ [travis-image]: https://secure.travis-ci.org/tylerhunt/relax.png
7
+
3
8
  A library of simple modules for building web service wrappers.
4
9
 
5
10
 
@@ -21,6 +26,11 @@ Relax is made up of three primary modules:
21
26
  * `Relax::Delegator` — delegates class methods to an instance of the client
22
27
  allowing for simple client usage
23
28
 
29
+ It also includes a basic configuration class, `Relax::Config`, which defines
30
+ some of client defaults and can used as the basis for your own custom
31
+ configuration classes.
32
+
33
+
24
34
  ### Example
25
35
 
26
36
  ``` ruby
@@ -28,6 +38,8 @@ require 'relax'
28
38
  require 'faraday_middleware' # for JSON response parsing
29
39
 
30
40
  module Vimeo
41
+ extend Relax::Delegator[:client]
42
+
31
43
  class Client
32
44
  include Relax::Client
33
45
 
@@ -58,9 +70,9 @@ module Vimeo
58
70
  end
59
71
  end
60
72
 
61
- extend Relax::Delegator
62
-
63
- delegate_to Client
73
+ def self.client
74
+ @client ||= Client.new
75
+ end
64
76
  end
65
77
 
66
78
  Vimeo.user(ENV['VIMEO_USERNAME']).videos
@@ -1,19 +1,30 @@
1
+ # This example shows:
2
+ #
3
+ # - inheriting from Relax::Config and overriding Client#config to use it
4
+ # - customizing the resource to use basic auth and XML response parsing
5
+ # - instantiating multiple clients with distinct configurations
6
+ # - modifying a client instance endpoint to point to a different API
7
+
1
8
  require 'relax'
2
9
  require 'multi_xml'
3
10
  require 'faraday_middleware'
4
11
 
5
12
  module Delicious
6
- module Config
13
+ class Config < Relax::Config
7
14
  attr :username, true
8
15
  attr :password, true
16
+
17
+ def initialize
18
+ super
19
+ self.base_uri = 'https://api.del.icio.us/v1'
20
+ end
9
21
  end
10
22
 
11
23
  class Client
12
24
  include Relax::Client
13
25
 
14
- def initialize
15
- config.base_uri = 'https://api.del.icio.us/v1'
16
- config.extend(Config)
26
+ def config
27
+ @config ||= Config.new
17
28
  end
18
29
 
19
30
  def posts
@@ -1,9 +1,15 @@
1
+ # This example shows:
2
+ #
3
+ # - overriding the default configuration values in Client#initialize
4
+ # - extending the configuration with custom values using config.extend
5
+ # - overriding Resource#get to merge in custom parameters
6
+ # - customizing the resource to use JSON response parsing
7
+ # - providing a module-level client accessor without using delegation
8
+
1
9
  require 'relax'
2
10
  require 'faraday_middleware'
3
11
 
4
12
  module Flickr
5
- extend Relax::Delegator[:client]
6
-
7
13
  module Config
8
14
  attr :api_key, true
9
15
  end
@@ -36,9 +42,7 @@ module Flickr
36
42
  end
37
43
 
38
44
  def connection
39
- super do |builder|
40
- builder.response(:json)
41
- end
45
+ super { |builder| builder.response(:json) }
42
46
  end
43
47
  end
44
48
 
@@ -57,10 +61,10 @@ module Flickr
57
61
  end
58
62
  end
59
63
 
60
- Flickr.configure do |config|
64
+ Flickr.client.configure do |config|
61
65
  config.api_key = ENV['FLICKR_API_KEY']
62
66
  end
63
67
 
64
- photos = Flickr.search.photos('ruby')
68
+ photos = Flickr.client.search.photos('ruby')
65
69
 
66
70
  puts photos.first(10).collect { |photo| photo['title'] }
@@ -1,3 +1,9 @@
1
+ # This example shows:
2
+ #
3
+ # - using Relax::Delegator to make the root module behave like a client
4
+ # - how to create resources that accept options for resource-level defaults
5
+ # - customizing the resource to use JSON response parsing
6
+
1
7
  require 'relax'
2
8
  require 'faraday_middleware'
3
9
 
@@ -3,6 +3,7 @@ require 'relax/version'
3
3
  module Relax
4
4
  autoload :Client, 'relax/client'
5
5
  autoload :Config, 'relax/config'
6
+ autoload :Configurable, 'relax/configurable'
6
7
  autoload :Delegator, 'relax/delegator'
7
8
  autoload :Resource, 'relax/resource'
8
9
  end
@@ -1,20 +1,5 @@
1
- require 'faraday'
2
-
3
1
  module Relax
4
2
  module Client
5
- USER_AGENT = "Relax Ruby Gem Client #{Relax::VERSION}"
6
- TIMEOUT = 60
7
-
8
- def config
9
- @config ||= Config.new.configure do |config|
10
- config.adapter = Faraday.default_adapter
11
- config.user_agent = USER_AGENT
12
- config.timeout = TIMEOUT
13
- end
14
- end
15
-
16
- def configure(&block)
17
- config.configure(&block)
18
- end
3
+ include Configurable
19
4
  end
20
5
  end
@@ -1,13 +1,19 @@
1
+ require 'faraday'
2
+
1
3
  module Relax
2
4
  class Config
5
+ USER_AGENT = "Relax Ruby Gem Client #{Relax::VERSION}"
6
+ TIMEOUT = 60
7
+
3
8
  attr :adapter, true
4
9
  attr :base_uri, true
5
10
  attr :timeout, true
6
11
  attr :user_agent, true
7
12
 
8
- def configure
9
- yield(self)
10
- self
13
+ def initialize
14
+ self.adapter = Faraday.default_adapter
15
+ self.user_agent = USER_AGENT
16
+ self.timeout = TIMEOUT
11
17
  end
12
18
  end
13
19
  end
@@ -0,0 +1,12 @@
1
+ module Relax
2
+ module Configurable
3
+ def config
4
+ @config ||= Config.new
5
+ end
6
+
7
+ def configure
8
+ yield(config)
9
+ self
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Relax
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -7,6 +7,8 @@ Gem::Specification.new do |gem|
7
7
  gem.homepage = 'http://github.com/tylerhunt/relax'
8
8
  gem.author = 'Tyler Hunt'
9
9
 
10
+ gem.required_ruby_version = '>= 1.9'
11
+
10
12
  gem.add_dependency 'faraday', '~> 0.8.0'
11
13
  gem.add_development_dependency 'rspec', '~> 2.6'
12
14
 
@@ -13,15 +13,6 @@ describe Relax::Client do
13
13
  it 'memoizes the configuration' do
14
14
  subject.config.should == subject.config
15
15
  end
16
-
17
- context 'defaults' do
18
- subject { client.config }
19
-
20
- its(:adapter) { should == Faraday.default_adapter }
21
- its(:base_uri) { should be_nil }
22
- its(:timeout) { should == 60 }
23
- its(:user_agent) { should == "Relax Ruby Gem Client #{Relax::VERSION}" }
24
- end
25
16
  end
26
17
 
27
18
  context '#configure' do
@@ -33,8 +24,8 @@ describe Relax::Client do
33
24
  }.to change(subject.config, :base_uri).to('http://api.example.com/v2')
34
25
  end
35
26
 
36
- it 'returns the configuration' do
37
- subject.configure { }.should == subject.config
27
+ it 'returns self' do
28
+ subject.configure { }.should == client
38
29
  end
39
30
  end
40
31
  end
@@ -1,17 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Relax::Config do
4
- context '#configure' do
5
- it 'yields an instance of the configuration' do
6
- expect {
7
- subject.configure do |config|
8
- config.base_uri = 'http://api.example.com/v2'
9
- end
10
- }.to change(subject, :base_uri).to('http://api.example.com/v2')
4
+ {
5
+ USER_AGENT: "Relax Ruby Gem Client #{Relax::VERSION}",
6
+ TIMEOUT: 60
7
+ }.each do |constant, value|
8
+ context "::#{constant}" do
9
+ subject { described_class.const_get(constant) }
10
+
11
+ it { should == value }
11
12
  end
13
+ end
14
+
15
+ context '.new' do
16
+ subject { described_class.new }
12
17
 
13
- it 'returns the configuration' do
14
- subject.configure { }.should == subject
18
+ context 'defaults' do
19
+ its(:adapter) { should == Faraday.default_adapter }
20
+ its(:base_uri) { should be_nil }
21
+ its(:timeout) { should == 60 }
22
+ its(:user_agent) { should == "Relax Ruby Gem Client #{Relax::VERSION}" }
15
23
  end
16
24
  end
17
25
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Relax::Configurable do
4
+ let(:configurable) { Class.new { include Relax::Configurable }.new }
5
+
6
+ subject { configurable }
7
+
8
+ context '#config' do
9
+ it 'returns an instance of Relax::Config' do
10
+ subject.config.should be_a(Relax::Config)
11
+ end
12
+
13
+ it 'memoizes the configuration' do
14
+ subject.config.should == subject.config
15
+ end
16
+ end
17
+
18
+ context '#configure' do
19
+ it 'yields an instance of the configuration' do
20
+ expect {
21
+ subject.configure do |config|
22
+ config.base_uri = 'http://api.example.com/v2'
23
+ end
24
+ }.to change(subject.config, :base_uri).to('http://api.example.com/v2')
25
+ end
26
+
27
+ it 'returns self' do
28
+ subject.configure { }.should == configurable
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-31 00:00:00.000000000 Z
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -51,6 +51,7 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - .gitignore
53
53
  - .rspec
54
+ - .travis.yml
54
55
  - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE
@@ -62,12 +63,14 @@ files:
62
63
  - lib/relax.rb
63
64
  - lib/relax/client.rb
64
65
  - lib/relax/config.rb
66
+ - lib/relax/configurable.rb
65
67
  - lib/relax/delegator.rb
66
68
  - lib/relax/resource.rb
67
69
  - lib/relax/version.rb
68
70
  - relax.gemspec
69
71
  - spec/relax/client_spec.rb
70
72
  - spec/relax/config_spec.rb
73
+ - spec/relax/configurable_spec.rb
71
74
  - spec/relax/delegator_spec.rb
72
75
  - spec/relax/resource_spec.rb
73
76
  - spec/spec_helper.rb
@@ -82,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
85
  requirements:
83
86
  - - ! '>='
84
87
  - !ruby/object:Gem::Version
85
- version: '0'
88
+ version: '1.9'
86
89
  required_rubygems_version: !ruby/object:Gem::Requirement
87
90
  none: false
88
91
  requirements:
@@ -98,6 +101,7 @@ summary: A flexible library for creating web service consumers.
98
101
  test_files:
99
102
  - spec/relax/client_spec.rb
100
103
  - spec/relax/config_spec.rb
104
+ - spec/relax/configurable_spec.rb
101
105
  - spec/relax/delegator_spec.rb
102
106
  - spec/relax/resource_spec.rb
103
107
  - spec/spec_helper.rb