Class: ShopifyAPI::Mock::Fixture

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

Overview

provides easy access to fixtures

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/fixtures.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



68
69
70
71
72
73
# File 'lib/shopify-mock/fixtures.rb', line 68

def all
  Dir[File.join(ShopifyAPI::Mock::Fixture.path, "*")].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:



84
85
86
87
88
89
# File 'lib/shopify-mock/fixtures.rb', line 84

def find(name, ext = :json)
  fixture_name = "#{name.to_s}.#{ext.to_s}"
  file_name = File.join(self.path, 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



96
97
98
# File 'lib/shopify-mock/fixtures.rb', line 96

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



106
107
108
109
110
# File 'lib/shopify-mock/fixtures.rb', line 106

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("./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/fixtures.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("./orders.xml")
data = fixture.data # => the contents of "./orders.xml"
fixture.data = "hello world"
data = fixture.data # => "hello world"

Parameters:

  • value (String)

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

Returns:

  • The new contents of the fixture



47
48
49
50
# File 'lib/shopify-mock/fixtures.rb', line 47

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("./orders.xml")
ext = fixture.ext # => :xml

Returns:

  • (Symbol)

    The extension



58
59
60
# File 'lib/shopify-mock/fixtures.rb', line 58

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("./orders.xml")
name = fixture.name # => :orders

Returns:

  • (Symbol)

    the the file name without the extension



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

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