riksteatern 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: fb9b51d8ad03311a39f4f6579bc7cd042100bf57
4
+ data.tar.gz: 3408bc0c2b2d89276941641ee986f64341daa427
5
+ SHA512:
6
+ metadata.gz: 9529b9c5a434b9818bd7606eb66365b8dbbb8647f584bfc1f165bd3a9cbf3ba4d847f6fb9b51b30113d42b204c3cf257f6d1ebc1e27a1483785b7321de20bd33
7
+ data.tar.gz: 417e6284e0af28f8d4208032eae59518a55513e2f5396fc826eea18b1d6ff7847efc374385d9d382a6d455ab1d0b91f495172f023bd45a3b60f8e1d52fd4b08d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in riksteatern.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Peter Hellberg
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,81 @@
1
+ # Riksteatern
2
+
3
+ A small Ruby client for the Riksteatern API.
4
+
5
+ You can find out more about the API (In Swedish) on [http://data.riksteatern.se/](http://data.riksteatern.se/).
6
+
7
+ [![Build Status](https://travis-ci.org/peterhellberg/riksteatern.png?branch=master)](https://travis-ci.org/peterhellberg/riksteatern)
8
+
9
+ ## Dependencies
10
+
11
+ No runtime dependencies. Bundler, Rake and Minitest are required for development.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'riksteatern'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install riksteatern
26
+
27
+ ## Usage
28
+
29
+ *You need to be authenticated to use the API.*
30
+
31
+ **Access request: [http://data.riksteatern.se/ansokan/](http://data.riksteatern.se/ansokan/)**
32
+
33
+ ```ruby
34
+ Riksteatern.account('<USERNAME>', '<PASSWORD>')
35
+ ```
36
+
37
+ Now you are ready to start fetching data :)
38
+
39
+ ```ruby
40
+ # Find an event by id
41
+ e = Riksteatern::Event.find(992423)
42
+ e.name #=> "En mans föreställning"
43
+
44
+ # Number of events in Stockholms län
45
+ Riksteatern::Event.all(regionName: 'Stockholms län').size #=> 46
46
+
47
+ # Find a venue by id
48
+ v = Riksteatern::Venue.find(2260101)
49
+
50
+ ## The region name of the location
51
+ v.location.region #=> "Västernorrlands län"
52
+
53
+ ## Print the address of the venue
54
+ puts v.address
55
+
56
+ # Find a production by id
57
+ p = Riksteatern::Production.find(42)
58
+
59
+ ## Get the number of pictures
60
+ p.pictures.size #=> 4
61
+
62
+ ## Get the categories of the production
63
+ p.categories #=> ["Barn/Skola", "Teater", "Mim", "Musikteater"]
64
+ ```
65
+
66
+ You might also want to take a look at the `specs`
67
+
68
+ ## Suggestions for improving the API
69
+
70
+ * Correct the spelling of `accesibility` *sic*
71
+ * Change `category` to `categories` since it is a list
72
+ * Upper limit on how many results to return
73
+ * Pagination of results
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
80
+ 4. Push to the branch (`git push origin my-new-feature`)
81
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rake/testtask'
4
+ require 'bundler/gem_tasks'
5
+
6
+ task :default => :spec
7
+
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.test_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc "Console"
13
+ task :console do
14
+ Bundler.with_clean_env do
15
+ exec 'pry -r./lib/riksteatern'
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'riksteatern/api'
4
+ require 'riksteatern/config'
5
+ require 'riksteatern/errors'
6
+ require 'riksteatern/event'
7
+ require 'riksteatern/http'
8
+ require 'riksteatern/producer'
9
+ require 'riksteatern/production'
10
+ require 'riksteatern/venue'
11
+ require 'riksteatern/version'
12
+
13
+ module Riksteatern
14
+ class << self
15
+ def configure(&block)
16
+ yield(config)
17
+ end
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+
23
+ def api
24
+ @api ||= Api.new(config)
25
+ end
26
+
27
+ def account(username, password)
28
+ configure do |c|
29
+ c.username = username
30
+ c.password = password
31
+ end
32
+
33
+ self
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ module Riksteatern
7
+ class Api
8
+ attr_accessor :config
9
+
10
+ def initialize(config)
11
+ @config = config
12
+
13
+ if @config.username.nil? || @config.password.nil?
14
+ raise UnauthorizedUser, "Blank username/password"
15
+ end
16
+ end
17
+
18
+ def locations(params = {})
19
+ get_array(:lokal, 'locations', params)
20
+ end
21
+
22
+ def productions(params = {})
23
+ get_array(:produktion, 'productions', params)
24
+ end
25
+
26
+ def repertoires(params = {})
27
+ get_array(:repertoar, 'repertoires', params)
28
+ end
29
+
30
+ def get_array(endpoint, field_name, params)
31
+ get_json(endpoint, params)[field_name] || []
32
+ end
33
+
34
+ def get(path, params = {})
35
+ http_get prepared_uri(path, params)
36
+ end
37
+
38
+ def get_json(endpoint, params = {})
39
+ load_json get("#{endpoint}.json", params) || "{}"
40
+ end
41
+
42
+ private
43
+
44
+ def http_get(uri)
45
+ uri.userinfo = URI.escape "#{@config.username}:#{@config.password}", '@'
46
+
47
+ @config.http_client.get(uri)
48
+ end
49
+
50
+ def load_json(doc)
51
+ @config.json_parser.call(doc)
52
+ end
53
+
54
+ def prepared_uri(endpoint, params = {})
55
+ endpoint_uri(endpoint).tap do |uri|
56
+ uri.query = URI.encode_www_form(params)
57
+ end
58
+ end
59
+
60
+ def endpoint_uri(endpoint)
61
+ URI.parse("#{@config.base_url}/#{endpoint}")
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Riksteatern
4
+ class Config
5
+ attr_accessor :base_url, :username, :password,
6
+ :json_parser, :http_client, :debug
7
+
8
+ def initialize
9
+ @base_url = 'https://riksteatern.se:8181/api'
10
+ @username = nil
11
+ @password = nil
12
+ @json_parser = ->(d) { JSON.parse(d) }
13
+ @http_client = Riksteatern::HTTP
14
+ @debug = false
15
+
16
+ yield self if block_given?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ module Riksteatern
4
+ class UnauthorizedUser < StandardError
5
+ end
6
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ module Riksteatern
4
+ class Event
5
+ class << self
6
+ def find(id)
7
+ data = get_data(eventId: id)
8
+ new(data.first) if data.any?
9
+ end
10
+
11
+ def all(params = {})
12
+ get_data(params).map { |c| new(c) }
13
+ end
14
+
15
+ alias :query :all
16
+
17
+ def get_data(params = {})
18
+ Riksteatern.api.repertoires(params)
19
+ end
20
+ end
21
+
22
+ attr_reader :data, :api_url, :id, :arranger_id, :arranger_name,
23
+ :name, :meta_description, :information_link, :venue_api_url,
24
+ :venue_name, :region_name, :municipality_name, :date, :time,
25
+ :ticket_information, :ticket_information_scenpass,
26
+ :ticket_information_online, :scenpass_benefit,
27
+ :public_show, :co_producer, :cancelled
28
+
29
+ def initialize(data)
30
+ @data = data.tap do |d|
31
+ i = d['eventInformation'] || {}
32
+
33
+ @api_url = d['eventUrl']
34
+ @id = @api_url.to_s.split('/').last.to_i
35
+ @arranger_id = d['arrangerId'].to_i
36
+ @arranger_name = d['arrangerName']
37
+
38
+ @name = i['eventName']
39
+ @meta_description = i['eventMetaDescription']
40
+ @information_link = i['informationLink']
41
+ @venue_api_url = i['venueUrl']
42
+ @venue_name = i['venueName']
43
+ @region_name = i['regionName']
44
+ @municipality_name = i['municipalityName']
45
+ @date = i['eventDate']
46
+ @time = i['eventTime']
47
+ @ticket_information = i['ticketInformation']
48
+ @ticket_information_scenpass = i['ticketInformationScenpass']
49
+ @ticket_information_online = i['ticketInformationOnline']
50
+ @scenpass_benefit = i['scenpassBenefit']
51
+ @public_show = i['publicShow']
52
+ @co_producer = i['coProducer']
53
+ @cancelled = i['cancelledEvent']
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'net/http'
4
+
5
+ module Riksteatern
6
+ class HTTP
7
+ class << self
8
+ def get(uri)
9
+ response = make_request(uri)
10
+
11
+ if response.code == '401'
12
+ raise UnauthorizedUser, "Wrong username/password"
13
+ elsif response.code == '200'
14
+ response.body
15
+ else
16
+ nil
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def setup_http(uri, &block)
23
+ Net::HTTP.start(uri.host, uri.port, {
24
+ use_ssl: uri.scheme == 'https'
25
+ }) do |http|
26
+ http.read_timeout = 60
27
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
28
+
29
+ yield(http)
30
+ end
31
+ end
32
+
33
+ def make_request(uri)
34
+ setup_http(uri) do |http|
35
+ response = http.request(prepared_request(uri))
36
+
37
+ http.finish
38
+
39
+ response
40
+ end
41
+ end
42
+
43
+ def prepared_request(uri)
44
+ Net::HTTP::Get.new(uri.request_uri).tap do |r|
45
+ r.basic_auth URI.decode(uri.user), URI.decode(uri.password)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ module Riksteatern
4
+ class Producer
5
+ attr_reader :id, :name, :main_contact_id, :homepage, :logotype
6
+
7
+ def initialize(data)
8
+ @id = data['producerId']
9
+ @name = data['producerName']
10
+ @main_contact_id = data['mainContactId']
11
+ @homepage = data['producerHomepage']
12
+ @logotype = data['producerLogotype']
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Riksteatern
4
+ class Production
5
+ class << self
6
+ def find(id)
7
+ data = get_data(productionId: id)
8
+ new(data.first) if data.any?
9
+ end
10
+
11
+ def all(params = {})
12
+ get_data(params).map { |c| new(c) }
13
+ end
14
+
15
+ alias :query :all
16
+
17
+ def get_data(params = {})
18
+ Riksteatern.api.productions(params)
19
+ end
20
+ end
21
+
22
+ attr_reader :data, :api_url, :id, :name, :intro_text, :description,
23
+ :homepage, :participants, :number_of_participants,
24
+ :performance_length_hours, :performance_length_minutes,
25
+ :pause, :categories, :pictures,
26
+ :first_performance_location, :first_performance_date,
27
+ :premiere_location, :premiere_date,
28
+ :new_premiere_location, :new_premiere_date,
29
+ :producer
30
+
31
+ def initialize(data)
32
+ @data = data.tap do |d|
33
+ i = d['productionInformation'] || {}
34
+
35
+ @api_url = d['productionUrl']
36
+ @id = Integer(@api_url.split('/').last)
37
+ @name = i['productionName']
38
+ @intro_text = i['productionIntroText']
39
+ @description = i['productionDescription']
40
+ @homepage = i['productionHomepage']
41
+ @participants = i['participants']
42
+ @number_of_participants = i['numberOfParticipants']
43
+ @performance_length_hours = i['performanceLengthHrs'].to_i
44
+ @performance_length_minutes = i['performanceLengthMns'].to_i
45
+ @pause = i['pause']
46
+ @categories = i['category']
47
+ @pictures = i['productionPictures']
48
+ @first_performance_location = i['firstPerformanceLocation']
49
+ @first_performance_date = i['firstPerformanceDate']
50
+ @premiere_location = i['premiereLocation']
51
+ @premiere_date = i['premiereDate']
52
+ @new_premiere_location = i['newPremiereLoction']
53
+ @new_premiere_date = i['newPremiereDate']
54
+
55
+ @producer = Producer.new(d['producerInformation'])
56
+ end
57
+ end
58
+ end
59
+ end