Class: Yelp::Client

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

Constant Summary collapse

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

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


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

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


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

def configuration
  @configuration
end

Instance Method Details

#check_api_keysObject

Checks that all the keys needed were given


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

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


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

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

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

#connectionObject

API connection


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

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