Method: Agave::Repo#initialize
- Defined in:
- lib/agave/repo.rb
#initialize(client, type, schema) ⇒ Repo
Returns a new instance of Repo.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/agave/repo.rb', line 17 def initialize(client, type, schema) @client = client @type = type @schema = schema schema.links.each do |link| method_name = METHOD_NAMES.fetch(link.rel, link.rel) self.define_singleton_method(method_name) do |*args| min_arguments_count = [ link.href.scan(IDENTITY_REGEXP).size, link.schema && link.method != :get ? 1 : 0 ].reduce(0, :+) args.size >= min_arguments_count or raise ArgumentError.new( "wrong number of arguments (given #{args.size}, expected #{min_arguments_count})" ) placeholders = [] url = link["href"].gsub(IDENTITY_REGEXP) do |_stuff| placeholder = args.shift.to_s placeholders << placeholder placeholder end response = if i(post put).include?(link.method) body = if link.schema unserialized_body = args.shift JsonApiSerializer.new(type, link).serialize( unserialized_body, link.method == :post ? nil : placeholders.last ) else {} end client.request(link.method, url, body) elsif link.method == :delete client.request(:delete, url) elsif link.method == :get query_string = args.shift all_pages = (args[0] || {}). symbolize_keys. fetch(:all_pages, false) is_paginated_endpoint = link.schema && link.schema.properties.has_key?("page[limit]") if is_paginated_endpoint && all_pages Paginator.new(client, url, query_string).response else client.request(:get, url, query_string) end end = if args.any? args.shift.symbolize_keys else {} end if .fetch(:deserialize_response, true) JsonApiDeserializer.new.deserialize(response) else response end end end end |