Module: ShopifyAPI::Mock

Defined in:
lib/shopify-mock.rb,
lib/shopify-mock/errors.rb,
lib/shopify-mock/fixtures.rb,
lib/shopify-mock/response.rb

Overview

the main module for managing the Shopify mocks

Defined Under Namespace

Classes: DisabledError, Fixture, Response

Class Method Summary collapse

Class Method Details

.allow_internet=(state = true) ⇒ Boolean

enables or disables access to the real Internet

Examples:

Turn off access to the Internet altogether

Shopify::Mock.allow_internet = false

Returns:

  • (Boolean)

    status of real Internet access



65
66
67
68
69
# File 'lib/shopify-mock.rb', line 65

def allow_internet=(state = true)
  return @allow_internet if @allow_internet == state
  @allow_internet = state
  FakeWeb.allow_net_connect = @allow_internet
end

.allow_internet?Boolean

gets the state of access to the real Internet

Examples:

Check if access has been disabled

can_access_internet = Shopify::Mock.allow_internet

Returns:

  • (Boolean)

    false if all access to the Internet has been disabled



56
57
58
# File 'lib/shopify-mock.rb', line 56

def allow_internet?
  @allow_internet || true
end

.enabled=(value = false) ⇒ Boolean

enable or disable ShopifyAPI::Mock

Examples:

Enabling ShopifyAPI::Mock

ShopifyAPI::Mock.enabled = true

Parameters:

  • value (Boolean) (defaults to: false)

    true to enable ShopifyAPI::Mock, false to disable

Returns:

  • (Boolean)

    true if enabled



28
29
30
31
32
33
34
35
36
37
# File 'lib/shopify-mock.rb', line 28

def enabled=(value=false)
  return @enabled if value == @enabled
  if value
    #load File.expand_path("../shopify-mock/responses.rb", __FILE__)
    ShopifyAPI::Mock.register_fixed_responses
  else
    ShopifyAPI::Mock::Response.clear
  end
  @enabled = value
end

.enabled?Boolean

get the enabled state of ShopifyAPI::Mock

Examples:

Check if ShopifyAPI::Mock is enabled

ShopifyAPI::Mock.enabled # => true or false

Returns:

  • (Boolean)

    true if enabled



18
19
20
# File 'lib/shopify-mock.rb', line 18

def enabled?
  @enabled || false
end

.register_fixed_responsesArray

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.

registers all the fixed responses

Returns:

  • (Array)

    the responses that were registered



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/shopify-mock.rb', line 74

def register_fixed_responses
  ShopifyAPI::Mock::Response.clear
  
  registered_responses = []
  ShopifyAPI::Mock::Fixture.all.each do |fixture|
    # register the count fixture for this resource
    count_fixture = ShopifyAPI::Mock::Fixture.find(:count, fixture.ext.to_sym)
    registered_responses << ShopifyAPI::Mock::Response.new(
      :get, "#{fixture.name.to_s}/count.#{fixture.ext}",
      count_fixture.data
    )
    # register the resource fixture
    registered_responses << ShopifyAPI::Mock::Response.new(
      :get, "#{fixture.name.to_s}.#{fixture.ext.to_s}",
      fixture.data
    )
  end
  registered_responses
end

.resetBoolean

resets the ShopifyAPI::Mocks back to their original state

Examples:

Reset the mocks

ShopifyAPI::Mock.reset

Returns:

  • (Boolean)

    true when successful

Raises:



45
46
47
48
49
# File 'lib/shopify-mock.rb', line 45

def reset
  raise ShopifyAPI::Mock::DisabledError, "cannot reset ShopifyAPI::Mock while it is disabled" \
    unless ShopifyAPI::Mock.enabled?
  self.class.register_fixed_responses
end