phishin-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ee4488e118bd6ff4bbc4845ee972089ff2c4a44
4
+ data.tar.gz: 42a82c1d9691889b32fe366d698bd952ac0336ea
5
+ SHA512:
6
+ metadata.gz: 45a63268185f2a1a7c4ad3c0afee7fa04d16266c5f02a7502942195acfcb0d042e4567a4172fd4b4ca5fde42a3f60e7d366dc9988a8b10063034279e23999e65
7
+ data.tar.gz: 2bf0d5f634419099acc1c80f70817585c7151a29147fc1087d6048d376de2ed735bcba8eb90c6b157af32b2c89c6a52d6db878691ed42db7e97a9012abfd4501
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *.swp
24
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phishin-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Bird
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Phish.in API Client
2
+
3
+ Talks to the http://phish.in API. Has built-in caching.
4
+
5
+ - [README](https://github.com/alexebird/phishin-client/blob/master/README.md)
6
+ - [Documentation](http://rubydoc.info/gems/phishin-client)
7
+ - [Phish.in API documentation](http://phish.in/api-docs)
8
+
9
+ ## Installation
10
+
11
+ gem install phishin-client
12
+
13
+
14
+ ## Getting Started
15
+
16
+ ```ruby
17
+ require 'phishin-client'
18
+ doglogger = Logger.new
19
+ c = Phishin::Client.new log: true, logger: doglogger
20
+
21
+ response = c.get_tracks(page: 1, per_page: 40) # Phishin::Api::Response instance
22
+ json_hash = response.data
23
+ ```
24
+
25
+ ## License
26
+
27
+ Please see [LICENSE](https://github.com/alexebird/phishin-client/blob/master/LICENSE.txt).
28
+
29
+
30
+ ## Author
31
+
32
+ Alex Bird [@alexebird](https://twitter.com/alexebird).
33
+ Big thanks to [@phish_in](https://twitter.com/phish_in).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,35 @@
1
+ module Phishin
2
+ module Api
3
+ class Response
4
+ attr_reader :http_status
5
+ attr_reader :total_entries
6
+ attr_reader :total_pages
7
+ attr_reader :page
8
+ attr_reader :data
9
+ attr_reader :url
10
+
11
+ def initialize(url, response_data)
12
+ @url = url
13
+ @success = response_data['success']
14
+
15
+ if success?
16
+ @total_entries = response_data['total_entries']
17
+ @total_pages = response_data['total_pages']
18
+ @page = response_data['page']
19
+ @data = response_data['data']
20
+ else
21
+ @message = response_data['message']
22
+ raise Phishin::Client::UnsuccessfulResponseError.new(url, @message)
23
+ end
24
+ end
25
+
26
+ def [](key)
27
+ return @data[key]
28
+ end
29
+
30
+ def success?
31
+ return @success == true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,121 @@
1
+ module Phishin
2
+ module Api
3
+ module V1
4
+ BASE_URL = 'http://phish.in/api/v1'
5
+ HEADERS = { 'accept' => 'application/json' }
6
+ DEFAULT_PARAMS = %i[page per_page sort_attr sort_dir]
7
+
8
+ # @param id_or_slug [String,Integer] track id or slug
9
+ def get_track(id_or_slug)
10
+ check_id_arg('track', id_or_slug)
11
+ perform_get_request('tracks/%s.json' % id_or_slug.to_s)
12
+ end
13
+
14
+ # @option opts [Integer] :page The desired page
15
+ # @option opts [Integer] :per_page Results per page
16
+ def get_tracks(opts={})
17
+ perform_get_request('tracks.json', select_params(opts, DEFAULT_PARAMS))
18
+ end
19
+
20
+
21
+
22
+ # @param (see #get_track)
23
+ def get_show(id_or_slug)
24
+ check_id_arg('show', id_or_slug)
25
+ perform_get_request('shows/%s.json' % id_or_slug.to_s)
26
+ end
27
+
28
+ # @option (see #get_tracks)
29
+ def get_shows(opts={})
30
+ perform_get_request('shows.json', select_params(opts, DEFAULT_PARAMS))
31
+ end
32
+
33
+ # @param show_date [String] a show date in the format YYYY-MM-DD
34
+ def get_show_on_date(show_date)
35
+ raise Phishin::Client::Error, "invalid argument: show_date must match YYYY-MM-DD" unless show_date && show_date =~ /\d{4}-\d{2}-\d{2}/
36
+ perform_get_request('show-on-date/%s.json' % show_date)
37
+ end
38
+
39
+
40
+
41
+ # @param (see #get_track)
42
+ def get_song(id_or_slug)
43
+ check_id_arg('song', id_or_slug)
44
+ perform_get_request('songs/%s.json' % id_or_slug.to_s)
45
+ end
46
+
47
+ # @option (see #get_tracks)
48
+ def get_songs(opts={})
49
+ perform_get_request('songs.json', select_params(opts, DEFAULT_PARAMS))
50
+ end
51
+
52
+
53
+
54
+ # @param (see #get_track)
55
+ def get_tour(id_or_slug)
56
+ check_id_arg('tour', id_or_slug)
57
+ perform_get_request('tours/%s.json' % id_or_slug.to_s)
58
+ end
59
+
60
+ # @option (see #get_tracks)
61
+ def get_tours(opts={})
62
+ perform_get_request('tours.json', select_params(opts, DEFAULT_PARAMS))
63
+ end
64
+
65
+
66
+
67
+ # @param (see #get_track)
68
+ def get_venue(id_or_slug)
69
+ check_id_arg('venue', id_or_slug)
70
+ perform_get_request('venues/%s.json' % id_or_slug.to_s)
71
+ end
72
+
73
+ # @option (see #get_tracks)
74
+ def get_venues(opts={})
75
+ perform_get_request('venues.json', select_params(opts, DEFAULT_PARAMS))
76
+ end
77
+
78
+
79
+
80
+ def get_eras
81
+ perform_get_request('eras.json')
82
+ end
83
+
84
+ def get_years
85
+ perform_get_request('years.json')
86
+ end
87
+
88
+
89
+
90
+ # @api private
91
+ def select_params(opts, keys)
92
+ params = {}
93
+ keys.each do |key|
94
+ params[key] = opts[key] if opts.key?(key)
95
+ end
96
+ return params
97
+ end
98
+
99
+ # @api private
100
+ def assert_response_data_field(response)
101
+ raise Phishin::Client::EmptyResponseError.new(response.url) if !response.data
102
+ end
103
+
104
+ # @api private
105
+ def perform_get_request(path, params={})
106
+ url = [BASE_URL, path].join("/")
107
+ logger.info "phish.in api GET url=#{url} params=#{params}" if logger
108
+ response = RestClient.get(url, HEADERS.merge(params: params))
109
+ resp = Phishin::Api::Response.new(url, Oj.load(response.to_s))
110
+ assert_response_data_field(resp)
111
+ return resp
112
+ end
113
+
114
+ # @api private
115
+ def check_id_arg(resource_name, val)
116
+ raise Phishin::Client::Error, "invalid argument: must pass #{resource_name} id or slug" unless val
117
+ end
118
+ end
119
+ end
120
+ end
121
+
@@ -0,0 +1,4 @@
1
+ module Phishin
2
+ module Api
3
+ end
4
+ end
@@ -0,0 +1,25 @@
1
+ module Phishin
2
+ module Client
3
+ class Client
4
+
5
+ include Phishin::Api::V1
6
+
7
+ attr_accessor :logger
8
+
9
+ DEFAULT_LOGGER = lambda do
10
+ require 'logger'
11
+ logger = Logger.new(STDOUT)
12
+ logger.progname = 'phishin-client'
13
+ logger
14
+ end
15
+
16
+ # @option opts [Boolean] :log Enable/disable logging
17
+ # @option opts [Object] :logger Logger-compatible object instance to use
18
+ def initialize(opts={})
19
+ opts[:log] = true if !opts.key?(:log)
20
+ @logger = opts[:log] ? (opts[:logger] || DEFAULT_LOGGER.call()) : nil
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,31 @@
1
+ module Phishin
2
+ module Client
3
+ class Error < StandardError ; end
4
+
5
+ class TrackNotFoundError < StandardError
6
+ def initialize(show_date, track_id_or_slug)
7
+ super("phish.in api could not find track with show_date=#{show_date} track_id_or_slug=#{track_id_or_slug} (possibly the 'missing track' issue)")
8
+ end
9
+ end
10
+
11
+ # Raised when there is no response body
12
+ class EmptyResponseError < StandardError
13
+ attr_reader :url
14
+
15
+ def initialize(url)
16
+ @url = url
17
+ super("phish.in api response body was empty for url=#{url}")
18
+ end
19
+ end
20
+
21
+ class UnsuccessfulResponseError < StandardError
22
+ attr_reader :url
23
+
24
+ def initialize(url, message)
25
+ @url = url
26
+ super("phish.in api response body has success=false. message='#{message}'")
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,6 @@
1
+ module Phishin
2
+ module Client
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
6
+
@@ -0,0 +1,23 @@
1
+ require 'rest-client'
2
+ require 'oj'
3
+
4
+ require 'phishin/api'
5
+ require 'phishin/api/v1'
6
+ require 'phishin/api/response'
7
+
8
+ require 'phishin/client/version'
9
+ require 'phishin/client/client'
10
+ require 'phishin/client/errors'
11
+
12
+ module Phishin
13
+ module Client
14
+
15
+ # Wrapper for {::Phishin::Client::Client#initialize}.
16
+ #
17
+ def new(opts={})
18
+ Phishin::Client::Client.new(opts)
19
+ end
20
+ extend self
21
+ end
22
+ end
23
+
@@ -0,0 +1 @@
1
+ require 'phishin/client'
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+ require 'date'
5
+ require 'phishin/client/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = 'phishin-client'
9
+ gem.version = Phishin::Client::VERSION
10
+ gem.date = Date.today.to_s
11
+ gem.licenses = ['MIT']
12
+ gem.authors = ['Alexander Bird']
13
+ gem.email = ['alexebird@gmail.com']
14
+ gem.homepage = 'https://github.com/alexebird/phishin-client'
15
+ gem.summary = %q{Phish.in API client}
16
+ gem.description = %q{Client for http://phish.in Phish streaming API.}
17
+
18
+ gem.required_ruby_version = '>= 1.9.3'
19
+
20
+ gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
21
+ gem.add_runtime_dependency 'oj', '~> 2.1.7'
22
+ #gem.add_runtime_dependency 'nokogiri', '~> 1.6.0'
23
+ gem.add_development_dependency 'rake', '~> 10.0'
24
+ gem.add_development_dependency 'rspec', '~> 2.14'
25
+ gem.add_development_dependency 'yard', '~> 0.8'
26
+ #gem.add_development_dependency 'redcarpet', '~> 3.0'
27
+ #gem.add_development_dependency 'simplecov', '~> 0.7.1'
28
+
29
+ gem.files = `git ls-files`.split($/)
30
+ gem.test_files = gem.files.grep(%r{^spec/})
31
+
32
+ gem.require_paths = ['lib']
33
+ end
34
+
35
+ # coding: utf-8
36
+ lib = File.expand_path('../lib', __FILE__)
37
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
38
+ require 'date'
39
+ require 'phishin/client/version'
40
+
41
+ Gem::Specification.new do |spec|
42
+ spec.name = "phishin-client"
43
+ spec.version = Phishin::Client::VERSION
44
+ spec.date = Date.today.to_s
45
+ spec.authors = ["Alex Bird"]
46
+ spec.email = ["alexebird@gmail.com"]
47
+ spec.summary = %q{Phish.in API client}
48
+ spec.description = %q{Client for http://phish.in Phish streaming API.}
49
+ spec.homepage = "https://github.com/alexebird/phishin-client"
50
+ spec.license = "MIT"
51
+
52
+ spec.files = `git ls-files -z`.split("\x0")
53
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
54
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
55
+ spec.require_paths = ["lib"]
56
+
57
+ spec.add_runtime_dependency "rest-client", "~> 1.6.7"
58
+ spec.add_runtime_dependency "oj", "~> 2.1.7"
59
+ spec.add_development_dependency "bundler", "~> 1.6"
60
+ spec.add_development_dependency "rake"
61
+ spec.add_development_dependency "yard", "~> 0.8"
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phishin-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Bird
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ description: Client for http://phish.in Phish streaming API.
84
+ email:
85
+ - alexebird@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/phishin-client.rb
96
+ - lib/phishin/api.rb
97
+ - lib/phishin/api/response.rb
98
+ - lib/phishin/api/v1.rb
99
+ - lib/phishin/client.rb
100
+ - lib/phishin/client/client.rb
101
+ - lib/phishin/client/errors.rb
102
+ - lib/phishin/client/version.rb
103
+ - phishin-client.gemspec
104
+ homepage: https://github.com/alexebird/phishin-client
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.14
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Phish.in API client
128
+ test_files: []
129
+ has_rdoc: