Class: ShopifyAPI::Mock::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify-mock/fixture.rb

Overview

provides easy access to fixtures

Constant Summary collapse

@@path =
nil
@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Fixture

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

creates a new instance of ShopifyAPI::Mock::Fixture

Parameters:

  • file_name (String)

    The location of the file to load into the fixture

Raises:

  • (IOError)

    Raised when file_name is invalid



13
14
15
16
# File 'lib/shopify-mock/fixture.rb', line 13

def initialize(file_name)
  raise IOError, "File not found: #{file_name}" unless File.exists? file_name
  @file_name = file_name
end

Class Method Details

.allArray, Fixture

finds all the fixtures

Examples:

Find all the fixtures

ShopifyAPI::Mock::Fixture.all

Returns:

  • (Array, Fixture)

    an array of all the Fixtures



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shopify-mock/fixture.rb', line 70

def all
  # get a list of all the fixture files
  files = []
  Dir[File.join(ShopifyAPI::Mock::Fixture.path, "**", "*")].each do |f|
    files << f unless File.directory? f
  end
  
  # map files to fixtures
  files.map do |file_name|
    fixture_name = File.basename(file_name)
    @@cache[fixture_name] ||= Fixture.new(file_name)
  end
end

.find(name, ext = :json) ⇒ ShopifyAPI::Mock::Fixture

finds a fixture by name

Examples:

Find the orders json fixture

fixture = ShopifyAPI::Mock::Fixture.find :orders

Find the orders xml fixture

fixture = ShopifyAPI::Mock::Fixture.find :orders, :xml

Parameters:

  • name (Symbol)

    The name of the fixture

  • ext (Symbol) (defaults to: :json)

    The extension of the symbol - defaults to :json

Returns:



93
94
95
96
97
98
# File 'lib/shopify-mock/fixture.rb', line 93

def find(name, ext = :json)
  fixture_name = "#{name.to_s}.#{ext.to_s}"
  file_name = File.join(self.path, ext.to_s, fixture_name)
  return nil unless File.exists? file_name
  @@cache[fixture_name] ||= Fixture.new(file_name)
end

.pathString

gets the current path to the fixtures

Examples:

Get the fixtures path

fixture_path = ShopifyAPI::Mock::Fixture.path

Returns:

  • (String)

    The fixtures path



105
106
107
# File 'lib/shopify-mock/fixture.rb', line 105

def path
  @@path ||= File.expand_path("../fixtures/", __FILE__)
end

.path=(value) ⇒ String

sets the current fixtures path

Examples:

Override the default fixtures path

ShopifyAPI::Mock::Fixture.path = File.join(Rails.root, "spec", "fixtures", "shopify")

Parameters:

  • value (String)

    The new fixtures path

Returns:

  • (String)

    The new fixtures path



115
116
117
118
119
# File 'lib/shopify-mock/fixture.rb', line 115

def path=(value)
  return @@path if @@path == value
  @@path = value
  @@cache = {}
end

Instance Method Details

#dataString

gets the content of the fixture

Examples:

Create a fixture and read its contents

fixture = ShopifyAPI::Fixture.new "./orders.xml"
data = fixture.data # => the contents of "./orders.xml"

Returns:

  • (String)

    the contents of the file, or @data if it was overwritten



24
25
26
# File 'lib/shopify-mock/fixture.rb', line 24

def data
  @data || File.read(@file_name)
end

#data=(value) ⇒ Object

overrides the contents of the fixture

Examples:

Override a the contents of a fixture

fixture = ShopifyAPI::Fixture.new "./orders.xml"
data = fixture.data # => the contents of "./orders.xml"
fixture.data = "hello world"
data = fixture.data # => "hello world"

Set contents of a fixture back to their default

fixture.data = nil

Parameters:

  • value (String)

    The new value to use, or set to nil to reset to default

Returns:

  • The new contents of the fixture



49
50
51
52
# File 'lib/shopify-mock/fixture.rb', line 49

def data=(value)
  @data = value
  data
end

#extSymbol

gets the extension of the fixture

Examples:

Create a new fixture and get its extension

fixture = ShopifyAPI::Fixture.new "./orders.xml"
ext = fixture.ext # => :xml

Returns:

  • (Symbol)

    The extension



60
61
62
# File 'lib/shopify-mock/fixture.rb', line 60

def ext
  File.extname(@file_name).gsub(".","").to_sym
end

#nameSymbol

gets the name of the fixture

Examples:

Load a fixture and get its name

fixture = ShopifyAPI::Fixture.new "./orders.xml"
name = fixture.name # => :orders

Returns:

  • (Symbol)

    the the file name without the extension



34
35
36
# File 'lib/shopify-mock/fixture.rb', line 34

def name
  File.basename(@file_name, ".#{self.ext.to_s}").to_sym
end