Module: Phishin::Api::V1

Included in:
Client::Client
Defined in:
lib/phishin/api/v1.rb

Constant Summary collapse

BASE_URL =
'http://phish.in/api/v1'
HEADERS =
{ 'accept' => 'application/json' }

Instance Method Summary collapse

Instance Method Details

#assert_response_data_field(response) ⇒ Object

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.



139
140
141
# File 'lib/phishin/api/v1.rb', line 139

def assert_response_data_field(response)
  raise Phishin::Client::EmptyResponseError.new(response.url) if !response.data
end

#check_id_arg(resource_name, val) ⇒ Object

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.



164
165
166
# File 'lib/phishin/api/v1.rb', line 164

def check_id_arg(resource_name, val)
  raise Phishin::Client::Error, "invalid argument: must pass #{resource_name} id or slug" unless val
end

#eras(opts = {}) ⇒ Object

Get eras.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



116
117
118
# File 'lib/phishin/api/v1.rb', line 116

def eras(opts={})
  perform_get_request('eras.json', opts)
end

#perform_get_request(path, opts) ⇒ Object

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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/phishin/api/v1.rb', line 144

def perform_get_request(path, opts)
  opts ||= {}
  url = [BASE_URL, path].join("/")
  force  = opts.delete(:force)
  params = opts.delete(:params) || {}
  key = [path,
            [params.to_a.sort.map { |e| e.join('=')}].join('&')]
          .join('?').chomp('?')

  json_str = ::Phishin::Client::Cache.fetch(key, force: force) do
    logger.info "phish.in api GET url=#{url} params=#{params}" if logger
    RestClient.get(url, HEADERS.merge(params: params)).to_s
  end

  resp = Phishin::Api::Response.new(url, JSON.load(json_str))
  assert_response_data_field(resp)
  return resp
end

#select_params(params, keys) ⇒ Object

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.



130
131
132
133
134
135
136
# File 'lib/phishin/api/v1.rb', line 130

def select_params(params, keys)
  _params = {}
  keys.each do |key|
    _params[key] = params[key] if params.key?(key)
  end
  return _params
end

#show(id_or_slug, opts = {}) ⇒ Object

Get show by id or slug (ie, date).

Parameters:

  • id_or_slug (Integer)

    id or slug belonging to the the resource

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



35
36
37
38
# File 'lib/phishin/api/v1.rb', line 35

def show(id_or_slug, opts={})
  check_id_arg('show', id_or_slug)
  perform_get_request(format('shows/%s.json', id_or_slug.to_s), opts)
end

#show_on_date(show_date, opts = {}) ⇒ Object

Get show by date (YYYY-MM-DD).

Parameters:

  • show_date (String)

    a show date in the format YYYY-MM-DD

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request

Raises:

  • (Phishin::Cilent::Error)

    if the date format is wrong



52
53
54
55
# File 'lib/phishin/api/v1.rb', line 52

def show_on_date(show_date, opts={})
  raise Phishin::Client::Error, "invalid argument: show_date must match YYYY-MM-DD" unless show_date && show_date =~ /\d{4}-\d{2}-\d{2}/
  perform_get_request(format('show-on-date/%s.json', show_date), opts)
end

#shows(opts = {}) ⇒ Object

Get many shows.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :params (Hash)

    parameters for HTTP GET query string

    • :page (Integer) The desired page

    • :per_page (Integer) Results per page

    • :sort_attr (String) Sorting attribute

    • :sort_dir (String) Sorting direction (ASC|DESC)

  • :force (Boolean) — default: false

    no caching for this request



43
44
45
# File 'lib/phishin/api/v1.rb', line 43

def shows(opts={})
  perform_get_request('shows.json', opts)
end

#song(id_or_slug, opts = {}) ⇒ Object

Get song by id or slug.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • id_or_slug (Integer)

    id or slug belonging to the the resource

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



63
64
65
66
# File 'lib/phishin/api/v1.rb', line 63

def song(id_or_slug, opts={})
  check_id_arg('song', id_or_slug)
  perform_get_request(format('songs/%s.json', id_or_slug.to_s), opts)
end

#songs(opts = {}) ⇒ Object

Get many songs

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :params (Hash)

    parameters for HTTP GET query string

    • :page (Integer) The desired page

    • :per_page (Integer) Results per page

    • :sort_attr (String) Sorting attribute

    • :sort_dir (String) Sorting direction (ASC|DESC)

  • :force (Boolean) — default: false

    no caching for this request



71
72
73
# File 'lib/phishin/api/v1.rb', line 71

def songs(opts={})
  perform_get_request('songs.json', opts)
end

#tour(id_or_slug, opts = {}) ⇒ Object

Get tour by id or slug.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • id_or_slug (Integer)

    id or slug belonging to the the resource

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



81
82
83
84
# File 'lib/phishin/api/v1.rb', line 81

def tour(id_or_slug, opts={})
  check_id_arg('tour', id_or_slug)
  perform_get_request(format('tours/%s.json', id_or_slug.to_s), opts)
end

#tours(opts = {}) ⇒ Object

Get many tours.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :params (Hash)

    parameters for HTTP GET query string

    • :page (Integer) The desired page

    • :per_page (Integer) Results per page

    • :sort_attr (String) Sorting attribute

    • :sort_dir (String) Sorting direction (ASC|DESC)

  • :force (Boolean) — default: false

    no caching for this request



89
90
91
# File 'lib/phishin/api/v1.rb', line 89

def tours(opts={})
  perform_get_request('tours.json', opts)
end

#track(id, opts = {}) ⇒ Object

Get track by id.

Parameters:

  • id (Integer)

    id belonging to the the resource

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



11
12
13
14
# File 'lib/phishin/api/v1.rb', line 11

def track(id, opts={})
  check_id_arg('track', id)
  perform_get_request(format('tracks/%d.json', id), opts)
end

#tracks(opts = {}) ⇒ Object

Get many tracks.

For more on available params, see phish.in/api-docs.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :params (Hash)

    parameters for HTTP GET query string

    • :page (Integer) The desired page

    • :per_page (Integer) Results per page

    • :sort_attr (String) Sorting attribute

    • :sort_dir (String) Sorting direction (ASC|DESC)

  • :force (Boolean) — default: false

    no caching for this request



26
27
28
# File 'lib/phishin/api/v1.rb', line 26

def tracks(opts={})
  perform_get_request('tracks.json', opts)
end

#venue(id_or_slug, opts = {}) ⇒ Object

Get venue by id or slug.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • id_or_slug (Integer)

    id or slug belonging to the the resource

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



99
100
101
102
# File 'lib/phishin/api/v1.rb', line 99

def venue(id_or_slug, opts={})
  check_id_arg('venue', id_or_slug)
  perform_get_request(format('venues/%s.json', id_or_slug.to_s), opts)
end

#venues(opts = {}) ⇒ Object

Get many venues.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :params (Hash)

    parameters for HTTP GET query string

    • :page (Integer) The desired page

    • :per_page (Integer) Results per page

    • :sort_attr (String) Sorting attribute

    • :sort_dir (String) Sorting direction (ASC|DESC)

  • :force (Boolean) — default: false

    no caching for this request



107
108
109
# File 'lib/phishin/api/v1.rb', line 107

def venues(opts={})
  perform_get_request('venues.json', opts)
end

#years(opts = {}) ⇒ Object

Get years.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force (Boolean) — default: false

    no caching for this request



123
124
125
# File 'lib/phishin/api/v1.rb', line 123

def years(opts={})
  perform_get_request('years.json', opts)
end