Skip to content

Commit

Permalink
Merge pull request #7 from erikgrueter1/feature/client-singleton-methods
Browse files Browse the repository at this point in the history
Feature/client singleton methods
  • Loading branch information
lkatzman authored Jul 30, 2018
2 parents d876ee2 + c67400e commit 18133a7
Show file tree
Hide file tree
Showing 45 changed files with 147 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
Gemspec/OrderedDependencies:
Exclude:

Gemspec/RequiredRubyVersion:
Enabled: No

# Offense count: 1
# Cop supports --auto-correct.
Layout/CommentIndentation:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/erikgrueter1/yelp-fusion" }
git_source(:github) { 'https://github.com/erikgrueter1/yelp-fusion' }

# Specify your gem's dependencies in yelp-fusion.gemspec
gemspec
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ Please remember to read and follow the [Terms of Use](https://www.yelp.com/devel

## Installation

Add this line to your application's Gemfile:
Add this line to your Rails application's Gemfile:

```ruby
gem 'yelp-fusion', '0.2.1-beta'
```

Add this line to your Ruby appliation's Gemfile:

```ruby
gem 'yelp-fusion', require: 'yelp/fusion'
```



And then execute:

$ bundle
Expand Down Expand Up @@ -98,23 +106,23 @@ client.phone_search('+15555555555')

### [Review Details API](https://www.yelp.com/developers/documentation/v3/business_reviews)

To find all of the reviews for a business, use ``#review`` with a business ID.
To find all of the reviews for a business, use ``#review`` with a business ID.

```ruby
client.review('lJAGnYzku5zSaLnQ_T6_GQ')
```

### [Transaction Details API](https://www.yelp.com/developers/documentation/v3/transaction_search)

To find all of the reviews for a business, use ``#transaction`` with ``'deliver'`` and a business ID.
To find all of the reviews for a business, use ``#transaction`` with ``'deliver'`` and a business ID.

```ruby
client.transaction_search('delivery', {location: 'San Francisco'})
```

### [Match Details API](https://www.yelp.com/developers/documentation/v3/business_match)

To find all of the reviews for a business, use ``#match`` with a business ID.
To find all of the reviews for a business, use ``#match`` with a business ID.

```ruby
client.match({name: 'swissbakers', address1: '168 Western Ave', city: 'allston', state: 'MA', country: 'US'})
Expand Down
4 changes: 2 additions & 2 deletions lib/yelp/fusion.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,4 +33,4 @@ def self.client
@client ||= Yelp::Fusion::Client.new
end
end
end
end
8 changes: 6 additions & 2 deletions lib/yelp/fusion/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -27,19 +27,23 @@
require 'yelp/fusion/endpoint/phone'
require 'yelp/fusion/endpoint/match'
require 'yelp/fusion/endpoint/transaction'
require 'yelp/fusion/singleton'

module Yelp
module Fusion
class Client
include Fusion::Singleton
API_HOST = 'https://api.yelp.com'.freeze

attr_accessor :configuration

# Creates an instance of the fusion client
# @param options [String, nil] a consumer key
# @return [Client] a new client initialized with the keys

def initialize(option = nil)
@configuration = nil
define_request_methods
return if option.nil?
@configuration = Configuration.new(option)
end
Expand Down Expand Up @@ -83,4 +87,4 @@ def connection
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/yelp/fusion/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/business.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/match.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/phone.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/review.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/endpoint/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion lib/yelp/fusion/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -100,6 +100,7 @@ class RequestTimedOut < Base; end
class AccessLimitReached < Base; end
class NotFound < Base; end
class ClientError < Base; end
class BusinessNotFound < Base; end
end
end
end
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/business.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/match.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/business.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/categories.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/center.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/hours.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/location.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/openHours.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/region.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/reviews.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/phone.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/review.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/yelp/fusion/responses/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 40 additions & 0 deletions lib/yelp/fusion/singleton.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Yelp
module Fusion
module Singleton
REQUEST_CLASSES = [Yelp::Fusion::Endpoint::Search,
Yelp::Fusion::Endpoint::Business,
Yelp::Fusion::Endpoint::Phone,
Yelp::Fusion::Endpoint::Review,
Yelp::Fusion::Endpoint::Transaction,
Yelp::Fusion::Endpoint::Match].freeze

private

# This goes through each endpoint class and creates singleton methods
# on the client that query those classes. We do this to avoid possible
# namespace collisions in the future when adding new classes
def define_request_methods
REQUEST_CLASSES.each do |request_class|
endpoint_instance = request_class.new(self)
create_methods_from_instance(endpoint_instance)
end
end

# Loop through all of the endpoint instances' public singleton methods to
# add the method to client
def create_methods_from_instance(instance)
instance.public_methods(false).each do |method_name|
add_method(instance, method_name)
end
end

# Define the method on the client and send it to the endpoint instance
# with the args passed in
def add_method(instance, method_name)
define_singleton_method(method_name) do |*args|
instance.public_send(method_name, *args)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/yelp/fusion/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion test/base_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 2 additions & 3 deletions test/business_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -26,9 +26,8 @@ class BusinessTest < Minitest::Test
def setup
api_key = 'api_placeholder'
@client = Yelp::Fusion::Client.new(api_key)
@business = Yelp::Fusion::Endpoint::Business.new(@client)
@results = VCR.use_cassette('business') do
@business.business('lJAGnYzku5zSaLnQ_T6_GQ', {})
@client.business('lJAGnYzku5zSaLnQ_T6_GQ', {})
end
@attributes = @results.business
end
Expand Down
19 changes: 17 additions & 2 deletions test/client_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -89,4 +89,19 @@ def test_needs_api_key
@client.connection
end
end
end

def test_if_responds_to_search_method
client = Yelp::Fusion::Client.new('api_key')
assert client.respond_to?(:search)
end

def test_if_responds_to_bussines_method
client = Yelp::Fusion::Client.new('api_key')
assert client.respond_to?(:business)
end

def test_if_responds_to_phone_search_method
client = Yelp::Fusion::Client.new('api_key')
assert client.respond_to?(:phone_search)
end
end
2 changes: 1 addition & 1 deletion test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 11 additions & 1 deletion test/error_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) Jobcase, Inc. All rights reserved.
# Copyright (c) Jobcase, Inc. All rights reserved.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -74,6 +74,16 @@ def test_validation_error_failure
end
end

def test_business_not_found_failure
test_error = Struct.new(:status, :body)
result = test_error.new(400, '{"error": {"code": '\
'"BUSINESS_NOT_FOUND", "description": ' \
'"The requested business could not be found."}}')
assert_raises Yelp::Fusion::Error::BusinessNotFound do
Yelp::Fusion::Error.check_for_error(result)
end
end

def test_token_missing_failure
test_error = Struct.new(:status, :body)
result = test_error.new(400, '{"error": {"code": '\
Expand Down
Loading

0 comments on commit 18133a7

Please sign in to comment.