json_api 1.0.0

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.
Files changed (2) hide show
  1. data/lib/json-api.rb +146 -0
  2. metadata +48 -0
@@ -0,0 +1,146 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'pp'
4
+
5
+ module JsonApi
6
+ def self.included(base)
7
+ base.send :include, InstanceMethods
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module InstanceMethods
12
+ def method_missing(name, *args)
13
+ if [:get, :post, :delete, :put].include? name
14
+ request(name, *args)
15
+ else
16
+ super
17
+ end
18
+ end
19
+
20
+ def request(method, path, params = {})
21
+ path = full_path(path)
22
+
23
+ params = merged_params(params)
24
+
25
+ query_params, form_params = (method == :get ? [params, {}] : [{}, params])
26
+
27
+ uri = uri(path, query_params)
28
+
29
+ req = req(method, uri, form_params)
30
+ configure_request(req)
31
+
32
+ res = http(uri).request(req)
33
+ configure_response(res)
34
+
35
+ log(method, path, params, res)
36
+
37
+ res
38
+ end
39
+
40
+ def full_path(path)
41
+ (path.include?('//') or @base_path.nil?) ? path : "#{@base_path}/#{path}"
42
+ end
43
+
44
+ def merged_params(params)
45
+ @default_params.nil? ? params : @default_params.merge(params)
46
+ end
47
+
48
+ def uri(path, params)
49
+ uri = URI.parse(path)
50
+ uri.query = URI.encode_www_form(params)
51
+ uri
52
+ end
53
+
54
+ def req(method, uri, params)
55
+ req = Net::HTTP.const_get(method.capitalize).new(uri.to_s)
56
+ req.form_data = params
57
+ req
58
+ end
59
+
60
+ def http(uri)
61
+ http = Net::HTTP.new(uri.host, uri.port)
62
+ http.use_ssl = uri.scheme == 'https'
63
+ http
64
+ end
65
+
66
+ def configure_response(res)
67
+ res.instance_variable_set(:@json_api, self)
68
+
69
+ def res.ok?
70
+ code == '200'
71
+ end
72
+
73
+ def res.hash
74
+ @hash ||= JSON.parse(body) rescue {}
75
+ end
76
+
77
+ def res.json
78
+ @json ||= JSON.pretty_generate(hash)
79
+ end
80
+
81
+ def res.error
82
+ @json_api.error(self)
83
+ end
84
+ end
85
+
86
+ def configure_request(req)
87
+ end
88
+
89
+ def log(method, path, params, res)
90
+ return unless @logger
91
+
92
+ @logger.call <<-heredoc
93
+ [JsonApi#request begin]
94
+ # Request
95
+ Method - #{method}
96
+ Path - #{path}
97
+ Params -
98
+ #{params.pretty_inspect.strip}
99
+ # Response
100
+ Code - #{res.code}
101
+ Body -
102
+ #{res.json}
103
+ [JsonApi#request end]
104
+ heredoc
105
+ end
106
+
107
+ def error(res)
108
+ res.message
109
+ end
110
+ end
111
+
112
+ module ClassMethods
113
+ def routes(routes)
114
+ routes.each do |name, path|
115
+ define_method("#{name}_path") do |*args|
116
+ case path
117
+ when String
118
+ path
119
+ when Symbol
120
+ send("#{path}_path", *args)
121
+ when Proc
122
+ path.call(*args)
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def method_missing(name, *args)
129
+ if instance_methods.include? name
130
+ new.send(name, *args)
131
+ else
132
+ super
133
+ end
134
+ end
135
+ end
136
+
137
+ def self.method_missing(name, *args)
138
+ if [:get, :post, :delete, :put].include? name
139
+ api = Class.new.send(:include, self)
140
+ api.new.send(name, *args)
141
+ else
142
+ super
143
+ end
144
+ end
145
+ end
146
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Speransky Danil
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: json-api is a simple library similar to rest-client and httparty, which
15
+ helps with wrappers around APIs.
16
+ email: speranskydanil@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/json-api.rb
22
+ homepage: http://speranskydanil.github.io/json-api/
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: json-api is a simple library similar to rest-client and httparty, which helps
47
+ with wrappers around APIs.
48
+ test_files: []