class Restroom::Proxy
Attributes
dsl[R]
instance[R]
Public Class Methods
new(instance, dsl)
click to toggle source
# File lib/restroom/proxy.rb, line 8 def initialize(instance, dsl) @instance = instance @dsl = dsl end
Public Instance Methods
all(params={})
click to toggle source
# File lib/restroom/proxy.rb, line 97 def all params={} filter_result(plural_path, :plural, params).map { |data| build data } end
build(data={})
click to toggle source
# File lib/restroom/proxy.rb, line 53 def build data={} model.new(data).tap do |obj| obj.restroom_parent = self end end
connection()
click to toggle source
# File lib/restroom/proxy.rb, line 49 def connection parent.connection end
expand_path(*path)
click to toggle source
# File lib/restroom/proxy.rb, line 63 def expand_path *path path.compact.join('/') end
filter(filter, params={})
click to toggle source
# File lib/restroom/proxy.rb, line 93 def filter filter, params={} filter_result(expand_path(resource_path, filter), :plural, params).map { |data| build data } end
filter_result(path, mode, params={})
click to toggle source
# File lib/restroom/proxy.rb, line 85 def filter_result(path, mode, params={}) response_filter.call(mode, parsed_response(request(:get, path, params))) end
get(key)
click to toggle source
# File lib/restroom/proxy.rb, line 89 def get key build filter_result(singular_path(key), :singular) end
id_method()
click to toggle source
# File lib/restroom/proxy.rb, line 41 def id_method dsl.id end
instance_id()
click to toggle source
# File lib/restroom/proxy.rb, line 59 def instance_id instance.send(dsl.parent.id) end
iterate(filter_by: nil, **args)
click to toggle source
# File lib/restroom/proxy.rb, line 13 def iterate filter_by: nil, **args Enumerator.new do |yielder| page = 1 loop do if filter_by list = filter(filter_by, **args.merge(page: page)) else list = all(**args.merge(page: page)) end page += 1 break if list.empty? list.each { |c| yielder.yield c } end end end
model()
click to toggle source
# File lib/restroom/proxy.rb, line 37 def model dsl.model end
parent()
click to toggle source
# File lib/restroom/proxy.rb, line 45 def parent instance.restroom_parent end
parsed_response(body)
click to toggle source
# File lib/restroom/proxy.rb, line 79 def parsed_response body JSON.parse body rescue JSON::ParseError raise ApiError, "couldn't parse response: #{body[0..20]}" end
plural_path()
click to toggle source
# File lib/restroom/proxy.rb, line 75 def plural_path resource_path end
request(method, path, args={})
click to toggle source
# File lib/restroom/proxy.rb, line 101 def request method, path, args={} response = connection.send(method, path, args) if (200...300).include? response.status return response.body else raise AuthenticationError, 'unauthorised' if response.status == 401 raise AuthenticationError, 'forbidden' if response.status == 403 raise ApiError, response.body[0..100] end rescue Faraday::ClientError => e raise NetworkError, e.message end
resource()
click to toggle source
# File lib/restroom/proxy.rb, line 33 def resource dsl.resource end
resource_path()
click to toggle source
# File lib/restroom/proxy.rb, line 67 def resource_path expand_path(parent.resource_path, instance_id, resource) end
response_filter()
click to toggle source
# File lib/restroom/proxy.rb, line 29 def response_filter dsl.response_filter end
singular_path(key)
click to toggle source
# File lib/restroom/proxy.rb, line 71 def singular_path(key) expand_path(resource_path, key) end