Class: Spaceship::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spaceship/base.rb

Overview

Spaceship::Base is the superclass for models in Apple Developer Portal. It’s mainly responsible for mapping responses to objects.

A class-level attribute ‘client` is used to maintain the which spaceship we are using to talk to ADP.

Example of creating a new ADP model:

class Widget < Spaceship::Base
  attr_accessor :id, :name, :foo_bar, :wiz_baz
  attr_mapping({
    'name' => :name,
    'fooBar' => :foo_bar,
    'wizBaz' => :wiz_baz
  })
end

When you want to instantiate a model pass in the parsed response: ‘Widget.new(widget_json)`

Direct Known Subclasses

PortalBase, Tunes::TunesBase

Defined Under Namespace

Classes: DataHash

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

The initialize method accepts a parsed response from Apple and sets all attributes that are defined by ‘attr_mapping`

Do not override ‘initialize` in your own models.



166
167
168
169
170
171
172
173
# File 'lib/spaceship/base.rb', line 166

def initialize(attrs = {})
  attrs.each do |key, val|
    self.send("#{key}=", val) if respond_to?("#{key}=")
  end
  self.raw_data = DataHash.new(attrs)
  @client = self.class.client
  self.setup
end

Class Attribute Details

.clientSpaceship::Client

The client used to make requests.

Returns:



58
59
60
# File 'lib/spaceship/base.rb', line 58

def client
  @client
end

Instance Attribute Details

#raw_dataHash/Array

Returns Holds the raw data we got from Apple’s server to use it later.

Returns:

  • (Hash/Array)

    Holds the raw data we got from Apple’s server to use it later



159
160
161
# File 'lib/spaceship/base.rb', line 159

def raw_data
  @raw_data
end

Class Method Details

.attr_mapping(attr_map = nil) ⇒ Object

Defines the attribute mapping between the response from Apple and our model objects. Keys are to match keys in the response and the values are to match attributes on the model.

Example of using ‘attr_mapping`

class Widget < Spaceship::Base
  attr_accessor :id, :name, :foo_bar, :wiz_baz
  attr_mapping({
    'name' => :name,
    'fooBar' => :foo_bar,
    'wizBaz' => :wiz_baz
  })
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spaceship/base.rb', line 107

def attr_mapping(attr_map = nil)
  if attr_map
    @attr_mapping = attr_map
    @attr_mapping.values.each do |method_name|
      getter = method_name.to_sym
      setter = "#{method_name}=".to_sym
      remove_method(getter) if public_instance_methods.include?(getter)
      remove_method(setter) if public_instance_methods.include?(setter)
    end
    include(mapping_module(@attr_mapping))
  else
    begin
      @attr_mapping ||= ancestors[1].attr_mapping
    rescue NameError, NoMethodError
    end
  end
  return @attr_mapping
end

.mapping_module(attr_mapping) ⇒ Module

Binds attributes getters and setters to underlying data returned from the API. Setting any properties will alter the ‘raw_data` hash.

Returns:

  • (Module)

    with the mapped getters and setters defined. Can be ‘include`, `extend`, or `prepend` into a class or object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spaceship/base.rb', line 75

def mapping_module(attr_mapping)
  Module.new do
    attr_mapping.each do |source, dest|
      getter = dest.to_sym
      setter = "#{dest}=".to_sym

      define_method(getter) do
        raw_data.get(*source.split('.'))
      end

      define_method(setter) do |value|
        self.raw_data ||= DataHash.new({})
        raw_data.set(source.split('.'), value)
      end
    end
  end
end

.method_missing(method_sym, *args, &block) ⇒ Object

Call a method to return a subclass constant.

If ‘method_sym` is an underscored name of a class, return the class with the current client passed into it. If the method does not match, NoMethodError is raised.

Example:

Certificate.production_push
#=> Certificate::ProductionPush

ProvisioningProfile.ad_hoc
#=> ProvisioningProfile::AdHoc

ProvisioningProfile.some_other_method
#=> NoMethodError: undefined method `some_other_method' for ProvisioningProfile


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/spaceship/base.rb', line 143

def method_missing(method_sym, *args, &block)
  module_name = method_sym.to_s
  module_name.sub!(/^[a-z\d]/) { $&.upcase }
  module_name.gsub!(/(?:_|(\/))([a-z\d])/) { $2.upcase }
  if const_defined?(module_name)
    klass = const_get(module_name)
    klass.set_client(@client)
  else
    super
  end
end

.set_client(client) ⇒ Spaceship::Base

Sets client and returns self for chaining.

Returns:



65
66
67
68
# File 'lib/spaceship/base.rb', line 65

def set_client(client)
  self.client = client
  self
end

Instance Method Details

#clientSpaceship::Client

Returns The current spaceship client used by the model to make requests.

Returns:

  • (Spaceship::Client)

    The current spaceship client used by the model to make requests.



183
184
185
# File 'lib/spaceship/base.rb', line 183

def client
  @client
end

#inspectObject



187
188
189
190
191
192
193
194
# File 'lib/spaceship/base.rb', line 187

def inspect
  inspectables = instance_variables - [:@client]
  inspect_vars = inspectables.map do |ivar|
     val = instance_variable_get(ivar)
     "#{ivar}=#{val.inspect}"
  end
  "\n#<#{self.class.name}\n\t#{inspect_vars.join("\n\t")}>"
end

#setupObject

This method can be used by subclasses to do additional initialisation using the ‘raw_data`



177
178
179
# File 'lib/spaceship/base.rb', line 177

def setup

end