Skip to content

Commit 6ce653e

Browse files
committed
Add Search endpoint
1 parent 0a648d0 commit 6ce653e

File tree

14 files changed

+263
-3
lines changed

14 files changed

+263
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Your contribution here.
44
* [#57](https://github.com/dblock/iex-ruby-client/pull/57): Update properties for chart api endpoint - [@brunjo](https://github.com/brunjo).
5+
* [#58](https://github.com/dblock/iex-ruby-client/pull/58): Add search endpoint - [@pmn4](https://github.com/pmn4).
56

67
### 1.1.0 (2019/07/08)
78

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ A Ruby client for the [The IEX Cloud API](https://iexcloud.io/docs/api/).
2626
- [Get Sector Performance](#get-sector-performance)
2727
- [Get Largest Trades](#get-largest-trades)
2828
- [Get a Quote for Crypto Currencies](#get-a-quote-for-crypto-currencies)
29+
- [Search](#search)
2930
- [Configuration](#configuration)
3031
- [Errors](#errors)
3132
- [SymbolNotFound](#symbolnotfound)
@@ -369,6 +370,21 @@ crypto.high_dollar #'$3,590'
369370

370371
See [#crypto](https://iexcloud.io/docs/api/#crypto) for detailed documentation or [crypto.rb](lib/iex/resources/crypto.rb) for returned fields.
371372

373+
### Search
374+
375+
Searches for companies*
376+
377+
``` Ruby
378+
results = client.search('msft')
379+
380+
results.first.company_name # 'Microsoft'
381+
```
382+
383+
See [#search](https://iexcloud.io/docs/api/#search) for detailed documentation.
384+
385+
* - IEX documentation suggests this endpoint will one day return more than just
386+
Companies.
387+
372388
## Configuration
373389

374390
You can configure client options globally or directly with a `IEX::Api::Client` instance.

lib/iex/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require_relative 'endpoints/ohlc'
1111
require_relative 'endpoints/price'
1212
require_relative 'endpoints/quote'
13+
require_relative 'endpoints/search'
1314
require_relative 'endpoints/sectors'
1415
require_relative 'endpoints/crypto'
1516

lib/iex/api/client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Client
1414
include Endpoints::Ohlc
1515
include Endpoints::Price
1616
include Endpoints::Quote
17+
include Endpoints::Search
1718
include Endpoints::Sectors
1819

1920
include Cloud::Connection

lib/iex/endpoints/search.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module IEX
2+
module Endpoints
3+
module Search
4+
def search(fragment, options = {})
5+
raise ArgumentError, 'Fragment is required' if fragment.blank?
6+
7+
get([
8+
'search',
9+
fragment
10+
].compact.join('/'), options).map do |data|
11+
# Per the IEX documentation, any type of Resource could be returned
12+
# this should become a case statement:
13+
# https://iexcloud.io/docs/api/#search
14+
IEX::Resources::Company.new(data)
15+
end
16+
rescue Faraday::ResourceNotFound => e
17+
raise IEX::Errors::SearchNotFoundError.new(fragment, e.response[:body])
18+
end
19+
end
20+
end
21+
end

lib/iex/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require_relative 'errors/search_not_found_error'
12
require_relative 'errors/symbol_not_found_error'
23
require_relative 'errors/client_error'
34
require_relative 'errors/permission_denied_error'

lib/iex/errors/client_error.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
require_relative 'error'
2+
13
module IEX
24
module Errors
3-
class ClientError < StandardError
5+
class ClientError < Error
46
attr_reader :response
57

68
def initialize(response)

lib/iex/errors/error.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module IEX
2+
module Errors
3+
class Error < StandardError; end
4+
end
5+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative 'error'
2+
3+
module IEX
4+
module Errors
5+
class SearchNotFoundError < Error
6+
attr_reader :response
7+
attr_reader :fragment
8+
9+
def initialize(fragment, response)
10+
@response = response
11+
@fragment = fragment
12+
super %(Fragment "#{fragment}" Not Found)
13+
end
14+
end
15+
end
16+
end

lib/iex/errors/symbol_not_found_error.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
require_relative 'error'
2+
13
module IEX
24
module Errors
3-
class SymbolNotFoundError < StandardError
4-
attr_reader :symbol
5+
class SymbolNotFoundError < Error
56
attr_reader :response
7+
attr_reader :symbol
68

79
def initialize(symbol, response)
810
@response = response

0 commit comments

Comments
 (0)