Class: Yelp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yelp/client.rb

Constant Summary collapse

API_HOST =
'https://api.yelp.com'
REQUEST_CLASSES =
[ Yelp::Endpoint::Search,
Yelp::Endpoint::Business,
Yelp::Endpoint::PhoneSearch]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Creates an instance of the Yelp client

Parameters:

  • options (Hash, nil) (defaults to: nil)

    a hash of the consumer key, consumer secret, token, and token secret



23
24
25
26
27
28
29
30
31
# File 'lib/yelp/client.rb', line 23

def initialize(options = nil)
  @configuration = nil
  define_request_methods

  unless options.nil?
    @configuration = Configuration.new(options)
    check_api_keys
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'lib/yelp/client.rb', line 17

def configuration
  @configuration
end

Instance Method Details

#check_api_keysObject

Checks that all the keys needed were given



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yelp/client.rb', line 52

def check_api_keys
  if configuration.nil? || !configuration.valid?
    @configuration = nil
    raise Error::MissingAPIKeys
  else
    # Freeze the configuration so it cannot be modified once the gem is
    # configured.  This prevents the configuration changing while the gem
    # is operating, which would necessitate invalidating various caches.
    @configuration.freeze
  end
end

#configure {|Configuration| ... } ⇒ Object

Configure the API client

Examples:

Simple configuration

Yelp.client.configure do |config|
  config.consumer_key = 'abc'
  config.consumer_secret = 'def'
  config.token = 'ghi'
  config.token_secret = 'jkl'
end

Yields:

Raises:

  • (MissingAPIKeys)

    if the configuration is invalid



43
44
45
46
47
48
49
# File 'lib/yelp/client.rb', line 43

def configure
  raise Error::AlreadyConfigured unless @configuration.nil?

  @configuration = Configuration.new
  yield(@configuration)
  check_api_keys
end

#connectionObject

API connection



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/yelp/client.rb', line 65

def connection
  return @connection if instance_variable_defined?(:@connection)

  check_api_keys
  @connection = Faraday.new(API_HOST) do |conn|
    # Use the Faraday OAuth middleware for OAuth 1.0 requests
    conn.request :oauth, @configuration.auth_keys

    # Using default http library, had to specify to get working
    conn.adapter :net_http
  end
end