hearthstone_card_api 0.0.1 → 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: 79c49b3ddb6dbb487b03968fe8789566829139c5
4
- data.tar.gz: 5fd230ca502bd1cd9edfc70fbb93778e147ad9f9
3
+ metadata.gz: 4d79457c921527c72de8580ffd57d65f1986c6a6
4
+ data.tar.gz: 97499ca213a04cddd5872bbd3b2d90df5b6c07d6
5
5
  SHA512:
6
- metadata.gz: 9e6123b7760fb892afd9e2b03298aaa7be945d55f2360b83de7ddb236426191226b5cf3595840319fbd85f2596d3cac565d7d13731bbd67044747fa11018aaee
7
- data.tar.gz: e24b369ce6d7fb4859f1666e3f3e9ca633b12d215c7b6cddb47ba3c0fbeef840d177c586f725c4838b0c792af4cb69ab92f6a4336f10eb36162d12d0e245377c
6
+ metadata.gz: bff57d7eff33868cb9c7a0537b73a0f4af09b2868ee64eb461c7895dad9523b0dcbfe3fab3a567f0d573639e58c26c8aecebdd6943288eafa95cc96b5f0ff387
7
+ data.tar.gz: 19ca1b33ade1e2da85813d642e01e9009806d42bba5a592614a64e76cf907aba7ccf24f4ed0b5f3778287c8929d02fa985112807bbd44a3a0c7970b057720c17
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *bundle
2
+
3
+ *swo
4
+ *swp
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015, 2016 Jeane Ramos
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,21 @@
1
+ require File.expand_path('../lib/hearthstone_card_api/version', __FILE__)
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'hearthstone_card_api'
7
+ s.version = HearthstoneCardApi::VERSION
8
+ s.date = '2017-03-10'
9
+ s.summary = "An easy to use Ruby wrapper for the api at HearthstoneAPI.com"
10
+ s.description = "An easy to use Ruby wrapper for the api at HearthstoneAPI.com. Return cards in multiple formats (objects, json, hash) and with multiple filters easily."
11
+ s.authors = ["Jeane Ramos"]
12
+ s.email = 'msjeanneramos@gmail.com'
13
+ s.require_path = "lib"
14
+
15
+ s.files = `git ls-files -z`.split("\x0")
16
+ s.add_dependency "unirest", "~> 1.1"
17
+
18
+ s.homepage = 'https://github.com/jeanbeanie/hearthstone_card_api.git'
19
+ s.license = 'MIT'
20
+ end
21
+
@@ -0,0 +1,47 @@
1
+ module HearthstoneCardApi
2
+
3
+ class Caller
4
+ attr_accessor :api_url, :api_key, :filters, :collectible
5
+
6
+ def initialize(args={})
7
+ @api_key = HearthstoneCardApi.api_key
8
+ @api_url = "https://omgvamp-hearthstone-v1.p.mashape.com/"
9
+
10
+ @collectible = "1"
11
+ @filters = {cost: nil, attack: nil, health: nil, durability: nil}
12
+ end
13
+
14
+ def return_cards(args={})
15
+ url = add_filters_to_url(args)
16
+ api_response(endpoint(url))
17
+ end
18
+
19
+ private
20
+ def add_filters_to_url(args={})
21
+ url = args[:url]
22
+ arg_filters = args[:filters] || {}
23
+ arg_collectible = args[:collectible] || "1"
24
+ url += "?"
25
+ url += "&collectible=#{arg_collectible}"
26
+ arg_filters.each do |key, value|
27
+ if !value.nil?
28
+ url += "&#{key}=#{value}"
29
+ end
30
+ end
31
+ return url
32
+ end
33
+
34
+ def api_response(url_param)
35
+ url = url_param
36
+ response = Unirest.get url,
37
+ headers:{
38
+ "X-Mashape-Key" => api_key}
39
+ data = HearthstoneCardApi::Data.new(response)
40
+ return data.formatted
41
+ end
42
+
43
+ def endpoint(url_param)
44
+ api_url + url_param
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,59 @@
1
+ module HearthstoneCardApi
2
+ class Data
3
+ attr_reader :format, :data, :objs
4
+
5
+ def initialize(api_response)
6
+ @objs = []
7
+ @data = api_response
8
+ @format = HearthstoneCardApi.data_format
9
+ end
10
+
11
+ def formatted
12
+ return self.send("return_#{format}", data)
13
+ end
14
+
15
+ private
16
+ def return_objects(d)
17
+ data = d.body
18
+ if data.is_a? Hash
19
+ if data.key?("error")
20
+ return data
21
+ else
22
+ data.each_pair do |key,value|
23
+ data_to_objects(value)
24
+ end
25
+ end
26
+ elsif data.is_a? Array
27
+ data_to_objects(data)
28
+ end
29
+ return objs
30
+ end
31
+
32
+ def return_json(data)
33
+ json = data.raw_body
34
+ return json
35
+ end
36
+
37
+ def return_hash(data)
38
+ hashes = data.body
39
+ return hashes
40
+ end
41
+
42
+ def data_to_objects(hash)
43
+ hash.each do |a|
44
+ b = Hash[a.map{|(k,v)| [underscore(k).to_sym, v]}]
45
+ obj = Struct.new(*b.keys).new(*b.values)
46
+ objs.push(obj)
47
+ end
48
+ return objs
49
+ end
50
+
51
+ def underscore(string)
52
+ string.gsub(/::/, '/').
53
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
54
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
55
+ tr("-", "_").
56
+ downcase
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module HearthstoneCardApi
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,54 @@
1
+ module HearthstoneCardApi
2
+
3
+ require_relative 'helpers/configuration'
4
+ require_relative 'hearthstone_card_api/caller'
5
+ require_relative 'hearthstone_card_api/data'
6
+ require 'unirest'
7
+ extend Configuration
8
+
9
+ define_setting :api_key
10
+ define_setting :data_format, "objects"
11
+
12
+ class Public
13
+
14
+ attr_accessor :call
15
+
16
+ def initialize(args={})
17
+ @call = HearthstoneCardApi::Caller.new
18
+ end
19
+
20
+ def get_cards(args={})
21
+ url = "cards"
22
+ args[:url] = url
23
+ call.return_cards(args)
24
+ end
25
+
26
+ def search_cards(args={}) #expects {:name}
27
+ name = args[:name]
28
+ url= "cards/search/#{name}"
29
+ args[:url] = url
30
+ call.return_cards(args)
31
+ end
32
+
33
+ def get_cards_by(args={}) #expects {:trait, :value}
34
+ trait = args[:trait]
35
+ value = args[:value]
36
+ url= "cards/#{trait}/#{value}"
37
+ args[:url] = url
38
+ call.return_cards(args)
39
+ end
40
+
41
+ def get_card_by_id(args={}) #expects {:id}
42
+ url = "cards/#{args[:id]}"
43
+ args[:url] = url
44
+ call.return_cards(args)
45
+ end
46
+
47
+ def get_card_by_name(args={}) #expects {:name}
48
+ name = args[:name]
49
+ url = "cards/#{name}"
50
+ args[:url] = url
51
+ call.return_cards(args)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ module Configuration
2
+
3
+ def configuration
4
+ yield self
5
+ end
6
+
7
+ def define_setting(name, default = nil)
8
+ class_variable_set("@@#{name}", default)
9
+
10
+ define_class_method "#{name}=" do |value|
11
+ class_variable_set("@@#{name}", value)
12
+ end
13
+
14
+ define_class_method name do
15
+ class_variable_get("@@#{name}")
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def define_class_method(name, &block)
22
+ (class << self; self; end).instance_eval do
23
+ define_method name, &block
24
+ end
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hearthstone_card_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeane Ramos
@@ -30,7 +30,15 @@ email: msjeanneramos@gmail.com
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
- files: []
33
+ files:
34
+ - ".gitignore"
35
+ - LICENSE.txt
36
+ - hearthstone_card_api.gemspec
37
+ - lib/hearthstone_card_api.rb
38
+ - lib/hearthstone_card_api/caller.rb
39
+ - lib/hearthstone_card_api/data.rb
40
+ - lib/hearthstone_card_api/version.rb
41
+ - lib/helpers/configuration.rb
34
42
  homepage: https://github.com/jeanbeanie/hearthstone_card_api.git
35
43
  licenses:
36
44
  - MIT