relax 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +63 -0
- data/Gemfile +3 -0
- data/LICENSE +3 -1
- data/README.md +80 -0
- data/Rakefile +2 -52
- data/examples/delicious.rb +61 -0
- data/examples/flickr.rb +66 -0
- data/examples/vimeo.rb +65 -0
- data/lib/relax.rb +6 -16
- data/lib/relax/client.rb +18 -0
- data/lib/relax/config.rb +12 -0
- data/lib/relax/delegator.rb +25 -0
- data/lib/relax/resource.rb +32 -0
- data/lib/relax/version.rb +3 -0
- data/relax.gemspec +17 -0
- data/spec/relax/client_spec.rb +39 -0
- data/spec/relax/config_spec.rb +17 -0
- data/spec/relax/delegator_spec.rb +32 -0
- data/spec/relax/resource_spec.rb +60 -0
- data/spec/spec_helper.rb +9 -13
- metadata +75 -96
- data/README.rdoc +0 -194
- data/VERSION.yml +0 -4
- data/lib/relax/action.rb +0 -49
- data/lib/relax/context.rb +0 -46
- data/lib/relax/contextable.rb +0 -15
- data/lib/relax/endpoint.rb +0 -21
- data/lib/relax/instance.rb +0 -25
- data/lib/relax/parameter.rb +0 -23
- data/lib/relax/performer.rb +0 -58
- data/lib/relax/service.rb +0 -45
- data/spec/relax/context_spec.rb +0 -18
- data/spec/relax/endpoint_spec.rb +0 -99
- data/spec/relax/integration_spec.rb +0 -63
- data/spec/relax/service_spec.rb +0 -32
- data/spec/services/blank_values_service.rb +0 -19
- data/spec/services/custom_parser_service.rb +0 -24
- data/spec/services/flickr.rb +0 -78
- data/spec/services/parameter_alias_service.rb +0 -17
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 0.2.0 – Unreleased
|
4
|
+
|
5
|
+
* Rewrote from scratch to be a smaller, simpler library
|
6
|
+
|
7
|
+
## 0.1.3 – August 31, 2009
|
8
|
+
|
9
|
+
* Added basic request logging
|
10
|
+
* Updated to Relief 0.0.4
|
11
|
+
|
12
|
+
## 0.1.2 – June 30, 2009
|
13
|
+
|
14
|
+
* Added support for proxies
|
15
|
+
* Added support for parameter name aliases
|
16
|
+
* Added an option to force blank values to be used
|
17
|
+
|
18
|
+
## 0.1.1 – June 3, 2009
|
19
|
+
|
20
|
+
* Added support for tokenized URL parameters
|
21
|
+
* Modified Service#authenticate to return self to allow chaining
|
22
|
+
* Extended parsers to allow for custom parser classes (Nathaniel Bibler)
|
23
|
+
|
24
|
+
## 0.1.0 – May 7, 2009
|
25
|
+
|
26
|
+
* Rewrote the API to use a DSL interface
|
27
|
+
* Replaced the embedded parsers with Relief
|
28
|
+
|
29
|
+
## 0.0.7 – April 27, 2009
|
30
|
+
|
31
|
+
* Added support for required request parameters
|
32
|
+
|
33
|
+
## 0.0.6 – February 3, 2009
|
34
|
+
|
35
|
+
* Fixed attribute-based parameters
|
36
|
+
* Fixed an issue with `node_name` in the new parsers (Nathaniel Bibler)
|
37
|
+
|
38
|
+
## 0.0.5 – October 20, 2008
|
39
|
+
|
40
|
+
* Added support for multiple parsers (Nathaniel Bibler)
|
41
|
+
* Added a REXML parser (Nathaniel Bibler)
|
42
|
+
* Added a GitHub gemspec and a Rake task for regenerating it
|
43
|
+
|
44
|
+
## 0.0.4 – November 17, 2007
|
45
|
+
|
46
|
+
* Added support for custom Request parameter types
|
47
|
+
|
48
|
+
## 0.0.3 – November 1, 2007
|
49
|
+
|
50
|
+
* Fixed that parameter values defined in ancestors weren't passed to children
|
51
|
+
* Added date and time parameter types to Response
|
52
|
+
* Added an accessor for the raw XML to Response
|
53
|
+
|
54
|
+
## 0.0.2 – October 10, 2007
|
55
|
+
|
56
|
+
* Renamed API to Service
|
57
|
+
* Added tutorial to the README
|
58
|
+
* Added Rdoc documentation
|
59
|
+
* Added support for custom Response parameter types
|
60
|
+
|
61
|
+
## 0.0.1 – September 27, 2007
|
62
|
+
|
63
|
+
* First public release
|
data/Gemfile
ADDED
data/LICENSE
CHANGED
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Relax
|
2
|
+
|
3
|
+
A library of simple modules for building web service wrappers.
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Relax a toolkit of sorts, which makes it easier to write Faraday-based API
|
9
|
+
Gems while still leaning heavily on the strengths of the Ruby language.
|
10
|
+
|
11
|
+
### Overview
|
12
|
+
|
13
|
+
Relax is made up of three primary modules:
|
14
|
+
|
15
|
+
* `Relax::Client` — forms the basis of a web service wrapper by storing the
|
16
|
+
configuration and serving as a factory for resources
|
17
|
+
|
18
|
+
* `Relax::Resource` — used to handle the actual web service connections and
|
19
|
+
help organize related endpoints
|
20
|
+
|
21
|
+
* `Relax::Delegator` — delegates class methods to an instance of the client
|
22
|
+
allowing for simple client usage
|
23
|
+
|
24
|
+
### Example
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
require 'relax'
|
28
|
+
require 'faraday_middleware' # for JSON response parsing
|
29
|
+
|
30
|
+
module Vimeo
|
31
|
+
class Client
|
32
|
+
include Relax::Client
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
config.base_uri = 'http://vimeo.com/api/v2'
|
36
|
+
end
|
37
|
+
|
38
|
+
def user(username)
|
39
|
+
Resources::User.new(self, username: username)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module Resource
|
44
|
+
include Relax::Resource
|
45
|
+
|
46
|
+
def connection
|
47
|
+
super { |builder| builder.response(:json) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module Resources
|
52
|
+
class User
|
53
|
+
include Resource
|
54
|
+
|
55
|
+
def videos
|
56
|
+
get("#{@options[:username]}/videos.json").body
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
extend Relax::Delegator
|
62
|
+
|
63
|
+
delegate_to Client
|
64
|
+
end
|
65
|
+
|
66
|
+
Vimeo.user(ENV['VIMEO_USERNAME']).videos
|
67
|
+
```
|
68
|
+
|
69
|
+
See the [`examples` directory][examples] for more.
|
70
|
+
|
71
|
+
[examples]: http://github.com/tylerhunt/relax/examples
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it.
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature.'`).
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
80
|
+
5. Create a new Pull Request.
|
data/Rakefile
CHANGED
@@ -1,53 +1,3 @@
|
|
1
|
-
|
2
|
-
require 'rake'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
require 'spec/rake/spectask'
|
1
|
+
#!/usr/bin/env rake
|
5
2
|
|
6
|
-
|
7
|
-
require 'jeweler'
|
8
|
-
|
9
|
-
Jeweler::Tasks.new do |gem|
|
10
|
-
gem.name = "relax"
|
11
|
-
gem.summary = %Q{A flexible library for creating web service consumers.}
|
12
|
-
gem.email = "tyler@tylerhunt.com"
|
13
|
-
gem.homepage = "http://github.com/tylerhunt/relax"
|
14
|
-
gem.authors = ["Tyler Hunt"]
|
15
|
-
gem.rubyforge_project = 'relax'
|
16
|
-
|
17
|
-
gem.add_dependency('rest-client', '~> 0.9.2')
|
18
|
-
gem.add_dependency('relief', '~> 0.0.4')
|
19
|
-
|
20
|
-
gem.add_development_dependency('jeweler', '~> 0.11.0')
|
21
|
-
gem.add_development_dependency('rspec', '~> 1.2.2')
|
22
|
-
end
|
23
|
-
rescue LoadError
|
24
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
25
|
-
end
|
26
|
-
|
27
|
-
task :default => :spec
|
28
|
-
|
29
|
-
Spec::Rake::SpecTask.new(:spec) do |spec|
|
30
|
-
spec.libs << 'lib' << 'spec'
|
31
|
-
spec.spec_files = FileList['spec/**/*_spec.rb']
|
32
|
-
end
|
33
|
-
|
34
|
-
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
35
|
-
spec.libs << 'lib' << 'spec'
|
36
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
-
spec.rcov = true
|
38
|
-
end
|
39
|
-
|
40
|
-
Rake::RDocTask.new do |rdoc|
|
41
|
-
if File.exist?('VERSION.yml')
|
42
|
-
config = YAML.load(File.read('VERSION.yml'))
|
43
|
-
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
44
|
-
else
|
45
|
-
version = ""
|
46
|
-
end
|
47
|
-
|
48
|
-
rdoc.rdoc_dir = 'rdoc'
|
49
|
-
rdoc.title = "relief #{version}"
|
50
|
-
rdoc.rdoc_files.include('README*')
|
51
|
-
rdoc.rdoc_files.include('LICENSE*')
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
-
end
|
3
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'relax'
|
2
|
+
require 'multi_xml'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
|
5
|
+
module Delicious
|
6
|
+
module Config
|
7
|
+
attr :username, true
|
8
|
+
attr :password, true
|
9
|
+
end
|
10
|
+
|
11
|
+
class Client
|
12
|
+
include Relax::Client
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
config.base_uri = 'https://api.del.icio.us/v1'
|
16
|
+
config.extend(Config)
|
17
|
+
end
|
18
|
+
|
19
|
+
def posts
|
20
|
+
@posts ||= Resources::Posts.new(self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Resource
|
25
|
+
include Relax::Resource
|
26
|
+
|
27
|
+
def connection
|
28
|
+
super do |builder|
|
29
|
+
builder.basic_auth(config.username, config.password)
|
30
|
+
builder.response(:xml)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module Resources
|
36
|
+
class Posts
|
37
|
+
include Resource
|
38
|
+
|
39
|
+
def recent(params={})
|
40
|
+
get('posts/recent', params).body['posts']['post']
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
delicious = Delicious::Client.new
|
47
|
+
pinboard = Delicious::Client.new
|
48
|
+
|
49
|
+
delicious.configure do |config|
|
50
|
+
config.username = ENV['DELICIOUS_USERNAME']
|
51
|
+
config.password = ENV['DELICIOUS_PASSWORD']
|
52
|
+
end
|
53
|
+
|
54
|
+
pinboard.configure do |config|
|
55
|
+
config.base_uri = 'https://api.pinboard.in/v1'
|
56
|
+
config.username = ENV['PINBOARD_USERNAME']
|
57
|
+
config.password = ENV['PINBOARD_PASSWORD']
|
58
|
+
end
|
59
|
+
|
60
|
+
puts delicious.posts.recent.first(10).collect { |post| post['href'] }
|
61
|
+
puts pinboard.posts.recent.first(10).collect { |post| post['href'] }
|
data/examples/flickr.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'relax'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
module Flickr
|
5
|
+
extend Relax::Delegator[:client]
|
6
|
+
|
7
|
+
module Config
|
8
|
+
attr :api_key, true
|
9
|
+
end
|
10
|
+
|
11
|
+
class Client
|
12
|
+
include Relax::Client
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
config.base_uri = 'http://api.flickr.com/services/rest/'
|
16
|
+
config.extend(Config)
|
17
|
+
end
|
18
|
+
|
19
|
+
def search
|
20
|
+
@search ||= Resources::Search.new(self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Resource
|
25
|
+
include Relax::Resource
|
26
|
+
|
27
|
+
def get(method, parameters={})
|
28
|
+
parameters.merge!(
|
29
|
+
method: method,
|
30
|
+
api_key: config.api_key,
|
31
|
+
format: :json,
|
32
|
+
nojsoncallback: 1
|
33
|
+
)
|
34
|
+
|
35
|
+
super(nil, parameters)
|
36
|
+
end
|
37
|
+
|
38
|
+
def connection
|
39
|
+
super do |builder|
|
40
|
+
builder.response(:json)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module Resources
|
46
|
+
class Search
|
47
|
+
include Resource
|
48
|
+
|
49
|
+
def photos(tags)
|
50
|
+
get('flickr.photos.search', tags: tags).body['photos']['photo']
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.client
|
56
|
+
@client ||= Client.new
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Flickr.configure do |config|
|
61
|
+
config.api_key = ENV['FLICKR_API_KEY']
|
62
|
+
end
|
63
|
+
|
64
|
+
photos = Flickr.search.photos('ruby')
|
65
|
+
|
66
|
+
puts photos.first(10).collect { |photo| photo['title'] }
|
data/examples/vimeo.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'relax'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
module Vimeo
|
5
|
+
extend Relax::Delegator[:client]
|
6
|
+
|
7
|
+
class Client
|
8
|
+
include Relax::Client
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
config.base_uri = 'http://vimeo.com/api/v2'
|
12
|
+
end
|
13
|
+
|
14
|
+
def user(username)
|
15
|
+
Resources::User.new(self, username: username)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Resource
|
20
|
+
include Relax::Resource
|
21
|
+
|
22
|
+
def connection
|
23
|
+
super { |builder| builder.response(:json) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Resources
|
28
|
+
class User
|
29
|
+
include Resource
|
30
|
+
|
31
|
+
def info
|
32
|
+
get("#{@options[:username]}/info.json").body
|
33
|
+
end
|
34
|
+
|
35
|
+
def videos
|
36
|
+
get("#{@options[:username]}/videos.json").body
|
37
|
+
end
|
38
|
+
|
39
|
+
def likes
|
40
|
+
get("#{@options[:username]}/likes.json").body
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.client
|
46
|
+
@client ||= Client.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
vimeo_user = Vimeo.user(ENV['VIMEO_USERNAME'])
|
51
|
+
|
52
|
+
info = vimeo_user.info
|
53
|
+
videos = vimeo_user.videos.first(10)
|
54
|
+
likes = vimeo_user.likes.first(10)
|
55
|
+
|
56
|
+
puts <<OUTPUT
|
57
|
+
Name: #{info['display_name']}
|
58
|
+
Location: #{info['location']}
|
59
|
+
|
60
|
+
Videos:
|
61
|
+
#{videos.collect { |post| "- #{post['title']}" }.join("\n")}
|
62
|
+
|
63
|
+
Likes:
|
64
|
+
#{likes.collect { |post| "- #{post['title']}" }.join("\n")}
|
65
|
+
OUTPUT
|
data/lib/relax.rb
CHANGED
@@ -1,18 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'relax/version'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
module Relax # :nodoc:
|
10
|
-
autoload :Action, 'relax/action'
|
11
|
-
autoload :Context, 'relax/context'
|
12
|
-
autoload :Contextable, 'relax/contextable'
|
13
|
-
autoload :Endpoint, 'relax/endpoint'
|
14
|
-
autoload :Instance, 'relax/instance'
|
15
|
-
autoload :Parameter, 'relax/parameter'
|
16
|
-
autoload :Performer, 'relax/performer'
|
17
|
-
autoload :Service, 'relax/service'
|
3
|
+
module Relax
|
4
|
+
autoload :Client, 'relax/client'
|
5
|
+
autoload :Config, 'relax/config'
|
6
|
+
autoload :Delegator, 'relax/delegator'
|
7
|
+
autoload :Resource, 'relax/resource'
|
18
8
|
end
|