api_consumer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/api_consumer.rb +32 -3
  3. metadata +15 -1
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- M2M5NDM5ZDgwZDhjZDQyZjM1MTQwZjJkZmE1YWZmZTZiOGM0OGEwNw==
4
+ NDlmNjk3YWM5NWE3Yjc1ZmY2ZjNlYjdhYjk3NDQ2YWFkODIyNDY0NA==
5
5
  data.tar.gz: !binary |-
6
- YTUzMDRhNzg3ZGQyMjQyZjZhZTdkNGYwZjIzNTBlZmRkMmYyODQyMA==
6
+ MDRjODAxN2E4YmQ3MDQ4OWQ3YTg3ZTQ3NGFjMjU3OWY1Y2Y1NmRkZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzhlMGUyNGMxOThjZmQ2MmM2NDdkZjhhMTM5ZjQ1OTM0OWExYmYxZGVmYWEw
10
- NmNiNzcxMjgyN2Q5YWM1YTczNDIyNTdjZjQwYWRmMDZlNDk1ZTNjMDJmYTU2
11
- MjYyMGY4NmU3ODNiNGFjY2U2NTZjNjU5ZjE1OWM2MjA5YzQ3NzI=
9
+ YzYyYzk0Zjc1ZjQyMzk1NDA0OGZkNjQ2ZGVmMDM4MjY4MzQ4OTAxMDEwYjc5
10
+ NGIwM2E2ZTIxM2Y2NmYyOWIzNjU4ZWU3OGEzYTQ0N2VmMDdiNzViZDBmMjJm
11
+ YTcwMGFhOTlmNmFkNzk2YjY1NWVlOGZhYjk0MDljZjg2ZmNmZTQ=
12
12
  data.tar.gz: !binary |-
13
- M2RiZDMwMDBmYjZkZGMyOWIzNWY1YmIxMGQzYTY3MDI1ZTA5YzI4OWY1MzI5
14
- ZjYxNzk0MzAzY2E4OTc3MzU1MTdiYTQwMGE2ZDE1ZTllOWFjYzE4YWE4M2Vh
15
- NmM5NWU2NzUzNjY2ZWE2Mzc4OTQxNTY4Yzc2ODRhNGNlNTE1MjY=
13
+ Njg1NzViN2NlNGQ3YzI1YTZjZjZiYmY5OGNiMGM4OGNmYjVhN2RmMmY2NTc2
14
+ MWY0NGY1YzEzYTBjODgxMmViMWNkMjQ3MTBlODdjMGVlNDc4MWQ2NjM3MGI4
15
+ NWY5NWE2NmRkOTJmYjQ0M2JlMWI2YjE0NGNkZDFkODlmOTE5MDA=
@@ -3,6 +3,7 @@ class APIConsumer
3
3
  require 'net/https'
4
4
  require 'uri'
5
5
  require 'json'
6
+ require 'nokogiri'
6
7
  require 'uber_cache'
7
8
  require 'logger'
8
9
 
@@ -62,8 +63,21 @@ class APIConsumer
62
63
  @settings ||= {}
63
64
  end
64
65
 
65
- DEFAULT_REQUEST_OPTS = {:method => :get, :headers => { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "EME-WEB-STORE-#{ENV['RACK_ENV']|| 'dev'}" }}
66
- def do_request(path, conn, opts = {})
66
+ DEFAULT_REQUEST_OPTS = {
67
+ :method => :get,
68
+ :headers => {
69
+ "Accept" => "application/json",
70
+ "Content-Type" => "application/json",
71
+ "User-Agent" => "API-CONSUMER-#{ENV['RACK_ENV'] || 'dev'}"
72
+ },
73
+ :ttl => 300
74
+ }
75
+ def do_request(path, conn, opts = {}, &blk)
76
+ if opts[:key] # cache if key sent
77
+ read_val = nil
78
+ return read_val if !opts[:reload] && read_val = cache.obj_read(opts[:key])
79
+ opts[:ttl] ||= settings[:ttl] || DEFAULT_REQUEST_OPTS[:ttl]
80
+ end
67
81
  opts[:headers] = DEFAULT_REQUEST_OPTS[:headers].merge(opts[:headers] || {})
68
82
  opts[:method] = opts[:method] || DEFAULT_REQUEST_OPTS[:method]
69
83
 
@@ -81,13 +95,25 @@ class APIConsumer
81
95
 
82
96
  response = nil
83
97
  begin
98
+ log.warn conn.inspect
99
+ log.warn req.inspect
84
100
  response = conn.request(req)
85
101
  if( settings[:type] == "json")
86
102
  results = JSON.parse(response.body)
87
103
  if ![200, 201].include?(response.code.to_i)
88
104
  results = error_code(response.code, opts[:errors])
89
105
  end
106
+ results = blk.call(results) if blk
107
+ cache.obj_write(opts[:key], results, :ttl => opts[:ttl]) if opts[:key]
90
108
  return results
109
+ elsif( settings[:type] == "rss")
110
+ rss = Nokogiri::XML(response.body)
111
+ return rss.xpath("//item").map{ |i|
112
+ { 'title' => i.xpath('title').inner_text,
113
+ 'link' => i.xpath('link').inner_text,
114
+ 'description' => i.xpath('description').inner_text
115
+ }
116
+ }
91
117
  end
92
118
  rescue Exception => exception
93
119
  log.error exception.message
@@ -96,7 +122,10 @@ class APIConsumer
96
122
  return error_code(response ? response.code : "NO CODE" , opts[:errors])
97
123
  end
98
124
  end
99
- return response.body
125
+ data = response.body
126
+ data = blk.call(data) if blk
127
+ cache.obj_write(opts[:key], data, :ttl => opts[:ttl]) if opts[:key]
128
+ return data
100
129
  end
101
130
 
102
131
  def connection(connection_flag = :normal)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_consumer
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
  - Chris Reister
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement