twenty20 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aad59c3f8645620c5b0390527f4cadeebfa0a21a
4
- data.tar.gz: d8e020a810e145f5228240c68c39ded8f3463842
3
+ metadata.gz: f4a9f6b147774926e206dc91a7710b4c0a200627
4
+ data.tar.gz: 1501033229edd7d185bf0ced06a5883ef2684a79
5
5
  SHA512:
6
- metadata.gz: 16ff75c3315438401164caef578eb839d79674e134e74a62b09c1c1da7f1bf363964aafe4148e304dc666c72a11f3993a88f9d368c6ff4298d38971fdec4262f
7
- data.tar.gz: a90218f44099818327fdce709da9c035497aec1bc270db9b3e4a9bc844f01127a5e227d0e9a3c777c552407202a1a1963e388d377228fd13111bdb3a46f3d7e5
6
+ metadata.gz: c1af16a8ece35a9ddb302776a2c938833d1e072180b06b11ea05b9b50816726b4611b0a4b1935c69254e46b7fa0ace6dd050a580b8df53e770dace0a5878549a
7
+ data.tar.gz: b14966f2f3d0a055601c4391d4f536cabaf5d00687dcee8ff960d51b1b1efcfb63ea147eff3787a66984b2ce50e1bc0c69e95549f7b9949924e08784414ac362
data/README.md CHANGED
@@ -34,12 +34,30 @@ Instantiate a new client
34
34
  Get Featured items
35
35
 
36
36
  ```ruby
37
- client.get_featured_items()
37
+ client.get_featured_items
38
38
  ```
39
+
39
40
  ```ruby
40
41
  [#<Item:0x007feff9212348 @caption="Red Canoes on Lake Louise", @id=45414069, @source_name="upload", @image_url="https://res.cloudinary.com/twenty20/image/upload/t_standard-fit/photos/cf34293d-1eeb-47a7-94c7-99397dce4c15.jpg", @featured_at="2014-11-29T22:40:04Z", @user_first_name=nil, @user_last_name=nil, @user_avatar_url=nil, @username=nil, @display_name=nil>, #<Item:0x007feff9212190 @caption=nil, @id=49291337, @source_name="instagram", @image_url="http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/1742594_280292302171814_502487005_n.jpg", @featured_at="2014-11-29T22:20:02Z"...]
41
42
  ```
42
43
 
44
+ Get featured items and pass a block to do something cool (idk write to a file, send a text message?)
45
+
46
+ ```ruby
47
+ client.get_featured_items {|item| #do something with each item}
48
+ ```
49
+
50
+
51
+ Get challenges open for submissions
52
+
53
+ ```ruby
54
+ client.get_challenges
55
+ ```
56
+
57
+ ```ruby
58
+ client.get_challenges {|challenge| #something cool here}
59
+ ```
60
+
43
61
  ## Contributing
44
62
 
45
63
  1. Fork it ( https://github.com/AlexWheeler/twenty20_API/fork )
@@ -8,26 +8,38 @@ module Twenty20
8
8
  include HTTParty
9
9
  BASE_URI = "https://api-v2.twenty20.com/"
10
10
 
11
- def get_featured_items
11
+ def get_featured_items(&block)
12
12
  featured_route = "items/featured"
13
13
  featured_params = "?featured=true"
14
14
  resource = build_route(featured_route, featured_params)
15
15
  response = self.class.get(resource)
16
16
  if(response.code == 200)
17
- item_collection = to_item_collection(response)
18
- item_collection
17
+ item_collection = to_collection(response, "item")
18
+ if(block_given?)
19
+ item_collection.each do |item|
20
+ block.call(item)
21
+ end
22
+ else
23
+ item_collection
24
+ end
19
25
  else
20
26
  "Error"
21
27
  end
22
28
  end
23
29
 
24
- def get_challenges
30
+ def get_challenges(&block)
25
31
  challenges_route = "/challenges/open-for-submissions"
26
32
  resource = build_route(challenges_route)
27
33
  response = self.class.get(resource)
28
34
  if(response.code == 200)
29
- challenge_collection = to_challenge_collection(response)
30
- challenge_collection
35
+ challenge_collection = to_collection(response, "challenge")
36
+ if block_given?
37
+ challenge_collection.each do |challenge|
38
+ block.call(challenge)
39
+ end
40
+ else
41
+ challenge_collection
42
+ end
31
43
  else
32
44
  "Error"
33
45
  end
@@ -35,12 +47,11 @@ module Twenty20
35
47
 
36
48
  private
37
49
 
38
- def to_item_collection(response) #creates Item objects out of each element in the response object and returns this new array
39
- response.parsed_response["items"].collect {|item| Item.new(item)}
40
- end
41
-
42
- def to_challenge_collection(response)
43
- response.parsed_response["challenges"].collect {|challenge| Challenge.new(challenge)}
50
+ def to_collection(response, collection_class)
51
+ response.parsed_response[collection_class + "s"].collect do |e|
52
+ o = Object.const_get("Twenty20::" + collection_class.capitalize) #creates new array full of collection_class object (i.e. Items, Challenges)
53
+ o.new(e)
54
+ end
44
55
  end
45
56
 
46
57
  def build_route(uri, params = "")
@@ -1,3 +1,3 @@
1
1
  module Twenty20
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -30,8 +30,13 @@ describe Twenty20::Client do
30
30
  expect(response).to eq("Error")
31
31
  end
32
32
 
33
- it "doesnt return error if status is 200" do
33
+ it "can accept a block and do something cool like push challenges into a new array" do
34
+ val = []
35
+ @client.get_featured_items {|item| val.push(item)}
36
+ expect(val.empty?).to_not eq(true)
37
+
34
38
  end
39
+
35
40
  end
36
41
 
37
42
  describe "get_challenges" do
@@ -43,5 +48,11 @@ describe Twenty20::Client do
43
48
  response = @client.get_challenges
44
49
  expect(response.class).to eq(Array);
45
50
  end
51
+
52
+ it "can accept a block and do something cool like push challenges into a new array" do
53
+ val = []
54
+ @client.get_challenges {|challenge| val.push(challenge)}
55
+ expect(val.empty?).to_not eq(true)
56
+ end
46
57
  end
47
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twenty20
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Wheeler