dirble 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +14 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +94 -0
  10. data/Rakefile +7 -0
  11. data/dirble.gemspec +33 -0
  12. data/lib/dirble.rb +16 -0
  13. data/lib/dirble/category.rb +48 -0
  14. data/lib/dirble/configurable.rb +20 -0
  15. data/lib/dirble/connection.rb +25 -0
  16. data/lib/dirble/errors.rb +5 -0
  17. data/lib/dirble/primary_category.rb +20 -0
  18. data/lib/dirble/query_executer.rb +34 -0
  19. data/lib/dirble/simple_api_model.rb +19 -0
  20. data/lib/dirble/song.rb +8 -0
  21. data/lib/dirble/station.rb +72 -0
  22. data/lib/dirble/version.rb +3 -0
  23. data/spec/category_spec.rb +44 -0
  24. data/spec/connection_spec.rb +26 -0
  25. data/spec/dirble_spec.rb +16 -0
  26. data/spec/primary_category_spec.rb +21 -0
  27. data/spec/spec_helper.rb +26 -0
  28. data/spec/station_spec.rb +48 -0
  29. data/spec/support/fake_dirble_api.rb +70 -0
  30. data/spec/support/fixtures/add_station.json +1 -0
  31. data/spec/support/fixtures/categories.json +1 -0
  32. data/spec/support/fixtures/child_categories.json +1 -0
  33. data/spec/support/fixtures/primary_categories.json +1 -0
  34. data/spec/support/fixtures/search_stations_general.json +1 -0
  35. data/spec/support/fixtures/station.json +1 -0
  36. data/spec/support/fixtures/stations_continent_asia.json +1 -0
  37. data/spec/support/fixtures/stations_count.json +1 -0
  38. data/spec/support/fixtures/stations_for_country_usa.json +1 -0
  39. data/spec/support/fixtures/stations_in_category.json +1 -0
  40. metadata +267 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0df8ae2244e7353f53e1dbf4a22458fc928a2eed
4
+ data.tar.gz: e39e36b0dba25becdc8734376011e7dc1791f5dd
5
+ SHA512:
6
+ metadata.gz: fc4d75608532f9b29290a6d9b8717cdacfb078e3c02ecfe23d671e232f863813e10b18675a66df560d4183fd2a9b717a87241e704b7418d106dbaace13080eb0
7
+ data.tar.gz: 76643a5a4a09deb653fdaacded4ee8f611e7d7a04e83a36c490d2a247238cd6495e61833e3c61e59196e02c7fbc331bea6e65cfae021c60a16dc18cf013918fe
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1 @@
1
+ dirble
@@ -0,0 +1 @@
1
+ ruby-2.1.5
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.1.5"
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx
7
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dirble.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Przemek Mroczek
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.
@@ -0,0 +1,94 @@
1
+ [![Code Climate](https://codeclimate.com/github/Lackoftactics/dirble/badges/gpa.svg)](https://codeclimate.com/github/Lackoftactics/dirble)
2
+ [![Build Status](https://travis-ci.org/Lackoftactics/dirble.svg?branch=master)](https://travis-ci.org/Lackoftactics/dirble)
3
+ [![Coverage Status](https://coveralls.io/repos/Lackoftactics/dirble/badge.png?branch=master)](https://coveralls.io/r/Lackoftactics/dirble?branch=master)
4
+ # Dirble
5
+
6
+ Gem to make interacting with http://api.dirble.com/ amazingly easy.
7
+ Using familiar Rails syntax. It uses Faraday and Typhoeus as adapter.
8
+
9
+ ## Compatibility
10
+
11
+ * Ruby 1.9.3 and 2
12
+ * JRuby 1.7
13
+ * Rubinius
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'dirble'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install dirble
30
+
31
+ ## Usage
32
+
33
+ In Rails: Put this quick configuration block in environment file.
34
+ Beyond Rails: Put this in initialization or config file.
35
+ ```ruby
36
+ Dirble.configure do |config|
37
+ config.api_key = "valid_api_key_from_dirble"
38
+ end
39
+ ```
40
+ Now you are ready to go.
41
+ ## Category
42
+ * Fetch all categories with
43
+
44
+ `Dirble::Category.all`
45
+
46
+ * Fetch primary categories with
47
+
48
+ `Dirble::Category.primary` or
49
+ `Dirble::PrimaryCategory.all`
50
+
51
+ * Find category by id
52
+
53
+ `Dirble::Category.find(2)`
54
+
55
+ * Get first category
56
+
57
+ `Dirble::Category.first`
58
+
59
+ * Find children of primary category
60
+
61
+ ```ruby
62
+ primary_category = Dirble::PrimaryCategory.first
63
+ children = primary_category.children
64
+ ```
65
+
66
+ * Find stations of category
67
+
68
+ `category.stations`
69
+
70
+ ## Station
71
+
72
+ * Search stations
73
+
74
+ `Dirble::Station.search('pop')`
75
+
76
+ * Stations by continent
77
+
78
+ `Dirble::Station.by_continent('Asia')`
79
+
80
+ * Stations by country
81
+
82
+ `Dirble::Station.by_country('us')`
83
+
84
+ * Stations amount. Check currently registered stations in Dirble.
85
+
86
+ `Dirble::Station.count`
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/[my-github-username]/dirble/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dirble/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dirble"
8
+ spec.version = Dirble::VERSION
9
+ spec.authors = ["Przemek Mroczek"]
10
+ spec.email = ["przemyslawmroczek@o2.pl"]
11
+ spec.summary = %q{Gem for easier interacting with dirble api.}
12
+ spec.description = %q{Api wrapper for easier using dirble api.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_development_dependency "pry-rescue"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "sinatra"
28
+ spec.add_development_dependency "sinatra-contrib"
29
+ spec.add_development_dependency "activesupport"
30
+ spec.add_development_dependency "faraday"
31
+ spec.add_development_dependency "typhoeus"
32
+ spec.add_development_dependency "coveralls"
33
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_support/all'
2
+ require 'dirble/simple_api_model'
3
+ require 'dirble/errors'
4
+ require 'dirble/configurable'
5
+ require 'dirble/connection'
6
+ require 'dirble/category'
7
+ require 'dirble/primary_category'
8
+ require 'dirble/station'
9
+
10
+ require 'dirble/version'
11
+
12
+ module Dirble
13
+ class << self
14
+ include Dirble::Configurable
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ module Dirble
2
+ class Category
3
+ extend Dirble::SimpleApiModel
4
+
5
+ attr_accessor :id, :name, :description
6
+
7
+ def initialize(options)
8
+ self.id = options[:id]
9
+ self.name = options[:name]
10
+ self.description = options[:description]
11
+ end
12
+
13
+ def self.all
14
+ call_api_with_results(
15
+ request_type: :get,
16
+ query: 'categories/apikey/{{api_key}}'
17
+ )
18
+ end
19
+
20
+ def self.find(category_id)
21
+ all.select { |category| category.id == category_id }.first
22
+ end
23
+
24
+ def self.first
25
+ all.first
26
+ end
27
+
28
+ def self.primary
29
+ call_api_with_results(
30
+ request_type: :get,
31
+ query: 'primaryCategories/apikey/{{api_key}}',
32
+ factory_klass: Dirble::PrimaryCategory
33
+ )
34
+ end
35
+
36
+ def stations
37
+ self.class.call_api_with_results(
38
+ request_type: :get,
39
+ query: "stations/apikey/{{api_key}}/id/#{id}",
40
+ factory_klass: Dirble::Station
41
+ )
42
+ end
43
+
44
+ def primary
45
+ false
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,20 @@
1
+ module Dirble
2
+ module Configurable
3
+ attr_accessor :api_key, :connection
4
+
5
+ def configure
6
+ yield self
7
+ prepare_connection
8
+ self
9
+ end
10
+
11
+ def prepare_connection
12
+ raise ArgumentError, 'You have to set api key in configure' unless self.api_key
13
+ self.connection = Dirble::Connection.new
14
+ end
15
+
16
+ def reset!
17
+ self.api_key = nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'query_executer'
2
+ require 'typhoeus'
3
+ require 'typhoeus/adapters/faraday'
4
+
5
+ module Dirble
6
+ class Connection
7
+ DIRBLE_API_URL = 'http://api.dirble.com/v1'
8
+
9
+ def exec_query(query_params)
10
+ QueryExecuter.new(connection, query_params).execute
11
+ end
12
+
13
+ private
14
+
15
+ def connection
16
+ @connection ||= begin
17
+ Faraday.new(url: DIRBLE_API_URL) do |faraday|
18
+ faraday.request :url_encoded
19
+ faraday.response :logger
20
+ faraday.adapter :typhoeus
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Dirble
2
+ module Errors
3
+ class InvalidRequestType < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Dirble
2
+ class PrimaryCategory < Category
3
+
4
+ def self.all
5
+ Dirble::Category.primary
6
+ end
7
+
8
+ def children
9
+ self.class.call_api_with_results(
10
+ request_type: :get,
11
+ query: "childCategories/apikey/{{api_key}}/primaryid/#{id}",
12
+ factory_klass: Dirble::Category
13
+ )
14
+ end
15
+
16
+ def primary
17
+ true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module Dirble
2
+ class QueryExecuter
3
+ REQUEST_TYPES = [:get, :post, :put, :delete, :patch]
4
+
5
+ def initialize(connection, query_params)
6
+ self.connection = connection
7
+ self.request_type = query_params[:request_type]
8
+ self.query = query_params[:query]
9
+ self.form_fields = query_params[:form_fields]
10
+ end
11
+
12
+ def execute
13
+ guard_query_params
14
+ replace_placeholder_with_api_key
15
+ if form_fields
16
+ connection.send(request_type, query, form_fields)
17
+ else
18
+ connection.send(request_type, query)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ attr_accessor :connection, :request_type, :query, :form_fields
25
+
26
+ def guard_query_params
27
+ raise Dirble::Errors::InvalidRequestType, 'Bad request type provided. Please use :get or :post' unless REQUEST_TYPES.include?(request_type)
28
+ end
29
+
30
+ def replace_placeholder_with_api_key
31
+ query.gsub!("{{api_key}}", Dirble.api_key)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ module Dirble
2
+ module SimpleApiModel
3
+ def call_api_with_results(options)
4
+ factory_klass = options.delete(:factory_klass) || self
5
+ response = Dirble.connection.exec_query(options).body
6
+ build_from_api_response(factory_klass, response)
7
+ end
8
+
9
+ def build_from_api_response(target_class, response)
10
+ parsed_collection(response).each_with_object([]) do |object_params, collection|
11
+ collection << target_class.new(object_params.with_indifferent_access)
12
+ end
13
+ end
14
+
15
+ def parsed_collection(json_response)
16
+ Array.wrap(JSON.parse(json_response))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module Dirble
2
+ class Song
3
+ attr_accessor :id, :image_url, :name, :title, :spotify_url, :played_on
4
+
5
+ def initialize(args)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,72 @@
1
+ module Dirble
2
+ class Station
3
+ REQUIRED_FIELDS = [:name, :website, :directory]
4
+
5
+ extend Dirble::SimpleApiModel
6
+
7
+ attr_accessor :id, :name, :stream_url, :description, :website, :url_id,
8
+ :song_history, :categories, :country, :bitrate, :status
9
+
10
+ def initialize(options)
11
+ self.id = options[:id]
12
+ self.name = options[:name]
13
+ self.website = options[:website]
14
+ self.country = options[:country]
15
+ self.bitrate = options[:bitrate]
16
+ self.status = options[:status]
17
+ end
18
+
19
+ class << self
20
+
21
+ def create(params)
22
+ guard_creation_of_station(params)
23
+ call_api_with_results(
24
+ request_type: :post,
25
+ query: 'station/apikey/{{api_key}}',
26
+ form_fields: params
27
+ ).first
28
+ end
29
+
30
+ def search(keyword)
31
+ call_api_with_results(
32
+ request_type: :get,
33
+ query: "search/apikey/{{api_key}}/search/#{keyword.parameterize}"
34
+ )
35
+ end
36
+
37
+ def by_continent(name)
38
+ call_api_with_results(
39
+ request_type: :get,
40
+ query: "continent/apikey/{{api_key}}/continent/#{name.parameterize}"
41
+ )
42
+ end
43
+
44
+ def by_country(code)
45
+ call_api_with_results(
46
+ request_type: :get,
47
+ query: "country/apikey/{{api_key}}/country/#{code}"
48
+ )
49
+ end
50
+
51
+ def count
52
+ response = Dirble.connection.exec_query(
53
+ request_type: :get,
54
+ query: 'amountStation/apikey/{{api_key}}'
55
+ ).body
56
+ JSON.parse(response)['amount']
57
+ end
58
+
59
+ private
60
+
61
+ def guard_creation_of_station(params)
62
+ unless all_required_fields?(params.keys)
63
+ raise ArgumentError, 'You forgot about one of this keys: name, website, directory'
64
+ end
65
+ end
66
+
67
+ def all_required_fields?(keys)
68
+ REQUIRED_FIELDS.all? { |required_field| keys.include?(required_field) }
69
+ end
70
+ end
71
+ end
72
+ end