Class: AppStoreDevApi::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/app_store_dev_api/specification.rb,
lib/app_store_dev_api/specification/component/schema.rb

Defined Under Namespace

Modules: Component

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Specification

Returns a new instance of Specification.



8
9
10
# File 'lib/app_store_dev_api/specification.rb', line 8

def initialize(hash)
  @hash = hash
end

Class Method Details

.download(path) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/app_store_dev_api/specification.rb', line 44

def self.download(path)
  uri = URI('https://developer.apple.com/sample-code/app-store-connect/app-store-connect-openapi-specification.zip')

  Net::HTTP.start(uri.host, uri.port, { use_ssl: true }) do |http|
    response = http.get(uri.path)

    File.write(path, response.body)
  end
end

.read(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/app_store_dev_api/specification.rb', line 33

def self.read(path)
  require 'zip'

  Zip::File.open(path) do |zip_file|
    entry, = zip_file.entries
    content = entry.get_input_stream(&:read)

    JSON.parse(content)
  end
end

Instance Method Details

#component_schema(ref) ⇒ Object



18
19
20
# File 'lib/app_store_dev_api/specification.rb', line 18

def component_schema(ref)
  Component::Schema.new(traverse(ref))
end

#create_request_schema_ref(version, type) ⇒ Object



28
29
30
31
# File 'lib/app_store_dev_api/specification.rb', line 28

def create_request_schema_ref(version, type)
  declarations = find(version, type)
  declarations['post']['requestBody']['content']['application/json']['schema']['$ref']
end

#find(version, type) ⇒ Object



12
13
14
15
16
# File 'lib/app_store_dev_api/specification.rb', line 12

def find(version, type)
  @hash['paths'].find do |path, _declaration|
    path == "/#{version}/#{type}"
  end&.[](-1)
end

#traverse(ref) ⇒ Object



22
23
24
25
26
# File 'lib/app_store_dev_api/specification.rb', line 22

def traverse(ref)
  _, *parts = ref.split('/')

  @hash.dig(*parts)
end