From 88c74b6d46c35ad590d208994bb2adbc1e0adeca Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Sat, 1 Jul 2017 16:50:00 -0500 Subject: [PATCH 1/6] begin ruby directory and spotify json file --- src-ruby/albums.json | 517 +++++++++++++++++++++++++++++++++++++++++++ src-ruby/main.rb | 0 2 files changed, 517 insertions(+) create mode 100644 src-ruby/albums.json create mode 100644 src-ruby/main.rb diff --git a/src-ruby/albums.json b/src-ruby/albums.json new file mode 100644 index 0000000..a466166 --- /dev/null +++ b/src-ruby/albums.json @@ -0,0 +1,517 @@ +albums +{ + + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=5&album_type=single&market=ES", + "items": [ + { + "album_type": "single", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6" + }, + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6", + "id": "1vCWHaC5f2uS3yhpwWbIA6", + "name": "Avicii", + "type": "artist", + "uri": "spotify:artist:1vCWHaC5f2uS3yhpwWbIA6" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1rw8ZTLnDHd74TWDDukjVi" + }, + "href": "https://api.spotify.com/v1/artists/1rw8ZTLnDHd74TWDDukjVi", + "id": "1rw8ZTLnDHd74TWDDukjVi", + "name": "Conrad Sewell", + "type": "artist", + "uri": "spotify:artist:1rw8ZTLnDHd74TWDDukjVi" + } + ], + "available_markets": [ + "AD", + "AR", + "AT", + "AU", + "BE", + "BG", + "BO", + "BR", + "CH", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DE", + "DK", + "DO", + "EC", + "EE", + "ES", + "FI", + "FR", + "GB", + "GR", + "GT", + "HK", + "HN", + "HU", + "ID", + "IE", + "IS", + "IT", + "JP", + "LI", + "LT", + "LU", + "LV", + "MC", + "MT", + "MY", + "NI", + "NL", + "NO", + "NZ", + "PA", + "PE", + "PH", + "PL", + "PT", + "PY", + "SE", + "SG", + "SK", + "SV", + "TR", + "TW", + "UY" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/1aHspryG74tpoMCOFLQGYV" + }, + "href": "https://api.spotify.com/v1/albums/1aHspryG74tpoMCOFLQGYV", + "id": "1aHspryG74tpoMCOFLQGYV", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/b7a4395d5d49c9f8b5fa917f927629577d78ca9d", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/52e0136ffdf197c1ea4b0eb31d652b95002e60d9", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/112a41faf668770024740a1dfd0b920221554c5b", + "width": 64 + } + ], + "name": "Taste The Feeling (Avicii Vs. Conrad Sewell)", + "type": "album", + "uri": "spotify:album:1aHspryG74tpoMCOFLQGYV" + }, + { + "album_type": "single", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6" + }, + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6", + "id": "1vCWHaC5f2uS3yhpwWbIA6", + "name": "Avicii", + "type": "artist", + "uri": "spotify:artist:1vCWHaC5f2uS3yhpwWbIA6" + } + ], + "available_markets": [ + "AD", + "AR", + "AT", + "AU", + "BE", + "BG", + "BO", + "BR", + "CH", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DE", + "DK", + "DO", + "EC", + "EE", + "ES", + "FI", + "GB", + "GR", + "GT", + "HK", + "HN", + "HU", + "ID", + "IE", + "IS", + "IT", + "JP", + "LI", + "LT", + "LU", + "LV", + "MC", + "MT", + "MY", + "NI", + "NL", + "NO", + "NZ", + "PA", + "PE", + "PH", + "PL", + "PT", + "PY", + "SE", + "SG", + "SK", + "SV", + "TR", + "TW", + "UY" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/1lhkoUf2SJxdrREtP72HV9" + }, + "href": "https://api.spotify.com/v1/albums/1lhkoUf2SJxdrREtP72HV9", + "id": "1lhkoUf2SJxdrREtP72HV9", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/78abe3d06d972e1654ae69d646d3cd0b4d4217de", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/4bae64b49815c2206848a4f858ff1a1eadcd4e32", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/53ce2336ecf81eb36480b3d7313eb8cc1f2346ce", + "width": 64 + } + ], + "name": "Pure Grinding (iSHi Remix)", + "type": "album", + "uri": "spotify:album:1lhkoUf2SJxdrREtP72HV9" + }, + { + "album_type": "single", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6" + }, + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6", + "id": "1vCWHaC5f2uS3yhpwWbIA6", + "name": "Avicii", + "type": "artist", + "uri": "spotify:artist:1vCWHaC5f2uS3yhpwWbIA6" + } + ], + "available_markets": [ + "AD", + "AR", + "AT", + "AU", + "BE", + "BG", + "BO", + "BR", + "CH", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DE", + "DK", + "DO", + "EC", + "EE", + "ES", + "FI", + "FR", + "GB", + "GR", + "GT", + "HK", + "HN", + "HU", + "ID", + "IE", + "IS", + "IT", + "JP", + "LI", + "LT", + "LU", + "LV", + "MC", + "MT", + "MY", + "NI", + "NL", + "NO", + "NZ", + "PA", + "PE", + "PH", + "PL", + "PT", + "PY", + "SE", + "SG", + "SK", + "SV", + "TR", + "TW", + "UY" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/4nCNj68SZym6hNxXDkRtjN" + }, + "href": "https://api.spotify.com/v1/albums/4nCNj68SZym6hNxXDkRtjN", + "id": "4nCNj68SZym6hNxXDkRtjN", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/c735be011394f4e7cdf1ebbf95d112cb69fd3414", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/7f4221fda86e4daa539fd29233fadad039cc46d9", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/e1930bf1293d89799a0e382b40ebad5455b11857", + "width": 64 + } + ], + "name": "Broken Arrows (Remixes)", + "type": "album", + "uri": "spotify:album:4nCNj68SZym6hNxXDkRtjN" + }, + { + "album_type": "single", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6" + }, + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6", + "id": "1vCWHaC5f2uS3yhpwWbIA6", + "name": "Avicii", + "type": "artist", + "uri": "spotify:artist:1vCWHaC5f2uS3yhpwWbIA6" + } + ], + "available_markets": [ + "AD", + "AR", + "AT", + "AU", + "BE", + "BG", + "BO", + "BR", + "CH", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DE", + "DK", + "DO", + "EC", + "EE", + "ES", + "FI", + "FR", + "GB", + "GR", + "GT", + "HK", + "HN", + "HU", + "ID", + "IE", + "IS", + "IT", + "JP", + "LI", + "LT", + "LU", + "LV", + "MC", + "MT", + "MY", + "NI", + "NL", + "NO", + "NZ", + "PA", + "PE", + "PH", + "PL", + "PT", + "PY", + "SE", + "SG", + "SK", + "SV", + "TR", + "TW", + "UY" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/0G1HMRmXtK9LZAa4TijYLv" + }, + "href": "https://api.spotify.com/v1/albums/0G1HMRmXtK9LZAa4TijYLv", + "id": "0G1HMRmXtK9LZAa4TijYLv", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/8202f8de7f1b57e676a2588b76beeefb9c3ff401", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/1eb44a79643485d3344f856b2a5d337bb62e5410", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/e42b67ce5361bbe87eb9b4a39b8a59b5ef1746a5", + "width": 64 + } + ], + "name": "For A Better Day (Remixes)", + "type": "album", + "uri": "spotify:album:0G1HMRmXtK9LZAa4TijYLv" + }, + { + "album_type": "single", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6" + }, + "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6", + "id": "1vCWHaC5f2uS3yhpwWbIA6", + "name": "Avicii", + "type": "artist", + "uri": "spotify:artist:1vCWHaC5f2uS3yhpwWbIA6" + } + ], + "available_markets": [ + "AD", + "AR", + "AT", + "AU", + "BE", + "BG", + "BO", + "BR", + "CH", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DE", + "DK", + "DO", + "EC", + "EE", + "ES", + "FI", + "FR", + "GB", + "GR", + "GT", + "HK", + "HN", + "HU", + "ID", + "IE", + "IS", + "IT", + "JP", + "LI", + "LT", + "LU", + "LV", + "MC", + "MT", + "MY", + "NI", + "NL", + "NO", + "NZ", + "PA", + "PE", + "PH", + "PL", + "PT", + "PY", + "SE", + "SG", + "SK", + "SV", + "TR", + "TW", + "UY" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/1oRvqWh2IkWMDF90jDEgzz" + }, + "href": "https://api.spotify.com/v1/albums/1oRvqWh2IkWMDF90jDEgzz", + "id": "1oRvqWh2IkWMDF90jDEgzz", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/2626556c707ad974cdf465d560375c3bdc7342fd", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/8878dee1c0f5c505a3c33ae184649153e2e06d63", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/b0ce4aaf8ce7ecd053df0ef57c28adec1fb7bf34", + "width": 64 + } + ], + "name": "For A Better Day (KSHMR Remix)", + "type": "album", + "uri": "spotify:album:1oRvqWh2IkWMDF90jDEgzz" + } + ], + "limit": 5, + "next": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=5&limit=5&album_type=single&market=ES", + "offset": 0, + "previous": null, + "total": 45 + +} diff --git a/src-ruby/main.rb b/src-ruby/main.rb new file mode 100644 index 0000000..e69de29 From 366427fd5a4cf2d2962855f3ca42d61c63362e45 Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Tue, 25 Jul 2017 19:10:37 -0500 Subject: [PATCH 2/6] add ruby comparisons --- Gemfile | 3 +++ README.md | 40 +++++++++++++++++++++++++++++++++++++--- src-ruby/albums.json | 2 -- src-ruby/main.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Gemfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4a1b86b --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'json' diff --git a/README.md b/README.md index 8cadcfa..592ff10 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,15 @@ First in Python: u'https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=5&album_type=single&market=ES' ``` +Second, in Ruby: + +```ruby +require 'json' +albums = JSON.parse(File.read('albums.json')) +puts albums['href'] +u'https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=5&album_type=single&market=ES' +``` + In Haskell: ``` @@ -89,6 +98,13 @@ Python: u'single' ``` +Ruby: + +```puts albums['items'][0]['album_type'] + +Example two is: single +``` + Haskell: ```haskell @@ -106,7 +122,16 @@ Python: # list comprehension version >>> [item['name'] for item in albums['items']] [u'Taste The Feeling (Avicii Vs. Conrad Sewell)', u'Pure Grinding (iSHi Remix)', u'Broken Arrows (Remixes)', u'For A Better Day (Remixes -)', u'For A Better Day (KSHMR Remix)'] +)', u'For A Better Day (KSHMR Remix)'] + +```ruby +list = [] +for name in albums['items'] + list.push(name) +end +puts list + +``` # for loop version lst = [] @@ -143,12 +168,21 @@ for item in albums['items']: lst.append(image['height']) ``` +```ruby +list2 = [] +for item in albums['items'] + for image in item['images'] + list2.push(image['height']) + end +end +``` + ```haskell λ> toListOf (\obj -> key "items" (values (key "images" (values (key "height" (_Number obj)))))) albums [640.0,300.0,64.0,640.0,300.0,64.0,640.0,300.0,64.0,640.0,300.0,64.0,640.0,300.0,64.0] ``` -Now let's do something like we'd actually be asked to do at our jobs. +Now let's do something like we'd actually be asked to do at our jobs. Boss: What albums are available in the HK and LU markets?!? @@ -170,7 +204,7 @@ Of course for me, this is well past the point where I usually start naming my le λ> let availableMarkets album = key "available_markets" (values (_String album)) λ> let isHkOrLU i = i `elem` ["HK","LU"] λ> let albumName album = key "name" (_String album) -λ> toListOf (\album -> items (filtered (anyOf availableMarkets isHkOrLU) (albumName album))) albums +λ> toListOf (\album -> items (filtered (anyOf availableMarkets isHkOrLU) (albumName album))) albums ["Taste The Feeling (Avicii Vs. Conrad Sewell)","Pure Grinding (iSHi Remix)","Broken Arrows (Remixes)","For A Better Day (Remixes)","For A Better Day (KSHMR Remix)"] ``` diff --git a/src-ruby/albums.json b/src-ruby/albums.json index a466166..bd21249 100644 --- a/src-ruby/albums.json +++ b/src-ruby/albums.json @@ -1,6 +1,4 @@ -albums { - "href": "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=5&album_type=single&market=ES", "items": [ { diff --git a/src-ruby/main.rb b/src-ruby/main.rb index e69de29..4c53902 100644 --- a/src-ruby/main.rb +++ b/src-ruby/main.rb @@ -0,0 +1,28 @@ +require 'json' + +# Part 1 +# Example 1 +albums = JSON.parse(File.read('albums.json')) +puts albums['href'] + +# Example 2 +# Get albums type +puts albums['items'][0]['album_type'] + +# Example 3 +# get the names of all albums +list = [] +for name in albums['items'] + list.push(name) +end +puts list + +# Example 4 +list2 = [] +for item in albums['items'] + for image in item['images'] + list2.push(image['height']) + end +end + +puts list2 From 09fa68cf5cfb772034ce31c6872fccddb1490a44 Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Tue, 25 Jul 2017 19:11:59 -0500 Subject: [PATCH 3/6] fix readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 592ff10..7fd79dc 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,8 @@ u'single' Ruby: -```puts albums['items'][0]['album_type'] +``` +puts albums['items'][0]['album_type'] Example two is: single ``` @@ -122,7 +123,8 @@ Python: # list comprehension version >>> [item['name'] for item in albums['items']] [u'Taste The Feeling (Avicii Vs. Conrad Sewell)', u'Pure Grinding (iSHi Remix)', u'Broken Arrows (Remixes)', u'For A Better Day (Remixes -)', u'For A Better Day (KSHMR Remix)'] +)', u'For A Better Day (KSHMR Remix)'] +``` ```ruby list = [] From b8c9df3a9701239effab528f84ac324570a8d496 Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Tue, 25 Jul 2017 19:12:54 -0500 Subject: [PATCH 4/6] fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fd79dc..4a8a7ee 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ end puts list ``` - +```python # for loop version lst = [] for name in albums['items']: From 2a05fa5fe72bd44b7a89bea9a5ec9c868bb47047 Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Tue, 25 Jul 2017 19:14:41 -0500 Subject: [PATCH 5/6] readme --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4a8a7ee..8dc8edd 100644 --- a/README.md +++ b/README.md @@ -124,22 +124,22 @@ Python: >>> [item['name'] for item in albums['items']] [u'Taste The Feeling (Avicii Vs. Conrad Sewell)', u'Pure Grinding (iSHi Remix)', u'Broken Arrows (Remixes)', u'For A Better Day (Remixes )', u'For A Better Day (KSHMR Remix)'] -``` +``` +```python +# for loop version +lst = [] +for name in albums['items']: + lst.append(name) +``` +Ruby: ```ruby list = [] for name in albums['items'] list.push(name) end puts list - -``` -```python -# for loop version -lst = [] -for name in albums['items']: - lst.append(name) -``` +``` Haskell: ``` From 6921c1d10724c3cab8d8e1f150a9f8c2788f62c7 Mon Sep 17 00:00:00 2001 From: Matt McCullar Date: Tue, 25 Jul 2017 19:15:40 -0500 Subject: [PATCH 6/6] read --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8dc8edd..ef4f684 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,7 @@ for item in albums['items']: lst.append(image['height']) ``` +Ruby: ```ruby list2 = [] for item in albums['items']