restroom 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfe0db3505141774d2a5408400265d2583936d22
4
+ data.tar.gz: 32d998f6b20581d5ba0a1a931a48dd71371ac82c
5
+ SHA512:
6
+ metadata.gz: 79168d50ff9ce7570b8c087423061d3fb2b258ead81baa3dd3949b3fe4f76e818fc61795720b48b6c8ecd94947d5bed375c026726ab477fdb6541de453f8aa59
7
+ data.tar.gz: 494cf28b04183c8c01bd8ba7f693bd7ad6ee85e6e0c5ec4834ef07ac3088c9da142f82bb9ae6cbd2580ac3dbfaa71a1045b8db0dffda824df17a60d23f7d0988
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ restroom
@@ -0,0 +1 @@
1
+ ruby-2.2.3
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restroom.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Fairfax Media
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Restroom
2
+
3
+ Restroom provides a DSL to quickly and easily describe a RESTful and build a gem around it. It was extracted during the development of a Bitbucket API gem, thus the examples below.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'restroom'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install restroom
20
+
21
+ ## Usage
22
+
23
+ Here's the client code from the [Bitbucket2](http://github.com/fairfax/bitbucket2) gem:
24
+
25
+ ```
26
+ module Bitbucket2
27
+ class Client
28
+ include Restroom
29
+
30
+ restroom 'https://api.bitbucket.org', base_path: '2.0' do
31
+ exposes :repositories, class: Repository, id: :full_name do
32
+ exposes :commits, class: Commit, id: :hash
33
+ exposes :pull_requests, resource: 'pullrequests', class: PullRequest do
34
+ exposes :commits, class: Commit, id: :hash
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ ```
41
+
42
+ ...and that's it - apart from some simple model files (for which I like to use Virtus):
43
+
44
+ ```
45
+ module Bitbucket2
46
+ class Commit
47
+ include Virtus.model
48
+
49
+ attribute :hash, String
50
+ end
51
+ end
52
+ ```
53
+
54
+ ...which are instantiated with a hash of attributes extracted from the API's returned JSON.
55
+
56
+ The `exposes` invocation takes several options:
57
+
58
+ - a key which is used to build the relation methods (so we can call Bitbucket2::Client.new.repositories, in this case),
59
+ - a class to instantiate,
60
+ - a id for building nested paths (so in the case of repositories we use the `full_name` attribute).
61
+
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at http://github.com/fairfaxmedia/restroom.
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "restroom"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,66 @@
1
+ require "restroom/version"
2
+ require 'restroom/proxy'
3
+ require 'restroom/relation'
4
+ require 'restroom/context'
5
+
6
+ require 'active_support/inflector'
7
+ require 'faraday'
8
+ require 'faraday_middleware'
9
+ require 'json'
10
+
11
+
12
+ module Restroom
13
+ def self.included(base)
14
+ base.extend(ClassMethods)
15
+ end
16
+
17
+ def restroom_parent
18
+ self.class
19
+ end
20
+
21
+ def id
22
+ nil
23
+ end
24
+
25
+ module ClassMethods
26
+ attr_reader :endpoint, :base_path
27
+
28
+ def restroom(endpoint, base_path: nil, &block)
29
+ @endpoint = endpoint
30
+ @base_path = base_path
31
+ Context.new(host: self, parent: self, &block)
32
+ end
33
+
34
+ def resource_path
35
+ base_path
36
+ end
37
+
38
+ def model
39
+ self
40
+ end
41
+
42
+ def response_filter
43
+ default_response_filter
44
+ end
45
+
46
+ def default_response_filter
47
+ Proc.new { |mode, response| response }
48
+ end
49
+
50
+ def stack
51
+ end
52
+
53
+ def connection
54
+ @connection ||= Faraday.new endpoint do |config|
55
+ stack(config)
56
+ config.adapter Faraday.default_adapter
57
+ end
58
+ end
59
+ end
60
+
61
+ class Error < StandardError; end
62
+ class ApiError < Error; end
63
+ class DataError < Error; end
64
+ class NetworkError < Error; end
65
+ class AuthenticationError < Error; end
66
+ end
@@ -0,0 +1,61 @@
1
+ require 'restroom/relation'
2
+
3
+ module Restroom
4
+ class Context
5
+
6
+ STRUCTURE = %I<children host parent key>
7
+ ATTRIBUTES = %I<resource model id response_filter>
8
+ INHERITABLE = %I<response_filter>
9
+
10
+ attr_reader *STRUCTURE
11
+ attr_accessor *ATTRIBUTES
12
+
13
+
14
+ INHERITABLE.each do |attr|
15
+ define_method attr do |value=nil|
16
+ return instance_variable_set("@#{attr}", value) if value
17
+ instance_variable_get("@#{attr}") || @parent.send(attr)
18
+ end
19
+ end
20
+
21
+ def initialize(host: nil, parent:, key: nil, **args, &block)
22
+ @children = []
23
+ @id = :id
24
+
25
+ args.each { |k, v| send "#{k}=", v }
26
+ instance_eval &block if block_given?
27
+
28
+ @key = key
29
+ @resource ||= key
30
+ @model ||= classify_resource(@resource)
31
+ @host = host || model # TODO guess model from key
32
+ @parent = parent
33
+
34
+ @model.send(:attr_accessor, :restroom_parent) if @model
35
+
36
+ @host.include Relation
37
+ @children.each do |child|
38
+ @host.add_relation(child.key, child)
39
+ end
40
+ end
41
+
42
+ def classify_resource(resource)
43
+ resource.to_s.classify.constantize if resource
44
+ end
45
+
46
+ def exposes key, **args, &block
47
+ @children << child = self.class.new(key: key, parent: self, **args, &block)
48
+ end
49
+
50
+ def dump
51
+ dumper(self, 0)
52
+ end
53
+
54
+ def dumper context, depth
55
+ puts "#{' ' * depth}#{context} - host: #{context.host}, parent: #{context.parent}, class: #{context.model}, id: #{context.id}"#, filter: #{context.response_filter}"
56
+ context.children.each do |child|
57
+ dumper(child, depth+1)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,114 @@
1
+ require 'restroom/relation'
2
+
3
+ module Restroom
4
+ class Proxy
5
+ include Relation
6
+ attr_reader :instance, :dsl
7
+
8
+ def initialize(instance, dsl)
9
+ @instance = instance
10
+ @dsl = dsl
11
+ end
12
+
13
+ def iterate filter_by: nil, **args
14
+ Enumerator.new do |yielder|
15
+ page = 1
16
+ loop do
17
+ if filter_by
18
+ list = filter(filter_by, **args.merge(page: page))
19
+ else
20
+ list = all(**args.merge(page: page))
21
+ end
22
+ page += 1
23
+ break if list.empty?
24
+ list.each { |c| yielder.yield c }
25
+ end
26
+ end
27
+ end
28
+
29
+ def response_filter
30
+ dsl.response_filter
31
+ end
32
+
33
+ def resource
34
+ dsl.resource
35
+ end
36
+
37
+ def model
38
+ dsl.model
39
+ end
40
+
41
+ def id_method
42
+ dsl.id
43
+ end
44
+
45
+ def parent
46
+ instance.restroom_parent
47
+ end
48
+
49
+ def connection
50
+ parent.connection
51
+ end
52
+
53
+ def build data={}
54
+ model.new(data).tap do |obj|
55
+ obj.restroom_parent = self
56
+ end
57
+ end
58
+
59
+ def instance_id
60
+ instance.send(dsl.parent.id)
61
+ end
62
+
63
+ def expand_path *path
64
+ path.compact.join('/')
65
+ end
66
+
67
+ def resource_path
68
+ expand_path(parent.resource_path, instance_id, resource)
69
+ end
70
+
71
+ def singular_path(key)
72
+ expand_path(resource_path, key)
73
+ end
74
+
75
+ def plural_path
76
+ resource_path
77
+ end
78
+
79
+ def parsed_response body
80
+ JSON.parse body
81
+ rescue JSON::ParseError
82
+ raise ApiError, "couldn't parse response: #{body[0..20]}"
83
+ end
84
+
85
+ def filter_result(path, mode, params={})
86
+ response_filter.call(mode, parsed_response(request(:get, path, params)))
87
+ end
88
+
89
+ def get key
90
+ build filter_result(singular_path(key), :singular)
91
+ end
92
+
93
+ def filter filter, params={}
94
+ filter_result(expand_path(resource_path, filter), :plural, params).map { |data| build data }
95
+ end
96
+
97
+ def all params={}
98
+ filter_result(plural_path, :plural, params).map { |data| build data }
99
+ end
100
+
101
+ def request method, path, args={}
102
+ response = connection.send(method, path, args)
103
+ if (200...300).include? response.status
104
+ return response.body
105
+ else
106
+ raise AuthenticationError, 'unauthorised' if response.status == 401
107
+ raise AuthenticationError, 'forbidden' if response.status == 403
108
+ raise ApiError, response.body[0..100]
109
+ end
110
+ rescue Faraday::ClientError => e
111
+ raise NetworkError, e.message
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,16 @@
1
+ module Restroom
2
+ module Relation
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def add_relation key, dsl
9
+ # TODO Check for collision
10
+ define_method key do
11
+ Proxy.new self, dsl
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Restroom
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restroom/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "restroom"
8
+ spec.version = Restroom::VERSION
9
+ spec.authors = ["Simon Hildebrandt"]
10
+ spec.email = ["simon.hildebrandt@fairfaxmedia.com.au"]
11
+
12
+ spec.summary = %q{RESTful api gem scaffolding}
13
+ spec.description = %q{Restroom provides an expressive DSL for quickly implementing wrapper gems for REST APIs.}
14
+ spec.homepage = "http://github.com/fairfaxmedia/restroom"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.add_runtime_dependency 'faraday'
22
+ spec.add_runtime_dependency 'activesupport'
23
+ spec.add_runtime_dependency 'json'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "byebug"
30
+ spec.add_development_dependency 'faraday_middleware'
31
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restroom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Hildebrandt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: faraday_middleware
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Restroom provides an expressive DSL for quickly implementing wrapper
140
+ gems for REST APIs.
141
+ email:
142
+ - simon.hildebrandt@fairfaxmedia.com.au
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".ruby-gemset"
150
+ - ".ruby-version"
151
+ - ".travis.yml"
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/restroom.rb
159
+ - lib/restroom/context.rb
160
+ - lib/restroom/proxy.rb
161
+ - lib/restroom/relation.rb
162
+ - lib/restroom/version.rb
163
+ - restroom.gemspec
164
+ homepage: http://github.com/fairfaxmedia/restroom
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.4.5.1
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: RESTful api gem scaffolding
188
+ test_files: []