lc-api 0.0.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lc-api/api.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'httparty'
2
+
3
+ module LcApi
4
+ class API
5
+ class << self
6
+ @@base_uri = 'http://0.0.0.0:3000/v1/'
7
+
8
+ def method_missing method_name, *args
9
+ # method_name == "get"
10
+ # "args" === "message/1, {}"
11
+ puts "args is: #{args}"
12
+ # we are expecting "get" to be the method name here, but to be safe, we allow for
13
+ # missing methods other than "get", which we toss up to super
14
+ super(method_name, args) unless [:get].include? method_name
15
+ uri = @@base_uri + args.shift
16
+
17
+ param = nil
18
+ param = args.shift
19
+
20
+ (uri = uri + "/#{param}") if (param)
21
+
22
+ puts "uri is: #{uri}"
23
+ HTTParty.get("#{uri}?key=#{LcApi.key}")
24
+ end
25
+ end
26
+
27
+ class ResponseError < StandardError
28
+ end
29
+
30
+ class NotFound < ResponseError
31
+ end
32
+
33
+ class Forbidden < ResponseError
34
+ end
35
+
36
+ class InternalServerError < ResponseError
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Category < Resource
3
+ ATTRIBUTES = %w[id name]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Location < Resource
3
+ ATTRIBUTES = %w[id slug facebook_id street_1 street_2 city state zip latitude longitude phone email date_founded]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Message < Resource
3
+ ATTRIBUTES = %w[id title part length series_id speaker_id resource_url youversionlive_id date_released]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Series < Resource
3
+ ATTRIBUTES = %w[id description hashtag slug parts promo viewable start_date end_date]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Speaker < Resource
3
+ ATTRIBUTES = %w[id name bio org_url org_name personal_url]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module LcApi
2
+ class Staff < Resource
3
+ ATTRIBUTES = %w[id name role email facebook_id location_id]
4
+ define_attribute_methods(ATTRIBUTES)
5
+ end
6
+ end
@@ -0,0 +1,85 @@
1
+ require 'json'
2
+
3
+ module LcApi
4
+ class Resource
5
+ class << self
6
+
7
+ def id(id, options=nil)
8
+ bool = options ? true : false
9
+ (options = {}) unless (options)
10
+ uri = member_name # ie 'message'
11
+ uri += "/#{id}" if id
12
+ parse_response(API.get(uri, options), bool) # method missing
13
+ end
14
+
15
+ def all
16
+ uri = member_name # get name of resource
17
+ parse_response(API.get(uri), true)
18
+ end
19
+
20
+ def parse_response(response, multiple=false)
21
+ case response.code.to_i
22
+ when 404
23
+ raise LcApi::API::NotFound.new(response), "Resource was not found"
24
+ when 403
25
+ raise LcApi::API::Forbidden.new(response), "Forbidden"
26
+ when 500
27
+ raise LcApi::API::InternalServerError.new(response), "500 Internal Server error"
28
+ end
29
+
30
+ return build_record(response.parsed_response) unless multiple
31
+
32
+ resources = []
33
+ response.parsed_response.each do |rec|
34
+ resources.push build_record(rec)
35
+ end
36
+ return resources
37
+ end
38
+
39
+ def build_record(response)
40
+ attributes = {}
41
+ response.each_pair do |key, val|
42
+ attributes[key] = val if @attributes.include? key
43
+ end
44
+ new(attributes)
45
+ end
46
+
47
+ def member_name # ie. LcApi::Message = Message = message
48
+ # ex. name = LcApi::Message
49
+ name.split('::').last.downcase
50
+ end
51
+
52
+ def define_attribute_methods(attributes)
53
+ @attributes = attributes
54
+
55
+ @attributes.each do |name|
56
+ define_method(name) { self[name] }
57
+ end
58
+ end
59
+ end
60
+
61
+ attr_accessor :attributes
62
+
63
+ def initialize attributes = {}
64
+ # raise error if user attempts to initialize a Resource by calling new
65
+ raise Error, "#{self.class} is an abstract class and cannot be instantiated" if instance_of? Resource
66
+ @attributes = {}
67
+ self.attributes = attributes # "attributes=" called
68
+ end
69
+
70
+ def [](key) # called externally, like so: "message.title"
71
+ @attributes[key]
72
+ end
73
+
74
+ def []=(key,value) # called internally
75
+ @attributes[key] = value if self.respond_to?(key)
76
+ end
77
+
78
+ def attributes=(attributes = {})
79
+ attributes.each_pair do |key, val|
80
+ self[key] = val # []= method call
81
+ end
82
+ end
83
+
84
+ end
85
+ end
data/lib/lc-api.rb CHANGED
@@ -1,3 +1,14 @@
1
- class LcApi
2
- end
1
+ require 'lc-api/api'
2
+ require 'lc-api/resource'
3
+ require 'lc-api/resource/message'
4
+ require 'lc-api/resource/series'
5
+ require 'lc-api/resource/speaker'
6
+ require 'lc-api/resource/location'
7
+ require 'lc-api/resource/staff'
8
+ require 'lc-api/resource/category'
3
9
 
10
+ module LcApi
11
+ class << self
12
+ attr_accessor :key
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lc-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,23 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
  date: 2013-04-25 00:00:00.000000000 Z
14
- dependencies: []
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
15
31
  description: A gem for consuming the LifeChurch.tv API
16
32
  email: daniel@lifechurch.tv
17
33
  executables: []
@@ -19,6 +35,14 @@ extensions: []
19
35
  extra_rdoc_files: []
20
36
  files:
21
37
  - lib/lc-api.rb
38
+ - lib/lc-api/api.rb
39
+ - lib/lc-api/resource.rb
40
+ - lib/lc-api/resource/category.rb
41
+ - lib/lc-api/resource/location.rb
42
+ - lib/lc-api/resource/message.rb
43
+ - lib/lc-api/resource/series.rb
44
+ - lib/lc-api/resource/speaker.rb
45
+ - lib/lc-api/resource/staff.rb
22
46
  homepage: http://rubygems.org/gems/lc-api
23
47
  licenses: []
24
48
  post_install_message: