Class: Jsoneur::Service
- Inherits:
-
Object
- Object
- Jsoneur::Service
- Defined in:
- lib/jsoneur/service.rb
Instance Attribute Summary collapse
-
#default_params ⇒ Object
Config methods.
-
#empty_response ⇒ Object
Config methods.
-
#path ⇒ Object
Config methods.
Instance Method Summary collapse
- #connection(&block) ⇒ Object
-
#get(params = {}) ⇒ Object
Raises (depending on adapter that is used, by default NetHTTP) * Faraday::Error::ConnectionFailed on connection problems * Faraday::Error::TimeoutError on timeouts e.g.
-
#initialize(name, base_url, &block) ⇒ Service
constructor
A new instance of Service.
- #set_faraday_defaults(faraday) ⇒ Object
Constructor Details
#initialize(name, base_url, &block) ⇒ Service
Returns a new instance of Service.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jsoneur/service.rb', line 9 def initialize(name, base_url, &block) @name = name @base_url = base_url # Set default config @default_params = {} @empty_response = [] block.call(self) if @faraday.nil? @faraday = Faraday.new(@base_url) do |f| set_faraday_defaults(f) end end end |
Instance Attribute Details
#default_params ⇒ Object
Config methods
7 8 9 |
# File 'lib/jsoneur/service.rb', line 7 def default_params @default_params end |
#empty_response ⇒ Object
Config methods
7 8 9 |
# File 'lib/jsoneur/service.rb', line 7 def empty_response @empty_response end |
#path ⇒ Object
Config methods
7 8 9 |
# File 'lib/jsoneur/service.rb', line 7 def path @path end |
Instance Method Details
#connection(&block) ⇒ Object
26 27 28 |
# File 'lib/jsoneur/service.rb', line 26 def connection(&block) @faraday = Faraday.new(@base_url, &block) end |
#get(params = {}) ⇒ Object
Raises (depending on adapter that is used, by default NetHTTP)
-
Faraday::Error::ConnectionFailed on connection problems
-
Faraday::Error::TimeoutError on timeouts
e.g. Typhoeus raises Faraday::Error::ClientError as well
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsoneur/service.rb', line 43 def get(params = {}) final_path = path % urlencoded_params(params) result = @faraday.get(final_path, default_params.merge(params)) if result && result.success? && result.body result.body else empty_response end end |
#set_faraday_defaults(faraday) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/jsoneur/service.rb', line 30 def set_faraday_defaults(faraday) faraday.request :json faraday.response :mashify faraday.response :json, :content_type => /\bjson$/ faraday.adapter Faraday.default_adapter end |